Sync directories between two servers using rsync

How to

Rsync is free file copying tool. It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon. Rsync finds files that need to be transferred using a quick check that have changed in size or in last-modified time. Any changes in the other preserved attributes (as requested by options) are made on the destination file directly when the quick check indicates that the file’s data does not need to be updated.

Here is small example how to sync /test directory on system1 with remote system2.

system1:/test # ll
total 36
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test1
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test2
<span style="color: #d42f3c;">drwxr-xr-x 2 root root 4096 Aug 15 12:37 test3
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test4
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test5</span>
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test6
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test7
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test8
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test9
system1:/test #

As we can see we are missing directories tets3 – test5 on system2 which are present on system1.

system2:/test # ll
total 24
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test1
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test2
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test6
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test7
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test8
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test9
system2:/test #

For syncing those two directories between two different servers we will use rsync as follow.

system1:/test # rsync -avz /test system2:/
sending incremental file list
test/
test/test3/
test/test3/filea
test/test3/fileb
test/test3/filec
test/test3/filed
test/test3/filee
test/test3/filef
test/test3/fileg
test/test4/
test/test4/filea
test/test4/fileb
test/test4/filec
test/test4/filed
test/test4/filee
test/test4/filef
test/test4/fileg
test/test5/
test/test5/filea
test/test5/fileb
test/test5/filec
test/test5/filed
test/test5/filee
test/test5/filef
test/test5/fileg

sent 1622 bytes  received 433 bytes  4110.00 bytes/sec
total size is 378  speedup is 0.18
system1:/test #

Now we can see the same directory structure on remote server as on local.

system2:~ # ll /test
total 36
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test1
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test2
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test3
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test4
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test5
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test6
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test7
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test8
drwxr-xr-x 2 root root 4096 Aug 15 12:37 test9
system2:~ #

some usefull options:

  • -v, –verbose increase verbosity
  • -c, –checksum skip based on checksum, not mod-time & size
  • -a, –archive archive mode
  • -e, –rsh=COMMAND specify the remote shell to use
  • -z, –compress compress file data during the transfer

Leave a Reply