How to backup/copy/synchronise a full folder lying on a remote server, onto another (local/remote) machine easily using rsync in linux
”rsync” is an open source utility in Linux/Unix primarily used for synchronising files/directories over the networks. You can also use it to backup/copy folders between machines. It is particularly made use of to while administering servers connected to the internet. The term “rsync” itself refers to “remote-synchronise”.
Rsync has got a number of options to control the file synchronisation operation and is a very much flexible command line utility. Rsync finds files that are to be transferred using a quick check algorithm, identifying changed files by their size or their last modified time.
While transferring data over the internet it is recommended that you use rsync over SSH as it allows a secure remote connection. The most popular rsync command option combinations are
- rsync --delete : delete the files that is not present on the sender's system
- rsync -v : Verbose (which gives detailed information)
-
rsync -e
: specify the ssh as remote shell - rsync -z : compress file data
- rsync -r : recurse into directories
- rsync -a : archive mode
Examples: Consider you want to copy the file backup.tar.gz to a server named example.server1.com The following command:
rsync -v -e ssh /home/user/backup.tar.gz [email protected]
will transfer the file backup.tar.gz located at /home/user/ to the server named example.server1.com.
If you want to transfer a file from server to your local system, use the following command:
rsync -v -e ssh [email protected] : ~/example.txt/tmp
Now how can we keep two folders in sync always? Just add a cron job that runs the rsync command to the do the sync operation as frequently as you want it to be.