RSYNC with different directory structures

rsyncsource

I'm trying to accomplish the below with RSYNC but can't quite figure out the syntax. My source contains a folder for each user, then some subfolders under Logs. I need to capture everything under the numeric folder with the sync.

SOURCE DIRECTORY STRUCTURE:  
/STORAGE/user1/db/Logs/151251/  
/STORAGE/user1/db/Logs/156123/  
/STORAGE/user1/db/Logs/117722/  
/STORAGE/user2/db/Logs/178438/  
/STORAGE/user2/db/Logs/161265/  

PREFERRED DESTINATION DIRECTORY STRUCTURE  
/LOGS/user1/151251/  
/LOGS/user1/156123/  
/LOGS/user1/117722/  
/LOGS/user2/178438/  
/LOGS/user2/161265

The command I've tried is:

rsync -azvr user@server.com:/STORAGE/*/db/Logs/*/ /LOGS/

This succeeds in copying all the files from all the numeric subfolders but I need to create the directory structure above (/LOGS/user/uniqueid/individual files).

Is there a way to accomplish this?

Best Answer

You can use rsync filters to specify which subpaths you want transferred:

rsync -asvr --include '*/' --include 'Logs/***' --exclude '*' user@server.com:/STORAGE/ /LOGS/

This will create all the source directories on the destination (but not containing any files). To not do this you need to explicitly include all parent directories:

rsync -asvr --include '/STORAGE/' --include '/STORAGE/*/' --include '/STORAGE/*/db/' --include '/STORAGE/*/db/Logs/***' --exclude '*' user@server.com:/STORAGE/ /LOGS/

Edit: To exclude files with a certain extension, add an --exclude pattern at the front:

rsync -asvr --exclude '*.bdb' --include ...
Related Topic