Skip to content
davidligPublic

About

A simple, YAML-driven CLI tool for migrating multiple email accounts between IMAP servers, powered by imapsync.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

imapshift — Parallel IMAP Account Migration CLI

Language: English | Español

A Python 3.11+ CLI application to automate parallel IMAP-to-IMAP mailbox migrations on Linux servers using imapsync.

Key Features

  • Single config.yaml configuration: keyed directly by email address under accounts, 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.yaml are wrapped with Pydantic SecretStr and redacted across console output and wrapper logs.
    • Temporary --passfile1 and --passfile2 credential files are created with 0600 permissions, wiped, and deleted even after errors or Ctrl+C.
    • Never uses shell=True and never passes passwords in process arguments.
    • Strict TLS certificate verification (SSL_verify_mode=1) and port 993 with SSL/TLS enabled by default.
    • Never deletes messages from the source server.
    • Real migrations never run accidentally without explicit --real.
  • 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+C shutdown, and persistent state tracking for retry.

Linux Requirements

  1. Python 3.11 or newer
  2. Either Docker (gilleslamiral/imapsync, recommended — zero Perl dependencies) or a local imapsync binary in PATH

Option A: Docker (Recommended — Zero Host Dependencies)

With Docker installed (backend: auto uses Docker automatically when local imapsync is absent, or set backend: docker):

docker pull gilleslamiral/imapsync

imapshift 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.

Option B: Local imapsync Installation

  • RHEL / AlmaLinux / Rocky / Fedora (with EPEL enabled on RHEL derivatives):
    sudo dnf install -y epel-release
    sudo dnf install -y imapsync
  • Debian / Ubuntu (imapsync is not shipped in modern Ubuntu/Debian apt repositories; 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.


Project Installation

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.txt

You can also install the package in editable mode to expose the imapshift command:

pip install -e .

Configuration (config.yaml)

  1. Copy the template and restrict permissions since config.yaml stores passwords:
cp config.yaml.example config.yaml
chmod 600 config.yaml

Never commit config.yaml with real passwords to version control (.gitignore already excludes config.yaml).

  1. Edit config.yaml with each account email address as the primary key under accounts:
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"

Configuration Reference

settings section

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.

accounts.<email> section

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.

source and destination options

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).

CLI Usage

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 --real

Accidental-run protection: if settings.dry_run is false and you run python main.py sync without --real or --dry-run, imapshift refuses to start a real migration and asks for explicit --real confirmation.


Logs, Retries, Ctrl+C, and retry

  • Per-account logs: each run writes an independent log file under logs/<account>_<timestamp>.log with imapsync output 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 active imapsync child processes, cleans up all temporary credential files, and marks interrupted accounts as interrupted in the state file.
  • Resuming & repeating migrations:
    • python main.py retry --real re-runs only accounts recorded as failed or interrupted from real runs (simulation --dry-run runs never overwrite real failure state).
    • Running python main.py sync --real again is safe and incremental via --useuid without duplicating messages or deleting source mail.

Running Automated Tests

All tests mock imapsync calls and never connect to real IMAP servers:

pytest -v

About

A simple, YAML-driven CLI tool for migrating multiple email accounts between IMAP servers, powered by imapsync.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages