rsync to copy difference

Rsync to copy difference of source and destination.

We use rsync command to copy files from source to destination. Rsync command sync modified/update files and newly created files on destination server.

But user need copy modified files and newly created files on source as compared to destination to be copied in separate folder.

I created 3 folders in /home as r1 r2 and r3. Then I touched 5 files named as 1 2 3 4 5 in r1 and r2 and r3 are empty.

Here source=/home/r1 destination=/home/r2 and difference=/home/r3

Output of ls -l for r1 r2 and r3 as given below:

[root@midnight home]# ls -l r1
total 0
-rw-r–r– 1 root root 0 Sep 17 01:30 1
-rw-r–r– 1 root root 0 Sep 17 01:30 2
-rw-r–r– 1 root root 0 Sep 17 01:30 3
-rw-r–r– 1 root root 0 Sep 17 01:30 4
-rw-r–r– 1 root root 0 Sep 17 01:30 5

[root@midnight home]# ls -l r2
total 0

[root@midnight home]# ls -l r3
total 0

Now. I will copy r1 to r2 and paste output.

rsync -va /home/r1/ /home/r2/

[root@midnight home]# rsync -va /home/r1/ /home/r2/
sending incremental file list
./
1
2
3
4
5

sent 306 bytes received 114 bytes 840.00 bytes/sec
total size is 0 speedup is 0.00

List all files from r2.

[root@midnight home]# ls -lt r2
total 0
-rw-r–r– 1 root root 0 Sep 17 01:30 1
-rw-r–r– 1 root root 0 Sep 17 01:30 2
-rw-r–r– 1 root root 0 Sep 17 01:30 3
-rw-r–r– 1 root root 0 Sep 17 01:30 4
-rw-r–r– 1 root root 0 Sep 17 01:30 5

 

Now let’s modify /home/r1/1 file and create two files as 6 and 7. And we need rsync to copy this modified file named as 1 and newly created file 6 & 7 to /home/r3 folder.

[root@midnight home]# rsync -aHxv –progress –compare-dest=/home/r2/ /home/r1/ /home/r3/
sending incremental file list
./
1
5 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=6/8)
6
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=1/8)
7
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=0/8)

sent 285 bytes received 81 bytes 732.00 bytes/sec
total size is 5 speedup is 0.01

Above command copiedĀ  modified and newly created files to folder r3

[root@midnight home]# ls -l /home/r3
total 4
-rw-r–r– 1 root root 5 Sep 17 01:36 1
-rw-r–r– 1 root root 0 Sep 17 01:36 6
-rw-r–r– 1 root root 0 Sep 17 01:36 7