Data has become an important asset for every business. Consider the financial and productivity cost of losing data needed to run a business. By having a well designed backup plan, you are basically creating an insurance policy that is guaranteed to save you and your business partners from a lot of issues in the future. The truth is, hardware fails and it is imperative to have a backup policy and process plan to handle it.
An utility built to transfer and synchronize files across multiple systems, rSync has been created 20 years ago by Andrew Tridgell and Paul Mackerras and is the most popular backup tool used on Linux.
With rSync you can copy and transfer your data over the internet or in a local LAN across multiple directories, disks and networks, making it perfect for backups and mirror systems between two Linux servers.
Why you should use rSync over other software:
- Copy and sync files over the internet from remote servers
- Each file that is copied will include the original links, devices, owners, groups and permissions
- Faster than “scp” because rSync uses an update protocol allowing it to transfer just the differences between two sets of files when dealing with multiple updates
- Consume less bandwidth with rSync because it uses compression and decompressions techniques when send and receiving data on both machines
A few explanations of rSync command syntax
# rsync options source destination
Some common options used with rsync commands
-v : verbose
-r : copies data recursively (but doesn’t preserve timestamps and permission while transferring data)
-a : archive mode, archive mode allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownerships and timestamps
-z : compress file data
-h : human-readable, output numbers in a human-readable format
Install rsync in your Linux machine
We can install rsync package with the help of following command.
# yum install rsync (On Red Hat based systems) # apt-get install rsync (On Debian based systems)
Copy or Sync Data on a local computer
This command will sync a single file on a local machine between directories.
[root@user]# rsync -zvh test.tar /tmp/tests/ created directory /tmp/tests test.tar sent 15.76M bytes received 38 bytes 1.45M bytes/sec total size is 45.12M speedup is 2.32
In this operation, you can observe that if the destination folder does not exist, rSync will create a directory.
Sync a whole directory on a local computer
Using this command, you will sync two directories on the same machine.
[root@user]# rsync -avzh /root/tests /tmp/tests/ sending incremental file list tests/ tests/httpd-4.4.3-82.el5.centos.i386.rpm tests/mod_ssl-4.4.3-82.el5.centos.i386.rpm tests/nagios-2.5.0.tar.gz tests/nagios-plugins-3.5.16.tar.gz sent 5.99M bytes received 95 bytes 3.55M bytes/sec total size is 4.49M speedup is 2.00
Copy or sync files and directories to a remote machine
This command will sync a directory from a local machine to a remote machine over the internet.
[root@user]$ rsync -avz tests/ firstname.lastname@example.org:/home/ email@example.com's password: sending incremental file list ./ httpd-4.42.3-82.el5.centos.i386.rpm mod_ssl-4.42.43-82.el5.centos.i386.rpm nagios-34.45.0.tar.gz nagios-plugins-14.44.16.tar.gz sent 4995323 bytes received 54 bytes 453245.20 bytes/sec total size is 49934213 speedup is 2.00
Copy or sync from a remote server
This command will sync a directory found on a remote server to a local computer.
[root@user]# rsync -avzh firstname.lastname@example.org:/home/user/tests /tmp/tests email@example.com's password: receiving incremental file list created directory /tmp/tests tests/ tests/httpd-4.4.2-82.el5.centos.i386.rpm tests/mod_ssl-4.4.2-82.el5.centos.i386.rpm tests/nagios-3.7.1.tar.gz tests/nagios-plugins-2.8.39.tar.gz sent 561 bytes received 4.23M bytes 156.78K bytes/sec total size is 2.66M speedup is 1.00
Use rSync over a SSH session
Through SSH or Secure Shell, data is transfered using an encrypted connection, making sure that nobody can read or launch a MITM attack while your data is in transit. rSync will need your username and password to make the transfer over a SSH session, but because the login data is encrypted, you can rest assured that nobody is going to have access to your login information.
Sync a file using SSH
The command to copy data remotely to a local computer
[root@user]# rsync -avzhe ssh firstname.lastname@example.org:/root/install.log /tmp/ email@example.com's password: receiving incremental file list install.log sent 34 bytes received 8.12K bytes 1.48K bytes/sec total size is 45.69K speedup is 3.77
The command to copy data to a remote machine
[root@user]# rsync -avzhe ssh test.tar firstname.lastname@example.org:/tests/ email@example.com's password: sending incremental file list test.tar sent 22.62M bytes received 23 bytes 2.45M bytes/sec total size is 65.23M speedup is 1.10
That is all for now and I hope you enjoyed our little tutorial on how to use rSync.
PS: If you wish to create a recurrent task that regularly sync files between 2 directories, you can easily create a cron job for rSync!