vps

 
 

Remote Backup of Files, Subversion, and MySQL Using Rsync and Ssh

TuxRecently my VPS provider was doing some power maintenance and recommended backing up data.  I realized I hadn't made a backup of that system in very long time, and hadn't worked up a backup strategy.  This caused me to whip up a backup solution using my two favorite things; ssh and rsync.

In the past I had used tar and scp to do remote backups, but this painfully slow, and I don't have a requirement to do moment in time backups, so the amazing efficiency of rsync.  The only trick was that this server has subversion and MySQL databases, and doing a straight copy of the data for these apps is extremely dangerous.  So with a couple remote ssh commands run from the backup machine I ended up with a slick, simple, fast, and reliable backup script:

#!/bin/bash

# Individually backup relevant etc configurations using rsync
# with the archive and compressions flags set
rsync -az -e "ssh -i path/to/ssh/key" backup_user@example.org:/etc/apache2 /data/backup/etc/
.
.
rsync -az -e "ssh -i path/to/ssh/key" backup_user@example.org:/etc/ssl /data/backup/etc/

# Use remote commands to trigger app specific backups for MySQL and subversion

# Run safe backup of all MySQL databases to a location that gets rsynced later
ssh -i path/to/ssh/key backup_user@example.org "mysqldump -u root -pPassword --all-databases > /opt/backup/mysql-current.sql"

# Run safe backup of subversion using svnadmin dump to a directory that gets rsynced
ssh -i path/to/ssh/key backup_user@example.org "svnadmin dump --quiet /opt/repos/svn > /opt/backup/svn.current.dump"

# Sync /opt.  Backup everything.  This will grab the remote MySQL and subversion backups
# we just made.
rsync -az -e "ssh -i path/to/ssh/key" backup_user@cexample.org:/opt/ /data/backup/opt/

That is it. I get a safe backup of my databases and svn plus all of my file data ~8 GiB total, and after the initial backup it all happens in less than 5 minutes.

Attach it all to crontab like:

0 * * * * /usr/local/bin/backup.py

and you get an hourly synchronized copy of the remote server.

 

 
 
Syndicate content