Backing up files from specific folders in rsync

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?


Solution 1:

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.

Solution 2:

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.

Solution 3:

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/...