Backup Files in Linux using Rsync Command Line

linux rsync  stands for remote sync which was written by Andrew Tridgell and Paul Mackerras back in 1996. It’s one of the most used and power ‘tools’ in the UNIX world and almost a standard for syncing data. Most Linux distributions have rsync pre-installed, but if it’s not there you can install the ‘rsync’ package for your distribution.

What is Rsync

Rsync is an extremely powerful tool and does more than just make copies of your files on your system. it also synchronize files on two directories on the same Server; or synchronize files and/or directories on two different systems on the same network.

Advantages and features of Rsync command

  • rsync faster than scp (Secure Copy) ,because rsync uses remote-update protocol which allows to transfer just the differences between two sets of files. First time rysnc copies the whole content of a file or a directory from source to destination then the next time rsync copies only the changed blocks and bytes to the destination.
  • Rsync consumes less bandwidth because it uses compression and decompression method while sending and receiving data both ends.
  • Rsync Supports copying links, devices, owners, groups and permissions.
  • its support remote copy.

Install rsync in Linux

For Red Hat and Centos
					yum install rsync
On Debian based systems
					apt-get install rsync

The basic syntax of rsync

rsync option Source-Directory Destination-Directory

Common linux rsync options command

  • -v : verbose
  • -r : copies data recursively (but don’t preserve timestamps and permission while transferring data).
  • -a : archive mode, archive mode allows copying files recursively .
  • -z : Saves bandwidth over the network, by compress file data
  • -h : human-readable, output numbers in a human-readable format
  • -P : for partial progress.
  • -e : To specify protocol name you want to use,for example if you are going to use the rsync with ssh protocol to backup or copy data remotely.

Use of –– delete Option :

If you have two sets of directories synced with each other. AND some files or directories deleted ,and you need to be sure that those files or folders will be delete on the destination as well? You need to use the ‘- – delete’ option which will take care of such cases.The command becomes:
					rsync -a –-delete Source Destination

How to sync directories (remotely) over network

ssh protocol help you to backup remotely. Use the following syntax to sync a remote directory with a local directory: Please note that you have to choose option -e to use ssh protocol.
					rsync -avzP ––delete -e ssh user@remote-server:source-directory /local-server-destination_directory/

Automate rsync backup

Now the best benefit and powerful part is to automate backup.
You can achieve this feature by creating cron job on Linux OS.
Run ‘crontab -e’ to create cron jobs. It will open an empty file where you can configure the command that you want to run at a desired time.
The format of crontab is quite simple; just pass the five fields followed by your rsync command:

m h dm m dw command

the following will run corn rsync every day at 6.30 PM:

					30 18 * * * rsync your-source your-Destination
Share on facebook
Share on twitter
Share on linkedin
Share on telegram
Share on whatsapp

Related Posts