Home » How to rsync multiple source folders

How to rsync multiple source folders

Solutons:


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="/*/*/" 
        --include=/etc/fstab 
        --include="/home/user/download/***" 
        --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

Java simple accounting program [closed]

There's a spelling mistake (an extra 'e' on the input parameter annualInterest*e*Rate): public void setAnnualInterestRate(double annualInteresteRate){ this.annualInterestRate = annualInterestRate; } Therefore you are setting the this.annualInterestRate to...

pc and mobile site code using user agent

You could do something like this through media queries <link rel="stylesheet" media="screen and (min-device-width: 1024px)" href="https://stackoverflow.com/questions/21937880/pc.css" /> <link rel="stylesheet" media="screen and (max-device-width:...

How many CPU ticks does it take to store a single None? [closed]

In short: In this case, since X does not implement Drop, storing None is done in a single instruction. The number of CPU cycles depends heavily on the exact hardware and on caching effects. But assuming a modern x86-64 CPU and assuming the memory is in L1 cache...

sql aggregate and split [closed]

This uses STRING_AGG to aggregate the strings into one long one, and then a Tally Table to split into the new rows. It is assumed you have a column to order by. if you do not, you cannot achieve what you are after without one as data in a table is stored in an...

What is “-bash: !”: event not found”

You can turn off history substitution using set +H. ! is a special character to bash, it is used to refer to previous commands; eg, !rm will recall and execute the last command that began with the string "rm", and !rm:p will recall but not execute the last...

Visualize a sparse matrix using Python Turtle graphics

What does the problem mean by (rows-1, columns-1)? This is tied up with your mysterious m variable and the order() function you left undefined. Let's proceed anyway. We can see from the matrix() function we're dealing with a square matrix but let's not even...

What does an “extra” semicolon means? [duplicate]

A for loop is often written int i; for (i = 0; i < 6; i++) { ... } Before the first semicolon, there is code that is run once, before the loop starts. By omitting this, you just have nothing happen before the loop starts. Between the semicolons, there is the...

Python: Compare lists, perform operation, create new list

I think the downvotes are justified because you did not provide any code at all to show us what you tried so far, so that we know where you are stuck with your code. Also i think the way you wrote the requirements is a bit confusing (0eth probably means nth...

Python put multiple line array into a single line

I think you did not the best job at describing the problem, it is not really clear why you have such files (i guess most people were assuming you have an existing array in python) containing an array definition as if it was written for a specific language with...

Sort array contains numeric string using c# array

You need give an order (actually a equivalence relation) to be able to sort. The order on characters is usually a,b,c,... and the order usually given on words such as 'one' is called lexicographic order. However you want to sort by the meaning behind, its...

Sort (hex) colors by Hue [closed]

In this updated example also we don't care about hash, we'll just deal with an arrays of hex colors: require 'paint' #intantiate web_colors from gist and shuffle them eval(`curl...

read file string and store in uint8_t array in c [closed]

Is that what you wanted? (to test give binary 01 combination as an first argument) #include <stdio.h> #include <stdint.h> uint8_t charToBin(char c) { switch(c) { case '0': return 0; case '1': return 1; } return 0; } uint8_t CstringToU8(const char *...

my JavaScript using jQuery is not working [closed]

You didn't include jQuery in your code: <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type="text/javascript" src="https://stackoverflow.com/questions/43397734/temp.js"></script> Make...

How to check if a Date falls in a certain week or Quarter [closed]

I would say do these steps, 1 - figure out first and last day of the week Get current week start and end date in Java - (MONDAY TO SUNDAY) 2 - check if the date falls in between that range or not Java: how do I check if a Date is within a certain range? - if...

How to add two object together

You should use a numeric data type for arithmetic operations (not Object). With out seeing the code in add() my recommendation is to store the total price in a double primitive. double price =0; for(int x = 0; x < rprice.size(); x++) { //you may need to...