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

后端 未结 4 1520
情话喂你
情话喂你 2021-01-31 08:36

I have such directories structure on server 1:

  • data
    • company1
      • unique_folder1
      • other_folder
      • ...
    • company2
4条回答
  •  北恋
    北恋 (楼主)
    2021-01-31 09:23

    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 user@server.com:/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 user@server.com:/path/to/old/data/    \
          /path/to/new/data
    

提交回复
热议问题