Hi,
I do similar what you're trying to achieve using imapsync in Docker:
imapsync
Every morning my emails are synced from my Gmail account to MailPlus and then deleted from my Gmail account. It can be configured depending on your needs (see here:
GitHub - imapsync) but here's what I have:
Code:
volume1
docker
imapsync
imapsync_settings_gmail.txt
imapsync.sh
- file 'imapsync_settings_gmail.txt':
Code:
imapsync_gmail;imap.gmail.com;[gmail username];[gmail app-specific password];[nas ip address];[mailplus username];[mailplus password];--port1 993 --ssl1 --port2 993 --ssl2 --addheader --syncinternaldates --nofoldersizes --nofoldersizesatend --noreleasecheck --gmail1 --subfolder2 Gmail --exclude Spam --exclude All --delete1 --noexpungeaftereach;
This is a parameter file that is passed to the shell script below. Some parameters of note:
--gmail1: applies some pre-defined settings specific to Gmail.
--subfolder2: stores the e-mails in the defined sub-folder (mine is simply called "Gmail").
--exclude: defines which folders to exclude from the sync. I exclude "Spam" and "All" folders.
--delete1: deletes all relevant e-mails from Gmail.
Additional parameters you might want to try:
--minage 730: does not sync messages newer than 730 days (2 years) old.
--dry: add this to do a dry-run without actually syncing anything. You can check the logs to see if it would do what you want.
- file 'imapsync.sh': shell script to run imapsync using the parameters defined in the file above:
Bash:
#!/bin/sh
#
# $Id: sync_loop_unix.sh,v 1.13 2022/01/09 09:53:47 gilles Exp gilles $
# Example for imapsync massive migration on Unix systems.
# See also http://imapsync.lamiral.info/FAQ.d/FAQ.Massive.txt
#
# Data is supposed to be in file.txt in the following format:
# host001_1;user001_1;password001_1;host001_2;user001_2;password001_2;;
# ...
# The separator is the character semi-colon ";"
# this separator character can be changed to any character
# by changing IFS=';' in the while loop below.
#
# Each line contains 7 columns. These columns are the 6 parameter values
# for the imapsync command options
# --host1 --user1 --password1 --host2 --user2 --password2
# plus an extra column for extra parameters and a trailing fake column
# to avoid CR LF part going in the 7th parameter extra.
# So don't forget the last semicolon, especially on MacOS systems.
#
# You can also add extra options in this script after the variable "$@"
# Those options will be applied in every imapsync run, for every line.
# The imapsync command below is written in two lines to avoid a long line.
# The character backslash \ at the end of the first line is means
# "the command continues on the next line".
#
# Use character backslash \ at the end of each supplementary line,
# except for the last line.
#
# You can also pass extra options via the parameters of this script since
# they will be in "$@". Shell knowledge is your friend.
# The credentials filename "file.txt" used for the loop can be renamed
# by changing "file.txt" below.
# The file file_failures.txt will contain the lines from file.txt that ended
# up in error, for whatever reason. It's there to notice and replay easily
# the failed imapsync runs. Is is emptied at the beginning of the loop run.
# I let you junggle with it.
file=$1
echo Looping on accounts credentials found in $file
echo
line_counter=0
# Empty the error listing
> file_failures.txt
{ while IFS=';' read n h1 u1 p1 h2 u2 p2 extra fake
do
line_counter=`expr 1 + $line_counter`
successMessage="success sync for line $line_counter "
failureMessage="$n;$h1;$u1;$p1;$h2;$u2;$p2;$extra;"
{ echo "$n" | tr -d '\r' | egrep '^#|^ *$' ; } > /dev/null && continue # this skip commented lines in file.txt
echo "==== Starting imapsync with --name $n --host1 $h1 --user1 $u1 --host2 $h2 --user2 $u2 $extra $@ ===="
if docker inspect -f '{{.Config.Image}}' $n >/dev/null 2>&1
then
echo "Using existing container: $n"
if docker start $n
then
echo $successMessage
else
echo $failureMessage | tee -a file_failures.txt
fi
else
echo "Creating container: $n"
if docker run --name $n gilleslamiral/imapsync:latest imapsync --host1 "$h1" --user1 "$u1" --password1 "$p1" \
--host2 "$h2" --user2 "$u2" --password2 "$p2" $extra
then
echo $successMessage
else
echo $failureMessage | tee -a file_failures.txt
fi
fi
echo "==== Ended imapsync with --name $n --host1 $h1 --user1 $u1 --host2 $h2 --user2 $u2 $extra $@ ===="
echo
done
} < $file
(It's recommended to use 'passfile1' and 'passfile2' instead of 'password1' and 'password2' but I could never get that to work. This is not an issue unless someone else has access to your logs).
The script checks if an imapsync image exists. If not then the latest version is downloaded. Then imapsync is executed. The container is stopped when the sync is completed.
- User-defined script scheduled to run daily:
Code:
cd /volume1/docker/imapsync
./imapsync.sh imapsync_settings_gmail.txt
This just runs the shell script with the parameter file as input.
This has been running on my NAS for about a year and works great. Hope it helps.