rsync | Excluding a subdirectory/folder

We’ve Oracle RMAN backups copied to a remote server everyday & those chunks are once again copied to a removable media to “avoid”, disasters.

After a revamp of backup path, I needed to adjust the rsync command within a shell script to exclude a folder from the source. Misunderstanding the instructions, the new modifications made the shell script to copy the backup chunks twice on the removable media!

Let us see what I did initially that caused the duplication

[root@orcl DAILYBKP]# cd /u03
[root@orcl u03]# mkdir -p folder1/erp/RMAN/DAILYBKP
[root@orcl u03]# mkdir -p folder1/sf1
[root@orcl u03]# mkdir -p folder1/sf2
[root@orcl u03]# mkdir -p folder1/sf3
[root@orcl u03]# mkdir -p folder2
[root@orcl u03]# touch folder1/erp/RMAN/DAILYBKP/r1.txt
[root@orcl u03]# touch folder1/erp/RMAN/DAILYBKP/r2.txt
[root@orcl u03]# touch folder1/sf2/r1.txt
[root@orcl u03]# touch folder1/sf3/r1.txt
[root@orcl u03]# rsync -avz --exclude '/u03/folder1/erp' /u03/folder1 /u03/folder2
sending incremental file list
folder1/
folder1/erp/
folder1/erp/RMAN/
folder1/erp/RMAN/DAILYBKP/
folder1/erp/RMAN/DAILYBKP/r1.txt
folder1/erp/RMAN/DAILYBKP/r2.txt
folder1/sf1/
folder1/sf2/
folder1/sf2/r1.txt
folder1/sf3/
folder1/sf3/r1.txt

The above example demonstrates the mistake that I made while “excluding” a folder from the source. The correct method to exclude a subdirectory or folder was as below

[root@orcl u03]# rsync -avz --exclude 'erp/' /u03/folder1 /u03/folder2
sending incremental file list
folder1/
folder1/sf1/
folder1/sf2/
folder1/sf2/r1.txt
folder1/sf3/
folder1/sf3/r1.txt

sent 263 bytes  received 74 bytes  674.00 bytes/sec
total size is 0  speedup is 0.00
[root@orcl u03]# cd folder2
[root@orcl folder2]# ls -ltrh
total 4.0K
drwxr-xr-x 5 root root 4.0K Aug 12 09:39 folder1
[root@orcl folder2]# cd folder1
[root@orcl folder1]# ls -ltrh
total 12K
drwxr-xr-x 2 root root 4.0K Aug 12 09:39 sf1
drwxr-xr-x 2 root root 4.0K Aug 12 09:40 sf2
drwxr-xr-x 2 root root 4.0K Aug 12 09:41 sf3
[root@orcl folder1]#

All I needed was to just mention the name of the subfolder that I needed to exclude, not the whole path. I’ve limited exposure to Linux and much of the OS activities are initiated on demand. This causes some interesting situations like the one above & definitely helps to learn something new!