Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to rsync specific directories and files with the same structure

for example :-

source/
source/110/var/lib/mysql
source/110/var/lib/mysql
source/110/var/lib/mysql
source/110/home
source/110/bin/
source/110/etc/
source/120/var/lib/mysql
source/120/var/lib/mysql
source/120/var/lib/mysql
source/120/home
source/120/bin/
source/120/etc/

how to rysnc mysql and home directories and the files included in the direcotry to the same destination folder and with the same structure to be like the following example :-

destination/
destination/110/var/lib/mysql
destination/110/home
destination/120/var/lib/mysql
destination/120/home

thanks

share|improve this question
    
See also this question: serverfault.com/questions/150269/… –  chutz May 27 '12 at 11:00

2 Answers 2

up vote 1 down vote accepted

You can do that with some fancy filters. The man page has a nice example of this, but with a recent rsync (>= 2.6.7) you can do something like:

rsync -a source/ destination/ \
    -f '+ /*/' \
    -f '+ /*/home/***' \
    -f '+ /*/var/' \
    -f '+ /*/var/lib/' \
    -f '+ /*/var/lib/mysql/***' \
    -f '- *'

In other words, you list which directories you want to include in your sync, and then exclude everything else. The *** wildcard is very useful but if your rsync is older, you will have to replace the + /*/home/*** filter with the following two:

    -f '+ /*/home/' \
    -f '+ /*/home/**' \
share|improve this answer
    
is there a way to do it in one comand line so i can set it in cronjob in the server ? –  iLinux85 May 27 '12 at 12:05
    
i tried the command but the command transfer the directory without the files within the directories –  iLinux85 May 27 '12 at 12:10
    
Sorry, I missed a leading slash in the filters. I already corrected my answer and even tried it out. Without the leading slash '+ */' was matching all directories and that is not what you want. –  chutz May 27 '12 at 15:23

Here's an alternative to using filters:

cd source && \
rsync -aR  110/var/lib/mysql  110/home  120/var/lib/mysql  120/home  destination

That is, you pass all the source directories as arguments to rsync, followed by the destination directory, all in one command. You must be certain to use the -R option in this case, so that your source directory paths are preserved in the destination.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.