Tuesday, January 16, 2018

Running rsync as a daemon



rsync is a very flexible utility for  transferring and synchronizing files across computer systems. For example, when we want to run updates to the remote devices, we can just update some files in the rsync server and another remote devices will receive the updated files automatically.







Rsync has three ways to decide if a file is outdated:
  1. Compare size of files
  2. Compare timestamps
  3. Compare static checksums

So we can choose   --size-only, --ignore-times or ---checksum option in order to decide how to compare files.


Let's write little example

  • Let's create some directory and write some files in it. The idea is the following: if we add any files in the directory , remote devices will revieve that files (any devices who have rsync client)

mkdir /opt/{my_app_settings}/dev/
  •  Edit rsyncd.conf file and write configurations. I have created 2 configuration. But before editing the file imagine that some remote devices mught need to revieve files from different directory. We have already created directory named "dev" but we can also can create another directory. For example let's create another directory named "prod". So rsync client will decide which directory to use  - in the other words where to get files. So our configuration looks like that:

[dev] 
path = /opt/{my_app_settings}/dev 
read only = true
uid = root
gid = root


[prod]
path = /opt/{my_app_settings}/prod 
read only = true 
uid = root 
gid = root 

  • Run rsync daemon

 rsync --daemon 

  • Run rsync client in order to retrieve new files:

  rsync -rtv root@IP::dev {destionation_directory} 


Also we can create sheduler in order to execute auto synchronization.

 crontab -e 

If you want to run cron job every 1 minute, write the following  cron config




  • Cron examples


Every 1 minute     * * * * * 
Every 15 minutea   */15 * * * * 
Every 30 minutes   */30 * * * * 
Every 1 hour       0 * * * * 
Every 6 hours      0 */6 * * * 
Every 12 hours     0 */12 * * * 
Once a day         4 0 * * *
Once a week        4 0 * * 0 
Once a month       4 0 1 * * 


Now just verify your cron job

 crontab -l 
Results should be something like that: