Monday, January 21, 2013

Rsync with filter for specific subdirectory

I have to ensure a backup of our document folders. The structure is

In each /DOC/companyN/ folder, we had some files and folders :-

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/*

 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.

Leave a Reply