/ mozey.co / blog

backup gmail

July 1, 2013

🔗 Gmvault

2017-01-27 For some reason offlineimap was marking my old messages as unread in gmail, maybe my config was wrong, but I probably won’t use it again!

Using Python 2.7.12

🔗 Install

pip install gmvault

gmvault -v

🔗 Sync gmail

Setup gmail for imap

gmvault sync [email protected]

Credentials stored in ~/.gmvault

Backup created at ~/gmvault-db

🔗 Calendar

Got to calendar settings

Copy URL for ics private address

wget URL

🔗 offlineimap

One way to keep an offline copy of gmail is to setup a mail client that support imap, Mail.app, Outlook, etc.

But this requires a graphical interface and is not easily automated.

Another option is to use offlineimap

sudo apt-get install offlineimap

🔗 Create a backup folder and config file.

mkdir ~/Gmail && cd ~/Gmail

sudo vi username.imaprc

🔗 Then setup a config file with the account details.

accounts = Gmail
maxsyncaccounts = 3

[Account Gmail]
localrepository = Local
remoterepository = Remote

[Repository Local]
type = Maildir
localfolders = /backup/username/mail

[Repository Remote]
type = IMAP
remotehost = imap.gmail.com
remoteuser = [email protected]
remotepass = password
ssl = yes
maxconnections = 1
realdelete = no

🔗 2016-11-21 Using an Oauth token

gmail blocks offlineimap due to security

Setup offlineimap.conf to use an oauth token, see comment for oauth2_client_secret

Create a google project, enable Gmail API and create credential for “OAuth client ID”

git clone https://github.com/google/gmail-oauth2-tools

python python/oauth2.py --generate_oauth2_token --client_id=YOUR_CLIENT_ID --client_secret=YOUR_CLIENT_SECRET

Use the refresh token, it can be used to request a new access token. The access token will expire

🔗 Update offlineimap conf

oauth2_client_id = YOUR_CLIENT_ID

oauth2_client_secret = YOUR_CLIENT_SECRET

oauth2_request_url = https://accounts.google.com/o/oauth2/token

oauth2_refresh_token = REFRESH_TOKEN

🔗 Now sync the backup folder with the gmail account.

offlineimap -c username.imaprc -u Noninteractive.Basic

🔗 To view the backup use mutt.

sudo apt-get install mutt

mutt -f ~/Gmail/INBOX

🔗 Extract maildir attachments with mu

Install on OSX using brew

brew install mu


Index mailbox (search DB stored at /Users/mozey/.mu/xapian)

mu index -m ~/Gmail

Search email matching SEARCH_TERM


Search email matching SEARCH_TERM with attachments

mu find SEARCH_TERM flag:attach

Get all messages with PDF attachments

mu find mime:application/pdf