Language: English | Español
A Python 3.11+ CLI application to automate parallel IMAP-to-IMAP mailbox migrations on Linux servers using imapsync.
- Single
config.yamlconfiguration: keyed directly by email address underaccounts, storing account credentials and server settings in one place (no.env, no CSV, no TOML, and no complex account lists). - Security by default:
- Passwords in
config.yamlare wrapped with PydanticSecretStrand redacted across console output and wrapper logs. - Temporary
--passfile1and--passfile2credential files are created with0600permissions, wiped, and deleted even after errors orCtrl+C. - Never uses
shell=Trueand never passes passwords in process arguments. - Strict TLS certificate verification (
SSL_verify_mode=1) and port993with SSL/TLS enabled by default. - Never deletes messages from the source server.
- Real migrations never run accidentally without explicit
--real.
- Passwords in
- Complete and repeatable sync: migrates all messages, folders, original internal dates (
--syncinternaldates), read/unread flags, starred messages, and special folders (--automap), and can be re-run safely without duplicating messages (--useuid). - Parallel & observable execution: configurable worker pool, independent log files per account, controlled retries on transient errors, graceful
Ctrl+Cshutdown, and persistent state tracking forretry.
- Python 3.11 or newer
- Either Docker (
gilleslamiral/imapsync, recommended — zero Perl dependencies) or a localimapsyncbinary inPATH
With Docker installed (backend: auto uses Docker automatically when local imapsync is absent, or set backend: docker):
docker pull gilleslamiral/imapsyncimapshift runs gilleslamiral/imapsync with --rm --init --network host, mounting only the temporary 0700 passfile directory read-only (:ro) and logs/ under your user UID/GID, and removes the container cleanly on Ctrl+C.
- RHEL / AlmaLinux / Rocky / Fedora (with EPEL enabled on RHEL derivatives):
sudo dnf install -y epel-release sudo dnf install -y imapsync
- Debian / Ubuntu (
imapsyncis not shipped in modern Ubuntu/Debianaptrepositories; install its Perl dependencies and the official upstream repository):sudo apt update sudo apt install -y \ libauthen-ntlm-perl libclass-load-perl libcrypt-openssl-rsa-perl \ libdata-uniqid-perl libdigest-hmac-perl libdist-checkconflicts-perl \ libencode-imaputf7-perl libfile-copy-recursive-perl libfile-tail-perl \ libio-compress-perl libio-socket-inet6-perl libio-socket-ssl-perl \ libio-tee-perl libjson-webtoken-perl libmail-imapclient-perl \ libmodule-scandeps-perl libnet-dbus-perl libnet-dns-perl \ libnet-ssleay-perl libproc-processtable-perl libreadonly-perl \ libregexp-common-perl libsys-meminfo-perl libterm-readkey-perl \ libunicode-string-perl liburi-perl make git wget git clone https://github.com/imapsync/imapsync.git /tmp/imapsync sudo make -C /tmp/imapsync install rm -rf /tmp/imapsync
Verify either docker image inspect gilleslamiral/imapsync or imapsync --version succeeds.
Clone or copy the repository to your Linux server and create a virtual environment:
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txtYou can also install the package in editable mode to expose the imapshift command:
pip install -e .- Copy the template and restrict permissions since
config.yamlstores passwords:
cp config.yaml.example config.yaml
chmod 600 config.yamlNever commit
config.yamlwith real passwords to version control (.gitignorealready excludesconfig.yaml).
- Edit
config.yamlwith each account email address as the primary key underaccounts:
settings:
dry_run: true
workers: 3
continue_on_error: true
automap: true
accounts:
info@example.com:
source:
host: imap.source.example.com
user: info@example.com
password: "source-password"
destination:
host: imap.dest.example.com
user: info@example.com
password: "destination-password"
sales@example.com:
source:
host: imap.other-source.example.org
port: 993
user: sales@example.com
password: "sales-source-password"
destination:
host: imap.other-dest.example.org
port: 993
user: sales@example.com
password: "sales-destination-password"| Key | Default | Description |
|---|---|---|
dry_run |
true |
When true, sync without flags defaults to simulation mode (--dry). |
workers |
3 |
Number of concurrent migration workers (1..32). |
continue_on_error |
true |
When false, stops queuing new accounts after a permanent/exhausted failure. |
automap |
true |
Enables imapsync --automap to map special folders. |
backend |
auto |
Execution backend: auto (local imapsync, else Docker), local, or docker. |
docker_image |
gilleslamiral/imapsync |
Docker image used when running with the docker backend. |
connect_timeout |
10.0 |
Timeout in seconds for pre-flight IMAP connection & login checks. |
retries |
2 |
Automatic retries for transient failures (network/timeouts). |
retry_delay |
5.0 |
Initial delay in seconds before retrying (exponential backoff). |
log_dir |
logs |
Directory where per-account log files are stored. |
state_file |
.imapshift/state.json |
Persistent JSON state file tracking the latest real run for retry. |
| Key | Default | Description |
|---|---|---|
active |
true |
Set to false to disable migrating this account. |
automap |
(global) | Optional per-account override for settings.automap. |
exclude_folders |
[] |
Optional list of folder names passed to --exclude. |
source / destination |
(required) | Independent source and destination IMAP server settings. |
| Key | Default | Description |
|---|---|---|
host |
(required) | IMAP server hostname or IP address. |
port |
993 |
IMAP port (1..65535). |
user |
<account email> |
IMAP login username. Defaults to the account's email key if omitted. |
password |
(required) | Account password for this IMAP endpoint (must be non-empty, single-line). |
tls |
ssl |
Transport security mode: ssl (IMAPS), starttls (STARTTLS), or none (unencrypted). |
verify_tls |
true |
Enforces TLS certificate verification (SSL_verify_mode=1). |
All commands support --config <path> to select another YAML file:
# List configured accounts and their last real-run status
python main.py list
# Validate YAML configuration, file permissions, and imapsync backend
python main.py check
# Validate configuration AND test live IMAP connection & login for active accounts
python main.py check --connect
# Simulate all migrations (runs pre-flight connection check first)
python main.py sync --dry-run
# Migrate all active accounts (aborts before copying if any account fails connection check)
python main.py sync --real
# Skip the pre-flight connection check if needed
python main.py sync --real --skip-connection-test
# Migrate a single account
python main.py sync info@example.com --real
# Migrate with 5 concurrent workers
python main.py sync --workers 5 --real
# Retry only failed or interrupted accounts
python main.py retry --real
# Use an alternative YAML configuration file
python main.py check --config production.yaml
python main.py sync --config production.yaml --realAccidental-run protection: if
settings.dry_runisfalseand you runpython main.py syncwithout--realor--dry-run,imapshiftrefuses to start a real migration and asks for explicit--realconfirmation.
- Per-account logs: each run writes an independent log file under
logs/<account>_<timestamp>.logwithimapsyncoutput and redacted diagnostics. - Controlled retries: transient errors (timeouts, socket resets) are retried automatically with exponential backoff; permanent failures (authentication errors, TLS verification errors) fail fast without wasting retries.
Ctrl+C(SIGINT) handling: stops scheduling new accounts, sends termination signals to activeimapsyncchild processes, cleans up all temporary credential files, and marks interrupted accounts asinterruptedin the state file.- Resuming & repeating migrations:
python main.py retry --realre-runs only accounts recorded asfailedorinterruptedfrom real runs (simulation--dry-runruns never overwrite real failure state).- Running
python main.py sync --realagain is safe and incremental via--useuidwithout duplicating messages or deleting source mail.
All tests mock imapsync calls and never connect to real IMAP servers:
pytest -v