Skip to content

Instantly share code, notes, and snippets.

@dalethedeveloper
Last active April 14, 2021 10:21
Show Gist options
  • Save dalethedeveloper/5549712 to your computer and use it in GitHub Desktop.
Save dalethedeveloper/5549712 to your computer and use it in GitHub Desktop.
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 [email protected] to [email protected]
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 [email protected] -password1 OLDGMAILPASSWORD \
-user2 [email protected] -password2 NEWGAPPSPASSWORD \
--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