Skip to content

Instantly share code, notes, and snippets.

@cnrd
Forked from dalethedeveloper/sync_mail.sh
Last active January 16, 2017 21:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Transferring to/from Gmail accounts to a Google Apps account with imapsync. Also good for GApps to GApps transfers, archives, or migrations.
#/!/bin/bash
# Most of the tweaks here are wrapped up in gmailmig
# https://code.google.com/p/gmailmig/
#
# Props to brave folks who figured this out before me
# http://imapsync.lamiral.info/FAQ
# http://www.thamtech.com/blog/2008/03/29/gmail-to-google-apps-email-migration
# http://seagrief.co.uk/2010/12/moving-to-google-apps-with-imapsync/
# http://blog.mcfang.com/tag/imapsync/
#
# Loop thorugh multiple times as things behave better working in 500mb chunks (-exitwhenover 500000000)
# http://mywiki.wooledge.org/ProcessManagement#OK.2C_I_have_its_PID.__How_do_I_check_that_it.27s_still_running.3F
# Going from OLDGMAIL@GMAIL.COM to NEWGMAIL@GAPPS.COM
for i in {1..10}
do
imapsync –split1 100 –split2 100 --fast --syncinternaldates --nocheckmessageexists --allowsizemismatch --exitwhenover 500000000 -host1 imap.gmail.com -host2 imap.gmail.com -port1 993 -port2 993 --ssl2 --ssl1 --authmech2 LOGIN --authmech2 LOGIN \
--pidfile '/tmp/imapsync.pid' \
-user1 $USER1USERNAME -password1 $USER1PASSWORD \
-user2 $USER2USERNAME -password2 $USER2PASSWORD \
# --regexmess 's/Delivered-To: OLDGMAIL\@GMAIL.COM/Delivered-To: NEWGMAIL\@GAPPS.COM/g' \
# --regexmess 's/<OLDGMAIL\@GMAIL.COM>/<NEWGMAIL\@GAPPS.COM>/g' \
--regexmess 's/Subject:(s*)\n/Subject: (no–subject)$1n/g' \
--regexmess 's/Subject: ([Rr][Ee]):(s*)\n/Subject: $1: (no–subject)$2n/g' \
--include '^\[Gmail\]\/All Mail' \
--include '^\[Gmail\]\/Sent' \
--regextrans2 's/\[Gmail\]\///' \
# If you want to keep transferred mail in a separate tag/folder
# --regextrans2 's/(.*)/OLDGMAIL\/$1/' \
--nofoldersizes &
# Use this line instead of the above to dry run and see what folders will go
# --dry --justfolders ;
# Let imapsync fire up and set the hardcoded PID file above in --pidfile
sleep 10
cpid=`cat /tmp/imapsync.pid`
# Peek in periodically to see if we're still running
while kill -0 $cpid
do
sleep 30
done
done
#Tip: Run like this to log: $> nohup ./sync_mail.sh >>sync.log &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment