Everybody is aware of how important it is to perform backup. This is especially true when you do not own the architecture on which your files are hosted.
Many of you probably remember the outage that GMail faced a bit more than a year ago when accounts have been accidentally deleted. Most of them (all?) have eventually been restored but this incident was probably a wake-up call for many. Once again, unless you have a local backup of your data, you are actually not owning anything when working with cloud services.
Quite a few websites have been spreading the word about GMVault lately. This is a great initiative that allows to easily backup data from your GMail account and easily restore them in case you need to. In order to automate the process, and ensure that backups are taken on a regular basis, I have configured a NAS (Synology DS111) to perform this operation.
Installing GMVault on a Synology NAS requires a few things to be in place. I will detail the different commands below but I assume that you already have IPKG installed on your NAS (if this is not the case, follow the instructions on Synology forum).
Login to NAS
Login as root through SSH (this is the same as the admin password).
Install Python 2.7
ipkg install python2.7
Before installing Python setuptools that are required by GMVault, it is necessary to configure Python. I found the instructions below on a QNAP forum but they apply on Synology as well.
cd /opt/bin ln -s /opt/bin/python2.7 python ln -s /opt/bin/pydoc-2.7 pydoc export PYTHONPATH=/opt/local/lib/python2.5/site-packages
(Note: I saw a possible alternative method here which consists in simply running
ipkg install py27-setuptools but I haven’t tried it.)
Have a look at the setuptools download page to select the correct version of setuptools to install and then follow the installation instructions on the same page. The specified prefix instruction (
--prefix=/opt) in the command below comes from the following guidelines.
cd /volume1/@tmp wget http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg#md5=fe1f997bc722265116870bc7919059ea sh setuptools-0.6c11-py2.7.egg --prefix=/opt
GMVault will try to compile a few things during the installation process. It is therefore easier to have GCC already installed.
ipkg install gcc
We will be installing GMVault from the sources. At this stage, I haven’t installed the wget package allowing to download through secure connection. I have therefore downloaded the package locally and uploaded to one of the Synology folders. I assume here that it is in the data folder.
cp /volume1/data/gmvault-1.6-beta-src.tar.gz /volume1/@tmp tar -xvzf gmvault-1.6-beta-src.tar.gz cd gmvault-1.6-beta/
Follow the installation instructions. Once again, I have specified the prefix option
--prefix=/opt based on the instruction from Synology.
python setup.py install --prefix=/opt
During the installation, one of the log line should look like:
Installing gmvault script to /opt/bin. This gives information about where GMVault script has been installed. If everything goes well, one should be able to run
which gmvault and obtain the folder in which the script is available (in this case
GMVault will use the $HOME folder of the user launching the script to store credential information of the GMail account. It is therefore important to configure SSH properly (see for instance here or there) to ensure that credentials will be stored in the appropriate folder.
As a normal user, run the gmvault script. To get an overview of the available options, run
sh /opt/bin/gmvault sync -h. In my case, I’d like the backup to be stored in a specific folder. It is possible to encrypt the emails as well with the
-e option, but it takes quite some time to execute the first backup in that case.
sh /opt/bin/gmvault sync --db-dir /volume1/data/backups/email firstname.lastname@example.org
During the first connection, GMVault will provide a URL in order to allow access to the GMail account. To easily grant access, one can simply paste the URL in any browser (i.e. it is not necessary to use a browser like lynx on Synology).
The first synchronization might take some time depending on the size of your mailbox. Once the original backup is made, one can use the
--resume option to continue from the last saved email or / and the
-t quick option to get only the latest emails.
sh /opt/bin/gmvault sync --resume -t quick --db-dir /volume1/data/backups/email email@example.com
Setup Cron Job
Since the purpose of this article was really about “set and forget”, we will conclude by setting up a cron job to execute the recurrent backup of the GMail account.
Create a file wherever you like and edit it. For this example, I set it up in the data partition mentioned above. I use nano as my editor of choice, if you haven’t it installed, simply run
ipkg install nano as root.
In this file, copy the following code. For logging purposes, it echoes a few information about the user environment.
#!/bin/sh echo "Home: " echo $HOME echo "Logname: " echo $LOGNAME sh /opt/bin/gmvault sync --resume -t quick --db-dir /volume1/data/backups/email firstname.lastname@example.org
Save the file (
ctrl + o) and close it (
ctrl + x).
Finally, set up the cron job (
nano /etc/crontab) by adding the following line at the end of the file. Simply replace
username by the user that should run the script and ensure that it is written on a single line.
0 0,12 * * * username su -c "sh /volume1/data/gmail_backup.sh" - username 1>/volume1/@tmp/gmail.log 2>/volume1/@tmp/gmail.err
I initially set up the cron job below, but it seems that there is a bug in the way cron jobs are executed on a Synology. Even though
username was specified as the person who should run the script, the root environment was loaded. It therefore resulted in an error as the GMail credentials were not loaded properly. In order to run the script as the normal user
username, I decided to run it through the
su - username command as seen above.
DEPRECATED 0 0,12 * * * username sh /volume1/data/gmail_backup.sh 1>/volume1/@tmp/gmail.log 2>/volume1/@tmp/gmail.err
Restart the crond service (Basic commands to get around the Synology Box using the CLI)
/usr/syno/etc/rc.d/S04crond.sh stop /usr/syno/etc/rc.d/S04crond.sh start
And that’s it. You can verify that everything runs properly by having a look at the logs files in
Your GMail account is now backed-up on a regular basis and restoring it is just one command line away (see
sh /opt/bin/gmvault restore -h).