2

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

1

2 Answers 2

2

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/**' \
3
  • is there a way to do it in one comand line so i can set it in cronjob in the server ?
    – iLinux85
    May 27, 2012 at 12:05
  • i tried the command but the command transfer the directory without the files within the directories
    – iLinux85
    May 27, 2012 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, 2012 at 15:23
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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