This tutorial will help you to sync data between two (web) servers with “Rsync“ for example to minimize/eliminate downtime/data loss of your web servers.
Rsync (Remote Sync) is a command tool to copy and synchronize data locally across directories, across disks and remotely across systems to perform data backups and mirroring between two Linux systems.
The main advantages of Rsync is:
- Speed: Initially copies the whole content between local and remote system. Next time, only those bytes and blocks of data that have changed.
- Rsync uses compression and decompression method while transferring data which consumes less bandwidth.
- Rsync has the ability to check and delete those files and directories at backup server that have been deleted from the main web server.
- It takes care of permissions, ownerships and special attributes while copying data remotely.
- It also supports SSH protocol to transfer data in an encrypted manner so that you will be assured that all data is safe.
Install Rsync Tool
Rysync package must be installed on local system and on remote system as well.
yum install rsync
Rsysncd listens the defaults TCP port 873
RSync Examples
Syntax:
rsysnc [options] source destination
Synchronizes two directory on local computer |
|
Synchronize local host to remote host |
|
Synchronize remote host to local host |
|
Synchronize remote to local host |
|
Synchronize remote host to local host with SSH |
|
Show Progress While Transferring Data with rsync |
|
Set Bandwidth Limit |
|
RSync options
-a, –archive | archive mode it preserves owner and groups, it preserve timestamp, symbolic links, permission, recursive mode. |
-r, –recursive | recurse into directories |
-p, –perms | preserve permissions |
-z, –compress | compress file data during the transfer |
-n, –dry-run | show what would have been transferred |
-v, –verbose | Verbose |
-e ssh | Rsync Over SSH, perform data transfer in secured connection with encryption |
Schedule Cron To Automate RSync
As we are going to use rsync with SSH protocol, ssh will be asking for authentication and if we won’t provide a password to cron it will not work. In order to work cron smoothly, it’s required to first setup passwordless ssh logins for rsync.
To setup a cron, open crontab file with the following command.
crontab –e
Below an example to run rsync every 5 minutes to sync the data between two web servers.
*/5 * * * * rsync -avzhe ssh remoteuser@remoteserver:/var/www/ /var/www/