Tuesday, November 29, 2011

Using --exclude-from in rsync

I've been toying with some rsync scripts tonight and after reading the rsync man page I still needed a little trial and error to determine the exact behaviour of the --exclude-from flag.

The --exclude-from flag specifies a file that contains exclude patterns (one per line). The rsync man page is VERY detailed on the subject of filters and how they can be used.

Looking at a simplified version of my scenario, consider the following directory structure:
   Source
   |---.DS_Store
   |---Alpha
   |  |---.DS_Store
   |  |---Document 1.txt
   |  |---Document 2.txt
   |  `---Temporary Items
   |     |---.DS_Store
   |     |---TI1.tmp
   |     `---TI2.tmp
   |---Beta
   |  |---.DS_Store
   |  |---Document 3.txt
   |  |---Document 4.txt
   |  `---Temporary Items
   |     |---.DS_Store
   |     |---TI2.tmp
   |     `---TI4.tmp
   `---Temporary Items
      |---.DS_Store
      `---TI5.tmp
I want backup the Source directory shown above and exclude the following from my backup:
  1. All of the ".DS_Store" files.
  2. All of the "Temporary Items" directories.
  3. All files contained in the "Temporary Items" directories.
My final backup will look like this:
   Destination
   |---Alpha
   |  |---Document 1.txt
   |  `---Document 2.txt
   `---Beta
      |---Document 3.txt
      `---Document 4.txt

Making this happen is very simple. The exclusions file (skip.txt) only needs to contain the following two lines:
Temporary Items
.DS_Store
The rsync command to perform the backup is as follows:
rsync -az  --exclude-from=./skip.txt ./Source/ ./Destination/
The output of this command is the "Destination" directory shown above.

There were two things that caused me greif getting my script to work:
  1. To exclude a file (or directory) with spaces in the name, the names DO NOT need to be quoted or escaped.
  2. To exclude all files underneath a directory you need only exclude the directory itself.
Now that this is committed to my long term memory I can move on.

1 comments:

Ibon Castilla said...

I was trying to scape white spaces all the time with no luck at all. Saved my life, thanks :)

Post a Comment