Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
953 views
in Technique[技术] by (71.8m points)

centos - How to use Rsync to copy only specific subdirectories (same names in several directories)

I have such directories structure on server 1:

  • data
    • company1
      • unique_folder1
      • other_folder
      • ...
    • company2
      • unique_folder1
      • ...
    • ...

And I want duplicate this folder structure on server 2, but copy only directories/subdirectories of unique_folder1. I.e. as result must be:

  • data
    • company1
      • unique_folder1
    • company2
      • unique_folder1
    • ...

I know that rsync is very good for this. I've tried 'include/exclude' options without success.

E.g. I've tried:

rsync -avzn --list-only --include '*/unique_folder1/**' --exclude '*' -e ssh [email protected]:/path/to/old/data/ /path/to/new/data/

But, as result, I don't see any files/directories:

receiving file list ... done
sent 43 bytes  received 21 bytes  42.67 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)

What's wrong? Ideas?


Additional information: I have sudo access to both servers. One idea I have - is to use find command and cpio together to copy to new directory with content I need and after that use Rsync. But this is very slow, there are a lot of files, etc.

question from:https://stackoverflow.com/questions/15687755/how-to-use-rsync-to-copy-only-specific-subdirectories-same-names-in-several-dir

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've found the reason. As for me - it wasn't clear that Rsync works in this way.
So correct command (for company1 directory only) must be:

rsync -avzn --list-only --include 'company1/' --include 'company1/unique_folder1/***' --exclude '*' -e ssh [email protected]:/path/to/old/data/ /path/to/new/data

I.e. we need include each parent company directory. And of course we cannot write manually all these company directories in the command line, so we save the list into the file and use it.


Final things we need to do:

1.Generate include file on server 1, so its content will be (I've used ls and awk):

+ company1/  
+ company1/unique_folder1/***  
...  
+ companyN/  
+ companyN/unique_folder1/***  

2.Copy include.txt to server 2 and use such command:

rsync -avzn                                        
      --list-only                                  
      --include-from '/path/to/new/include.txt'    
      --exclude '*'                                
      -e ssh [email protected]:/path/to/old/data/    
      /path/to/new/data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...