
Backup using rsync
Overview
Backing up your critical data is essential. This is one way to do it, describing a way to automize backups from one Linux machine to another (in my example two Bubba servers, but it could be any two Linux machines).
I use rsync, which contains a clever algorithm identifying what's changed since the last backup, and only copying the changed files. This means that a complete file copy doesn't have to be transferred on every backup occation, it only transfers the files that have changed.
The rsync command is initiated with a bash script, that in this case also does some copying and emails the backup status to the a user.
Finally, the script is added as a cron entry, making it run regularly, in my case, once every night.
I need to backup the email handled by my main Bubba, to a secondary Bubba acting (among other things) as a backup server. Files are copied from my main Bubba (used as email and web server) to my backup-Bubba. The script is run at the backup-Bubba, thus, the files are "pulled" to that machine. In the following guide, the machines are denoted as main-Bubba and backup-Bubba. I have called the users on each machine "main-user" and "backup-user".
Note: I give no warranties that this actually works, or even does what I intend. Use this guide at your own risk, for educational purposes only. Test it thoroughly before trusting it as a backup solution. Excito is not responsible for the content herein.
Now, to the guide:
Setting up "backup-Bubba"
Log on as backup-user, then do 'su' to become root. Uncomment the debian sources from /etc/apt/sources.list, then do:
Code: Select all
apt-get install rsync
Type exit to return as your normal user, "backup-user".
Now, create a directory holding your script:
Code: Select all
mkdir /home/backup-user/cron
Code: Select all
mkdir /home/backup-user/backup
Code: Select all
rsync -av -e ssh main-user@main-Bubba:/home/main-user/something_to_backup /home/backup-user/backup/
Since we want this to be automated, typing your password every time isn't an option. This can be bypassed by depositing ssh keys on main-Bubba (google for 'ssh-keygen' if you wan't to dig deeper). To generate ssh keys:
Code: Select all
ssh-keygen -t dsa -b 1024 -f /home/backup-user/cron/backup-Bubba-rsync-key
Copy the public key file from backup-Bubba to main-Bubba, using scp:
Code: Select all
scp /home/backup-user/cron/backup-Bubba-rsync-key.pub main-user@main-Bubba:/home/main-user/.ssh
Setting up "main-Bubba"
Log on as main-user, then do 'su' to become root. Uncomment the debian sources as before, then do:
Code: Select all
apt-get install rsync
Add the key to the authorized_keys file:
Code: Select all
cat bubba-01-rsync-key.pub >> .ssh/authorized_keys
Now, back to backup-Bubba for further testing:
Testing the ssh and rsync connection
To test the ssh-connection, without having to use your password:
Code: Select all
ssh -i /home/backup-user/cron/backup-Bubba-rsync-key main-user@main-Bubba
And to test an rsync transfer in the same manner:
Code: Select all
rsync -av -e "ssh -i /home/backup-user/cron/backup-Bubba-rsync-key" main-user@main-Bubba:/home/main-user/something_to_backup backup
Setting up a cron job to do this regularly
Create the script file:
Code: Select all
nano /home/backup-user/cron/backup-script
Code: Select all
#!/bin/sh
rsync -av -e "ssh -i /home/backup-user/cron/backup-Bubba-rsync-key" main-user@main-Bubba:/home/main-user/something_to_backup/
/home/backup-user/backup/
Code: Select all
chmod +x backup-script
Code: Select all
./backup-script
Code: Select all
crontab -e
And put something like:
Code: Select all
0 23 * * * /home/backup-user/cron/backup-script
Code: Select all
42 07 19 * * /home/backup-user/cron/backup-script
Other functionality in your backup script
You can add just about anything there, for example emailing of the backup status, using a script looking something like this:
Code: Select all
#!/bin/sh
MESSAGE="/tmp/emailmessage.txt" #A temp file storing the contents of your email message
echo "Your backup started at " > $MESSAGE
DATE=`date +%y%m%d-%T`
echo $DATE >> $MESSAGE
echo "..with the following log information:" >> $MESSAGE
#Do the actual file transfer
#(The --delete option means that files deleted at main-Bubba also will be deleted at backup-Bubba, something that fits me better
#since I'm backing up email.)
rsync -avz --delete -e "ssh -i /home/backup-user/cron/backup-Bubba-rsync-key" main-user@main-Bubba:/home/main-user/Mail/ current
>> $MESSAGE
#Create a new folder with todays date and time
DATE=`date +%y%m%d-%T`
mkdir $DATE >> $MESSAGE
#Copy the contents of your current backup to todays folder
cp -r current/ $DATE/ >> $MESSAGE
echo "..and ended at" >> $MESSAGE
echo $DATE >> $MESSAGE
mail -s "Backup done" you@yourdomain.com < $MESSAGE

Also, don't forget to restore /etc/apt/sources.list when you are done!