System Backup on Unreliable Link thanks to rdiff-backup and rsync

I’ve just write a brand new script called system-backup.py. It’s similar to my website-backup.py script but instead of website and MySQL databases, it is designed to backup systems of several machines. This script is based on an idea from the “Backup up on unreliable link” article from the official rdiff-backup wiki. It use rdiff-backup to keep the last 20 backups and rsync to speed-up the backup process.

I run this script to backup all the local machines within my LAN. I start the backup process everyday thanks to a cron entry similar to this one:

0 20 * * * root /root/system-backup.py >> /mnt/backup-disk/backup.log

If you need more information about the rsync part the script, please have a look to my previous Remote Backup with rsync article, which detail how-to setup key authentification with ssh.

Website Backup Script: MySQL dumps and SSH supported.

Three months after the last version, here is a big update of my backup scripts for websites. The script was greatly improved and among new features, the most important is the support of backups over SSH and backups of MySQL databases.

Change log:

  • Each item of the user’s backup_list must specify the type property (FTP, FTPs, SSH, MySQLdump or MySQLdump+ssh).
  • The property previously known as site is now host.
  • File system structure changed: /ftp-mirror folders renamed to /mirror.
  • Add SSH backups.
  • The script is able to detect if a SSH connexion can be initiated without a password. This was designed for people who don’t like the idea of storing clear password in the script. Thanks to this feature, you can benefit public key authentication from OpenSSH.
  • Use of rsync whenever it’s possible for bandwidth efficiency.
  • FTP and FTPs (aka FTP over SSL) are now handled separately: this suppress the default fall-back to FTP if FTPs is not supported by the remote server. This is safer as it doesn’t let lftp make the decision for you to send your clear password on the net.
  • All ports are optionnal, no need to specify it you use default ports.
  • Add MySQL backups thanks to mysqldump.
  • Two mode of MySQL backups: through SSH or direct connection to server.
  • A particular database to backup can be specified. Else, all databases are backed up.
  • Much more detailed logs that include external command’s output.
  • Auto-detect the existence of required external tools and commands at launch.
  • Use pexpect lib to simulate user password input.
  • Run all external commands in english for consistency.
  • Check that the script is running in a posix environnement.
  • Fix bug related to directory creation.

If you were using a previous version of my backup script and want to use this updated version, take care of changes, especially the ones describes in the first 3 items of the change log above.

Remote Backup with rsync

This little article describe how to setup an automatic backup procedure to a remote machine via the rsync tool.

Prerequisites

  • A distant server, where backup will be stored (homeserver.com in this case),
  • A user account on this server (mine was kevin),
  • A ssh deamon running on the server that allow the user to log in.

Setup rsync

First, install rsync on the client and on the server using:

urpmi rsync

Synchronization

Then, to synchronise from the local machine to the distant server, just do:

rsync -avz -e ssh /home/client_user/Documents kevin@homeserver.com:/mnt/raid2/
  • /home/client_user/Documents is the local folder we want to save (located in the home folder of the client user client_user),
  • homeserver.com is the distant server name (could be en IP address),
  • kevin is the distant user,
  • /mnt/raid2/ is the distant folder where we want to save the local one.

Croned synchronization

First, create a pair of cryptographic keys (public, private):

ssh-keygen -t rsa

Then, from the local machine as user client_user, register you on the distant server:

ssh-copy-id -i ~/.ssh/id_rsa.pub kevin@homeserver.com

In case your distant machine’s SSH server is running on another port than 22 (which is the default port), let’s said 222, here is the command that emulate ssh-copy-id (as the later doesn’t have a port parameter):

cat ~/.ssh/id_rsa.pub | ssh -p 222 kevin@homeserver.com "cat >> ~/.ssh/authorized_keys"

Create a script named rsync_data_backup.sh that contain the command you’ve used previously to synchronize your data:

rsync -avz -e ssh /home/client_user/Documents kevin@homeserver.com:/mnt/raid2/

To run this script with a cron entry, the (unsecure) solution found is to create a key without a passphrase. The cron entry could be something like:

15 13 * * 1-5 client_user /home/client_user/rsync_data_backup.sh > /home/client_user/rsync_data_backup.log

This crontab entry will automaticcaly synchronise our data each first-5 days of the week, at 13:15.