ip address : 103.43.x.34
ip address : 103.43.x.36
  1. Install rsync on master
    apt/yum -y install rsyncd
  2. Generate public key on master
    ssh-keygen -t rsa
  3. Copy public key from master to slave for login without password
    ssh-copy-id [email protected]
  4. Create directory and file for testing
    mkdir /var/www/html
    cd /var/www/html && touch file{1..100}.txt
    cd /var/www/html && fallocate -l 1G dummyfile #create 1G dummyfile
  5. Sync directory html from master to slave
    rysnc -avzh -e ssh /path/source-dir userhost:/path/dest-dir
    rsync -avzh -e ssh /var/www/html [email protected]:/var/www/

    sending incremental file list
    total size is 0 speedup is 0.00

      -a : archive file
      -v : verbose the process
      -z : compress file
      -e : archive file first
      -h : human readable

If there are changes to the source directory such as deleting of files, the files on the remote host will remain and will not be deleted. Therefore, you can use --delete option so that the files on the remote host are also deleted (sync). Below is the command:

rsync -avzh --delete -e ssh /var/www/html [email protected]:/var/www/

Then you can use cron to synchronize files between hosts automatically.

vim etc/crontab

15 * * * * root /usr/bin/rsync -avz --delete -e ssh /var/www/html [email protected]:/var/www/

systemctl restart crond
systemctl enable crond