Home » How to rsync multiple source folders

How to rsync multiple source folders


You can pass multiple source arguments.

rsync -a /etc/fstab /home/user/download bkp

This creates bkp/fstab and bkp/download, like the separate commands you gave. It may be desirable to preserve the source structure instead. To do this, use / as the source and use include-exclude rules to specify which files to copy. There are two ways to do this:

  • Explicitly include each file as well as each directory component leading to it, with /*** at the end of directories when you want to copy the whole directory tree:

    rsync -a 
        --include=/etc --include=/etc/fstab 
        --include=/home --include=/home/user --include="/home/user/download/***" 
        --exclude="*" / bkp
  • Include all top-level directories with /*/ (so that rsync will traverse /etc and /home when looking for files to copy) and second-level directories with /*/*/ (for /home/user), but strip away directories in which no file gets copied. This is more convenient because you don’t have to list parents explicitly. You could even use --prune-empty-dirs --include="*/" instead of counting the number of levels, but this is impractical here as rsync would traverse the whole filesystem to explore directories even though none of the include rules can match anything outside /etc and /home/user/download.

    rsync -a --prune-empty-dirs 
        --include="/*/" --include="/*/*/" 
        --exclude="*" / bkp

I really like Gilles’ answer, however, I’d like to add that in my view the requirement to sync multiple folders while preserving the directory structure is best met by passing multiple source arguments in conjunction with the --relative option.

In this case, we could have something as follows:

rsync -aR /etc/fstab /home/user/download bkp

which would result in bkp/etc/fstab and bkp/home/user/download.

The best part about this is that (I believe since rsync v. 2.6.7) we can in essence control how much of the directory structure we want to replicate at the receiver.
(See the documentation on the --relative option here)

So e.g. if we did this

rsync -aR /home/./user1/download /home/./user2/download bkp

we would end up with bkp/user1/download and bkp/user2/download.

This also works – curly braces, containing comma separated list of sources.

rsync -vap --progress --stats root@server:{/etc,/root/backups,/home/ultralazer} /mnt/bigdrive

Somewhat similar to what happens when you invoke curly braces sytnax with cp and certain other utilities:

cp -vr /etc /root/backups /home/{ultralazer,zerocool} /mnt/bigdrive

Related Solutions

how do i get particular key value from string in swift4

It's JSON array of dictionary you can get all the value of particular key by use map with array like below. let arrayValues = mainArray.map{$0["District_NameEng"] as! String} print (arrayValues) Below is the sample code to parse your json string // I've escaped...

Popup text box in HTML with javascript

Does this meet your requirements? function showPopup() { document.getElementById('2').style.display = "block"; } function syncValueWith2() { document.getElementById('2').value = document.getElementById('1').value; } function syncValueWith1() {...

C++ Fizz Buzz alternative version [closed]

You should be able to do this equally well with either for-loop or while-loop. However, that is not why your program is failing. As mentioned earlier by @EvilTeach, your program is basically accepting input from stdin(via cin) outside the braces({}) in main()...

Output is different than I expected [closed]

shopping_cart = ['apples', 'oranges', 'banana', 'kiwi', 'avocado', 'peaches'] s_list = list(shopping_cart) # shopping_cart is already list so you can ignore it. if 'mango' in s_list: print('Done') else: print('Not Done') i Think this is what you're trying to...

How does one make jsdoc actually output docs?

I have fixed it by not using export infront of classes, instead exporting them at the end of the file. like this: import { Errors } from "../errors.js"; import { Models } from "./models.js"; /** * Several paragraphs of text that explain this class * * @class *...

How can I run a jsp program? [closed]

You need to have a JSP capable web-server or application server. Check Apache Tomcat project. And follow the documentation that phoenix provided. Check this link. It gives more info http://www.jsptut.com/Getfamiliar.jsp The first result of a google search....

Why doesn’t the “extern” keyword work in the same file?

extern int x; tells the compiler: "I will provide you a int x in some other compilation unit". Please expect to find it at link time. So, you need another file: example8b.cpp int x = 0; int y = 0; int z = 0; and you need to link both files in your project. But...

Pygame event handling key events

this answer is partly copied from programarcadegames.com, if you want to make a game with pygame, you should have a look at the courses. [assuming you already have a main loop, if not, start the course mentioned above from scratch:] outside the main loop, set...

C# Creating a Grading Program

You are looking for else if construction: //TODO: out the right thresholds static string GradeMe(int average) { if (average >= 90) // 90+ return "Grade A"; else if (average >= 80) // [80..90) return "Grade B"; else if (average >= 70) // [70..80) return...

How to know the do while loop in C programming [closed]

Well: Hope the following helps you. Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in C programming language checks its condition at the bottom of the loop A do...while loop is similar to a while loop,...

What is ambient occlusion?

Ambient occlusion is a method to approximate how bright light should be shining on any specific part of a surface, based on the light and it's environment. This is used to add realism. Wikipedia has a nice paragraph that explains what is done. Ambient occlusion...

Can Anyone tell me how can i solve this Exception [closed]

Arrays are zero-indexed. Let's assume for the sake of argument that k is of length 10. What would happen here?: int x; char ch[]= k.toCharArray(); //Convert String into character char p[]=k.toCharArray(); //Convert String into character x=k.length(); ch is now...