Difference between revisions of "Rsync"
Revision as of 17:33, 21 January 2009
The rsync protocol can be used to make quick and easy backups, as it only transfers files that have changed, which means backups are much quicker, than if you just copied all your files over every time. This tutorial aims to show you how to make a scheduled backup, using rsync, typically to a removable media.
Obviously, to use rsync, you need rsync:
# pacman -S rsync
We need to make a script that tells rsync to backup your data.
Open a text editor, for example:
# nano /etc/cron.daily/rsync.backup
In this location, it will be run daily, without any further configuration.
You need to paste this script into the text editor:
#!/bin/bash rsync -ar --delete /folder_to_backup/ /location_to_backup/ &> /dev/null
-a means files are archived, -r means files are copied recursively, and --delete means files deleted on the source are deleted on the backup
Here, "folder_to_backup" needs to be changed to what you want to backup (/home/ , for example) and "location_to_backup" is where you want to back it up to (/media/disk , for example).
If you want to backup to a remote host using SSH, use this script instead:
#!/bin/bash rsync -ar --delete -e ssh /folder_to_backup/ remoteuser@remotehost:/location_to_backup/ &> /dev/null
-e ssh tells rsync to use ssh, remoteuser is the user on the host remotehost
We now need to make it executable:
#chmod +x /etc/cron.daily/rsync.backup
And that's it. It'll now run daily, as long as your cron daemon is configured correctly (which is out of the scope of this document).
To check if rsync actually has preformed a backup you can use a log, here's a simple example:
#echo beginning backup `date` >> ~/RsyncBackupLog
Put this line in your backup script and it will write "beginning backup 02-01-2009" in the text file RsyncBackupLog now you can see when the last backup is made.
You can also log the output of rsync:
#rsync -ar --delete /folder_to_backup/ /location_to_backup/ >> ~/Rsync.log
The log can be find in your home directory and it's called Rsync.log
When you are making a backup to a server you can build in a check if the server is online:
#if ping -c 1 -w 5 10.0.0.102 > /dev/null ; then mount the share rsync -ar --delete -e ssh /folder_to_backup/ remoteuser@remotehost:/location_to_backup/ &> /dev/null else echo echo backup failed `date` >> ~/RsyncBackupLog fi
For more examples of really great bash scripting you should just google for 'rsync backup script' or go to the 'General Programming Forum' of the Arch forum