Backups 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 computer in your LAN. 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 /home/axel/smb4k/CORE2DUO/Core2Duo(E)/backup/ as the destination directory. CORE2DUO is the name of the second computer and Core2Duo(E) is the hard disk I want to copy the files to. backup is the directory where all the files will be copied. The remote disk here is mounted under my home directory. As you can understand there is no difference if the remote computer has Linux or Windows instaled. Of course the procedure is the same if both directories are in the same computer.
So, open a terminal and type:
rsync -r -t -v --progress --delete -c -l -z /home/axel/ /home/axel/smb4k/CORE2DUO/Core2Duo(E)/backup/
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
/home/axel/smb4k/CORE2DUO/Core2Duo(E)/backup/ 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:

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:
nano sync.sh
and copy in there:
#!/bin/sh rsync -r -t -v --progress --delete -c -l -z /home/axel/ /home/axel/smb4k/CORE2DUO/Core2Duo(E)/backup/
Save (Ctrl+O) and exit (Ctrl+X). 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=nano
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_mont 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
Don't forget that the cron deamon should be running all the time for this command to be executed. Check if crond is running by typing:
/sbin/service crond status
Add this page to your favorite Social Bookmarking websites
Set as favorite
Email this
Hits: 29790
Trackback(0)
TrackBack URI for this entryComments (3)
Subscribe to this comment's feedrsync to a remote machine?
Hello;
Thanks for the info. very useful.
I was wondering how to rsync a remote machine?
Can you explain that please?
Thank You!
Thanks for the info. very useful.
I was wondering how to rsync a remote machine?
Can you explain that please?
Thank You!
...
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/
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/
Write comment