Using Duplicity with VPS.NET's Rsync Backup space
Did you know VPS.NET offers an offsite backup service for your VPS.NET server for only $5 a month? It’s under “Edit VPS” in your VPS.NET control panel; just order the Rsync back up service and you’re ready to go. Here’s a tip on how to maximize your experience using Duplicity!
Duplicity is a piece of software that I highly recommend any webmaster look into. Duplicity backs up entire directories in a tar format, and then encrypts them using GnuPG, meaning that they cannot be viewed or tampered with by any third parties. It is still in beta, however we’ve put it through some pretty thorough testing and have not come across any detrimental problems. For this tutorial we’re going to be backing up the /home directory and the /mysql directory, which will allow us to save all files and MySQL database content.
Installing Duplicity on CentOS5.
wget http://code.launchpad.net/duplicity/0.6-series/0.6.08b/+download/duplicity-0.6.08b.tar.gz
tar xfz duplicity-0.6.08b.tar.gz
cd duplicity-0.6.08b
python setup.py install
Because Duplicity uses librsync to keep incremental backups, we’ll need to make sure that librsync is installed on your cloud VPS server.
rpm -ivh http://yum.jardiknas.org/atomic/centos/5/x86_64/RPMS/librsync-0.9.7-9.el5.art.x86_64.rpm
rpm -ivh http://yum.jardiknas.org/atomic/centos/5/x86_64/RPMS/librsync-devel-0.9.7-9.el5.art.x86_64.rpm
Generate your GPG keys to encrypt the backups:
gpg –gen-key
gpg –list-keys
/root/.gnupg/pubring.gpg
————————
pub 1024D/0A1C1C1C 2010-04-02
uid RsyncBackup (rsyncbackup)
<root@localhost>
sub 2048g/5D1D326F 2010-04-02
The GPG key ID in this test for duplicity is 0A1F1C50, and should be used to encrypt the backups. The key that is generated on your cloud VPS will be different. Please keep note what your generated GPG key is, as it will be necessary later. You can use the following command to test out the GPG key and Duplicity.
duplicity full –volsize=200 –encrypt-key=”0A1C1C1C” /var/named scp://1234@rsync1.cloudkeeper.net/domain.com/var_test/
Configuring Duplicity to work with cPanel
One problem that we’ve found is Duplicity attempts to save everything in /tmp. In most situations there is not enough space there to support the backups, so we instead need to create a new directory capable of handling our backups.
mkdir /usr/local/tmp
chmod 1777 /usr/local/tmp
ls -ld /usr/local/tmp
drwxrwxrwt 2 root root 4096 Apr 5 12:36 /usr/local/tmp/
Before we create the backup scripts, we’re going to backup our databases to the /home partition. This not only creates a second backup of your MySQL databases, but it will also make restoration much easier in the event of a failure.
mkdir /home/mysqlbackup
chmod 1777 /home/mysqlbackup
vi /home/mysqlbackup/mysql-daily-backup.sh
Paste the following command into the file:
mysqldump -uDBUSER DBNAME -pDBPASSWORD > /home/mysqlbackup/DBNAME.sql
You will need to replace DBUSER with the database username, DBNAME with the database name, and DBPASSWORD with the password for the MySQL user.
If you have multiple databases that you wish to backup, you will need to paste that line in for each database.
Now create a daily crontab to run the script. I use 12AM in the example, however for larger databases, you may wish to start it around 9-10PM.
0 0 * * 1-7 sh /home/mysqlbackup/mysql-daily-backup.sh
Now lets create two scripts that will handle our daily and weekly backups.
Creation of the daily file:
mkdir /usr/local/custom-backup
vi /usr/local/custom-backup/backup-daily.sh (nano will also work)
Paste the following script into the backup-daily.sh file. Make sure to edit the password (1234 is an example password) and domain.com to your backups settings. As well, make sure to edit your GPG key settings to the key that was generated earlier.
#!/bin/bash
export TMPDIR=/usr/local/tmp/
duplicity remove-older-than 14D –encrypt-key=”0A1C1C1C”
scp://1234@rsync1.cloudkeeper.net/domain.com/home/
duplicity remove-older-than 14D –encrypt-key=”0A1C1C1C”
scp://1234@rsync1.cloudkeeper.net/domain.com/mysql/
duplicity inc –volsize=200 –encrypt-key=”0A1C1C1C”
–exclude=/home/virtfs /home/
scp://1795@rsync1.cloudkeeper.net/domain.com/home/
duplicity inc –volsize=200 –encrypt-key=”0A1C1C1C” /var/lib/mysql/
scp://1234@rsync1.cloudkeeper.net/domain.com/mysql/
Now lets create the weekly backup script:
vi /usr/local/custom-backup/backup-weekly.sh
Paste the following into the backup-weekly.sh file:
#!/bin/bash
export TMPDIR=/usr/local/tmp/
duplicity full –volsize=200 –encrypt-key=”0A1C1C1C”
–exclude=/home/virtfs /home/
scp://1234@rsync1.cloudkeeper.net/domain.com/home/
duplicity full –volsize=200 –encrypt-key=”0A1C1C1C” /var/lib/mysql/
scp://1234@rsync1.cloudkeeper.net/domain.com/mysql/
Finally, add two crontabs to set the backups to run:
10 2 * * 1-6 sh /usr/local/custom-backup/backup-daily.sh
10 2 * * 0 sh /usr/local/custom-backup/backup-weekly.sh
Both backups are set to run at 2:10AM. The daily backup will run 6 days a week, backing up only incremental data changes. On Sunday, the weekly backup will run, doing a full backup of all your data.
Useful links:
http://duplicity.nongnu.org/FAQ.html
http://www.rsync.net/resources/howto/duplicity.html
(Thank you Bogdan for writing this up for me!)