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 — rsync -avzh /root/rpmpkgs /tmp/backups/
  • Synchronize local host to remote host — rsync -avz /home/userx/ remoteuser@remoteserver:/share/rsysnctest/
  • Synchronize remote host to local host — rsync -avzh remoteuser@remoteserver:/home/userx /tmp/backup
  • Synchronize remote to local host — rsync -avz remoteuser@remoteserver:/share/rsysnctest/ /home/userx/
  • Synchronize remote host
    to local host with SSH
    — rsync -avzhe ssh remoteuser@remoteserver:/var/www/ /var/www
  • Show Progress While
    Transferring Data with rsync
    — rsync -avzhe ssh –progress /home/rpmpkgs remoteuser@remoteserver:/root/rpmpkgs
  • Set Bandwidth Limit — rsync –bwlimit=100 -avzhe ssh /var/lib/rpm/ remoteuser@remoteserver:/root/tmprpm/

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/