Tuesday, September 18, 2012

Demonstrating Syncing two directories using rsync utility


Assumption:

 -    Directory “source” need to be sync with “destination” directory
-    For ease of demonstration, destination files I also on same box where source directory is .


=> Initially destination directory is empty. Source directory have 6 files

LocalSystem# ll source destination/
destination/:
total 0

source:
total 0
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file1
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file2
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file3
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file4
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file5
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file6


=> Here is how we can sync destination directory with source directory

LocalSystem# rsync -avz /tmp/source/      RemotSystem:/tmp/destination/

LocalSystem# ll source destination
destination:
total 0
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file1
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file2
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file3
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file4
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file5
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file6

source:
total 0
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file1
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file2
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file3
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file4
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file5
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file6


=> Some of the files have been deleted from source directory. rsync will delete them from destination on next sync.

LocalSystem#  rsync -avz --delete /tmp/source/      RemotSystem:/tmp/destination/
root@     RemotSystem's password:
sending incremental file list
./
deleting file6
deleting file4
deleting file2

sent 65 bytes  received 15 bytes  14.55 bytes/sec
total size is 0  speedup is 0.00
LocalSystem# ll source destination/
destination/:
total 0
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file1
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file3
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file5

source:
total 0
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file1
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file3
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file5


=> Now some new files are created and some more files deleted, rsync will copy new file to destination directory and delete already deleted file from source.

LocalSystem#  rsync -avz --delete /tmp/source/      RemotSystem:/tmp/destination/
root@     RemotSystem's password:
sending incremental file list
./
deleting file5
deleting file3
file7
file8

sent 145 bytes  received 53 bytes  44.00 bytes/sec
total size is 0  speedup is 0.00
LocalSystem# ll source destination/
destination/:
total 0
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file1
-rw-r--r--. 1 root root 0 2012-09-18 09:29 file7
-rw-r--r--. 1 root root 0 2012-09-18 09:29 file8

source:
total 0
-rw-r--r--. 1 root root 0 2012-09-18 09:19 file1
-rw-r--r--. 1 root root 0 2012-09-18 09:29 file7
-rw-r--r--. 1 root root 0 2012-09-18 09:29 file8

No comments:

Post a Comment