Files
ep-stalwart-v2/migration/sync-mailboxes.sh
T

49 lines
1.4 KiB
Bash

#!/usr/bin/env bash
# Runs imapsync (via the upstream gilleslamiral/imapsync image) for every account
# in a migration accounts.csv, copying mail from docker-mailserver to Stalwart.
set -euo pipefail
SOURCE_PORT=993
DEST_PORT=993
DRY_RUN=false
usage() {
echo "Usage: $0 -c accounts.csv -s source-host -t dest-host [-p source-port] [-q dest-port] [-n]" >&2
exit 1
}
while getopts "c:s:t:p:q:n" opt; do
case "$opt" in
c) CSV="$OPTARG" ;;
s) SOURCE_HOST="$OPTARG" ;;
t) DEST_HOST="$OPTARG" ;;
p) SOURCE_PORT="$OPTARG" ;;
q) DEST_PORT="$OPTARG" ;;
n) DRY_RUN=true ;;
*) usage ;;
esac
done
[[ -z "${CSV:-}" || -z "${SOURCE_HOST:-}" || -z "${DEST_HOST:-}" ]] && usage
while IFS=',' read -r email new_password old_password aliases quota_mb; do
[[ -z "$email" ]] && continue
if [[ -z "$old_password" || -z "$new_password" ]]; then
echo "Missing old_password or new_password for $email" >&2
exit 1
fi
echo "==> Syncing $email"
args=(run --rm gilleslamiral/imapsync imapsync
--host1 "$SOURCE_HOST" --port1 "$SOURCE_PORT" --ssl1 --user1 "$email" --password1 "$old_password"
--host2 "$DEST_HOST" --port2 "$DEST_PORT" --ssl2 --user2 "$email" --password2 "$new_password"
--automap --syncinternaldates --skipcrossduplicates --no-modulesversion)
[[ "$DRY_RUN" == true ]] && args+=(--dry)
if ! docker "${args[@]}"; then
echo "warning: imapsync exited non-zero for $email" >&2
fi
done < <(tail -n +2 "$CSV")