Data backup with rsync
I’ve always wanted to have my work files synced between my laptop, my home server and the server at work, so I can use them whenever I need. Doing a manual copy after using them is an option, but it’s the worst one. A better alternative is to copy the folder I use every day, but this is time consuming and I usually don’t have the time to wait for the copy to be done. So I decided to use rsync which has many advantages. First of all I can use it in a script, so I don’t have to remember what files I modified. Also, rsync keeps my folder(s) synced by copying only the modified files.
The first thing to do is to be sure that rsync over ssh works on your station.
$ rsync -avz -e ssh remoteuser@remotehost:/remote/dir /this/dir/
You will be asked for the remoteuser’s password. If everything went OK you are ready for the next step.
For using rsync in scripts you will have to create a private/public pair of keys to allow a ’ssh’ connection without asking for a password.
ssh-keygen -t rsa
When prompted for a password, do not enter one. This will generate a password-less key called id_rsa and a public key called id_rsa.pub. Copy the public key to the remote host and add it to the authorized_keys:
scp id_rsa.pub remoteuser@remotehost:/home/remoteuser/local.pub
cat local.pub >> .ssh/authorized_keys
Now you will not be prompted for a password. To test it, just run:
ssh remotehost
So you have now rsync running and ssh password-less, it’s time to decide what you want to copy to the remote host.
I use to keep all my files in one folder (with subfolders) so my examples will be pretty simple, rsync will copy all the files and folders from the source folder. But i don’t want to copy some .iso files I have there or the backup files created by joe (files ending with ~). So I created an exclude file, which I’ll pass to rsync.
My backup.exclude is something like:
- *~
-*iso
You can add here any file or pattern you want to be excluded.
Now the backup script:
#!/bin/sh
EXCLUDE=/home/pctips/backup.exclude
LOGFILE=/home/pctips/backup.log
RSYNC=/usr/bin/rsync
ARGS=”-v -r –delete -e ssh –exclude-from=$EXCLUDE”
SOURCE=”/home/pctips/work”
DEST=”user@10.1.2.39:/home/user/backup”$RSYNC ${ARGS} $SOURCE $DEST > $LOGFILE
cat $LOGFILE | mail -s backup pctips@pc-tips.net
With this, when I like to save my work from laptop to my home server. You can modify it so you can specify the remote host and/or the remote user. You can also create a cron job to have this script run automatically.
After I run this script I have the same folder tree on the server as on the laptop, which can be very useful in case of data loss
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.





July 1st, 2008 at 4:51 am
[...] siguiente tutorial nos muestra como usar esta interesante utilidad en [...]
March 9th, 2009 at 3:54 pm
Hay,I like your posting friend and i would like to link exchange with your blog if you want.this is my blog Car Body Kit And also dont forget to come.i will happy if you come .Good bye friend
March 22nd, 2009 at 11:26 pm
It seems like something is missing, no?
March 24th, 2009 at 1:44 pm
can you be more specific?