rsync logoBackups are very important if you don't want to lose your data in case of a hard disk failure or generally if something goes wrong. Therefore it is a good idea to keep a copy of your data in another computer just to be sure. In some other cases you just want to keep the data you have in two places syncronized. However it is boring each time to select all the files and copy them to the new location. In this guide I will show how to use rsync to automate this process.

First of all you have to install rsync. This is easy if you use your distribution's package management system.

Now let's say that you want to synchronize a directory and its subdirectories from your computer to another hard disk drive. To do so you first have to mount the remote hard disk. I have described how to do this easilly using smb4k here . For this guide I will use /home/axel/ as the source directory and /mnt/data2/Backup/home as the destination directory.

So, open a terminal and type:

rsync -r -t -v --progress --delete -c -l -z /home/axel/ /mnt/data2/Backup/home/

Don't forget the / at the end of both directories.

This command in generall will synchronize the destination directory with the source one. Let me explain better what each option does.

-r copies also all subdirectories within main directory
-t preserves modification times
-v verbose mode, shows more information
--progress shows progress during transfer
--delete deletes files in destination which are not present in the source
-c compares files contents by checksum
-l copies symbolic links
-z compresses files for faster transfer
/home/axel/ source directory
/mnt/data2/Backup/home/ destination directory

If you want first to test what the result of the command will be you can also use the -n option which executes just a simulation.

If you want to preserve owner, group and permissions of your files you have to use 3 more options.

-o preserves owner
-g preserves group
-p preserves permissions

You can also exluce some subdirectories if you don't want them. To see the complete options list type man rsync.

If you find all those options complicated and you prefer the GUI way you can install grsync. grsync is a Graphical User Interface for rsync. Although it doen't support all of rsync capabilities, such as remote synchronization through ssh, it is pretty good for the job mentioned above. You can probably find it in your distribution's Package Management System as well. If you don't download it from here . This is how it looks like:

grsync

Next you can automate even more this progress. For example let's say you want to synchronize the above directories every day at noon. First you have to create the script which will be responsible for this job. Open a terminal and type:

gedit sync.sh

or

kwrite sync.sh

depending on whether you are using GNOME or KDE.

Paste the following lines in there:

#!/bin/sh
rsync -r -t -v --progress --delete -c -l -z /home/axel/ /mnt/data2/Backup/home/

Save and exit. Next you have to make this executable and test if it is running properly. (Don't forget the simulation -n option.)

chmod +x sync.sh
./sync.sh

If the result is ok it's time to put it in a cron job. You must have cron installed. Again use your PMS to get it. The default editor from crontab is vi. Therefore if you prefer nano or anything else change it like this:

export EDITOR=gedit

Next type:

crontab -e

and paste this line:

0 12 * * * /home/axel/sync.sh

This is the structure of the command:

minute hour day_of_the_month month day_of_the_week command

So in this way you will have a daily backup of your files at noon. As another example if you want to syncronize your files at 12 o'clock every 7 days type:

0 12 * * 7 /home/axel/sync.sh

Moreover you can synchronize your files on a remote computer on which you have SSH access. To do so simply type:

rsync -avz -e ssh remoteuser@remotehost:/remote/dir /local/dir/

Comments (9)

Subscribe to this comment's feed
rsync to a remote machine?
0
Hello;
Thanks for the info. very useful.
I was wondering how to rsync a remote machine?
Can you explain that please?

Thank You!
Matey , June 23, 2009
...
axel
Thank you Matey!

To rsync a remote machine you must have an ssh daemon running on the remote machine. Then the command would be something like this

rsync -avz -e ssh remoteuser@remotehost:/remote/dir /local/dir/
axel , June 25, 2009
Auto Backup
0
Dear sir,
I have So in this way you will have a daily backup of your files at noon.Networking Linux Server As another example if you want to syncronize your files at 18 o'clock every 7 days type:
Bharat singh , July 21, 2010
Backup server files on remote machine via FTP
0
Hi,

how could I use rsync in order to backup my files on a remote machine via FTP??

Thanks!
Michalis , May 05, 2011
...
axel
Hi Michalis, rsync doesn't work through FTP. You have to use another tool such as ftpsync or lftp
axel , July 24, 2011
...
0
Many thanks for the tips, Axel.
Also if you want to synchronize two machines over ssh, you need to install rsync on both machines, or you'll get a connection error.

Regards
birkof , November 16, 2011
...
axel
You are welcome birkof, thanks for the tip. All the times I've performed rsync over ssh rsync was already installed on the remote machine so it didn't occur to get this connection error. smilies/tongue.gif
axel , November 16, 2011
...
0
Could you tell me what's the name of the cursor you're using (or provide any other info that may help me find it)?
Paul , May 26, 2012
...
axel
Hi Paul. It's oxygen white theme of KDE.
axel , May 27, 2012

Write comment

smaller | bigger
security image
Write the displayed characters

busy

Linux

Login Form

Follow me on...

  • Facebook
  • Twitter
  • Google+: u/0/b/113039112812192417058/
  • Digg
  • Reddit: myguides
  • RSS Feed
  • e-mail

Member Login