Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How can I specify specific files to include/exclude in grsync/rsync?

Currently, I use different operations to backup files from only specified folders. I want to do it now in 1 pass. I think I should be looking at the include/exclude options?

Suppose I have a folder structure like

/
  /inc1
    /inc1.1
    /...
  /inc2
    /...
  /exc1
  files in root here ... 

I want to only backup files/folders from inc1 & inc2 how can I do it?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

You can use --include and --exclude options or for a backup process you may want to use --include-from so you can list the folders you want to back up in a file.

For example your file may be called includes.txt and save in your home directory. It would contain

  inc1/
  inc2/

And the rsync command to back up folders inc1 and inc2 in your home directory would be

rsync $HOME --include-from=~/includes.txt /home/backup/

rsync is very flexible, you are best off consulting the man page first and if you get stuck asking a more specific question.

share|improve this answer
    
When testing rsync use options -nv which will show you what it will do without actually doing it. –  Richard Holloway Aug 9 '11 at 12:05

To include only specific directories, include them and their contents, then exclude *. If the directories aren't at the root, you need to include every directory leading to them. For example, to include only /source/inc1, /source/inc2 and /source/also/included:

rsync -a --include='/inc1/***' --include='/inc2/***' \
         --include='/also' --include='/also/included/***' \
         --exclude='*' /source /target

See also a quick guide to common rsync filters.

share|improve this answer

Maybe I'm missing something, but you can just specify those folders to rsync:

rsync -Pav /inc1 /inc2 /path/to/backup/folder/

This will create:

/path/to/backup/folder/inc1/
/path/to/backup/folder/inc1/inc1.1/
/path/to/backup/folder/inc1/...
/path/to/backup/folder/inc2/
/path/to/backup/folder/inc2/...
share|improve this answer
    
When you want to use a remote server as the source, I'm guessing you'd have to add user@server: before every folder. I just tried rsync -nav root@server:/etc/psa root@server:/usr/local/psa /localdir/ and it didn't work. So I think this technique only works with local sources. –  Chad von Nau Jul 11 '13 at 0:10
    
@ChadvonNau: rsync will work with at most one remote (ie, either a remote source or a remote destination). In your example, you're using both a remote source and a remote destination. –  Jeremy Kerr Jul 12 '13 at 7:16
    
I was attempting to do two remote source folders and a local destination. Trying to apply your two source technique to a remote host situation. –  Chad von Nau Jul 12 '13 at 20:32
1  
@ChadvonNau: Ah, I didn't interpret the newline as a space there! In that case, try rsync -nav 'root@server:{/etc,/usr/local}/psa' /localdir/ –  Jeremy Kerr Jul 23 '13 at 7:40
    
That's awesome. It doesn't maintain the directory structure the way doing it with an includes file would, but it's way easier to setup. –  Chad von Nau Jul 24 '13 at 7:45

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.