Monday, January 21, 2013

Rsync with filter for specific subdirectory

1 comments
I have to ensure a backup of our document folders. The structure is
/DOC/company1/
/DOC/company2/
...
/DOC/companyN/

In each /DOC/companyN/ folder, we had some files and folders :-
test
storage
abc.php
user_privileges
conf

I have to ensure backups for storage and user_privileges folder of each company.

And to achive this, I have creared the following rsync command :-
 rsync -az  --include='/*/'  --include='*/storage/***'  --include='*/user_privileges/***'  --exclude='*'   /DOC/*    10.0.0.1:/BACKUP/DOCS

 I used the following options :-

 a : for archive mode
 z : for compression

Now, the filter rules  are
 include='/*/' : this includes all sub-directories in /DOC
 include='*/storage/***' : this includes all storage directories in /DOC/companyN
 include='*/user_privileges/***' : this includes all user_privileges directories in /DOC/companyN
 exclude='*' : use it at last to exclude all other files and directories

And its working now. However, I found a problem with this command where it send any directory from /DOC which does not have either storage  or user_privileges directory.
For now, we accept and release it as there is a very little chance to have any directory in /DOC which does not have any storage or user_privileges directory.

One Response so far

  1. Thank you for this! I wasted two hours of my life trying to figure out rsync's ridiculously unintuitive include/exclude rules. Your post solved the problem in 5 minutes. Thank you!!!

Leave a Reply

Labels