Pelicanux

Just A Few Random Words

Rsync

I admit it, I didn’t spend too much time to find out the title. Rsync is such a powerful tool, I had to write a complete article on it anyway.

Any reasonable UNIX sysadmin must know this tool, but most of documentation one can find over the Internet is its man. Well, rather rough to handle regarding the complexity of the beast.

Of course, rsync by default listens on TCP port 873. And the communication is unencrypted, which sucks. But fortunately, combining it with SSH provides a SysAdmin his (her?) handyman. Let’s go :O

Synchronization over SSH

  • Connection is granted by a tagged SSH key: The SSH-key is tagged by a forced-command launched if SSH connection succeeds
1
from="a.b.c.d",command="rsync --server --daemon --config rsync.conf ." ssh-dss ....
  • And here is the rsync server configuration file:
1
2
3
4
5
6
7
8
9
uid = 0  # root uid
gid = 0  # root uid
use chroot = true
read only = true   # 
hosts allow = a.b.c.d # Client IP address (SSH authentication lets the access to SSH server, here is extra-protection for the rsync server)
hosts deny = 0.0.0.0  # Others IP addresses are denied

[$module]
  path = $PATH_TO_FOLDER
  • On client side:
1
2
3
4
5
rsync \
   --delete \  # Delete on destination files which does not exists on source
   -avz \      # -z:compress data during transfer; -a:archive; -v:verbose
   -e "ssh -i ~$USER/.ssh/$PRIVATE_KEY" \
   ${server}::${module} ${CLIENT_SIDE_DESTINATION}
  • To upload, one must invert ${server}::${module} and ${CLIENT_SIDE_DESTINATION} and set read only = false on rsync.conf

Statistics

1
2
3
4
5
rsync --archive --delete --stats --verbose from to | \
 awk 'BEGIN {count = flag = 0} \
   /^deleting/ {count++; next} \
   /^Number of files: [0-9]*$/ {flag=1; print "Files deleted: " count} \
   {if (flag == 1) {print}}'

Trigger an action when a files is synchronized

1
2
3
4
5
[$module]
   path = $PATH_TO_FOLDER
   post-xfer exec triggered_when_files_synchronization_is_done.sh
   pre-xfer exec triggered_just_before_synchronization_starts.sh
  files-from = <(find . -mtime +1 -name "*.log" -print0)

In this example, only files modified more than 1 day ago which names are like *.log are synchronized

Include and Exclude

1
2
3
4
5
[$module]
   path = /
   include /etc/*** /usr/bin/tools/***
   exclude /*** /usr/ /usr/bin/***
   filter = - .*

We want to include folders /etc and /usr/bin/tools and no more, and avoid every hidden files

Conclusion

Ooof! This article was rather long, more than I would expect at start. I hope it would be of some use for some desperate reader! Last tip for this day: We can trigger a post-pre action once file is downloaded or uploaded; one can use inotify to trigger a rsync command once file appears on filesystem.