28

I'm using tar to make daily backups of a server and want to avoid backup of /proc and /sys system directories, but without excluding any directories named "proc" or "sys" somewhere else in the file tree.

For, example having the following directory tree ("bla" being normal files):

# find
.
./sys
./sys/bla
./foo
./foo/sys
./foo/sys/bla

I would like to exclude ./sys but not ./foo/sys.

I can't seem to find an --exclude pattern that does that...

# tar cvf /dev/null * --exclude=sys
foo/

or...

# tar cvf /dev/null * --exclude=/sys
foo/
foo/sys/
foo/sys/bla
sys/
sys/bla

Any ideas? (Linux Debian 6)

Udo G
  • 12,572
  • 13
  • 56
  • 89
  • Are you sure there is no exclude? If you are using MAC OS it is a different story! Look [here](http://www.gnu.org/software/tar/manual/html_node/exclude.html#SEC108) – drinchev May 09 '12 at 07:19
  • Not sure I understand your question. There is a `--exclude` option, but I don't know how to match it for single, absolute file names (not any file by that name) - see my examples above. – Udo G May 09 '12 at 07:21
  • Look here: http://stackoverflow.com/questions/984204/shell-command-to-tar-directory-excluding-certain-files-folders – paulsm4 May 09 '12 at 07:22

3 Answers3

16

You can specify absolute paths to the exclude pattern, this way other sys or proc directories will be archived:

tar --exclude=/sys --exclude=/proc /
CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • 4
    True, but the important detail about this is that the excluded file name must match exactly the notation reported by the `tar` listing. For my example that would be `./sys` - as I just found out now. – Udo G May 09 '12 at 07:34
7

Using tar you can exclude directories by placing a tag file in any directory that should be skipped.

Create tag files,

touch /sys/.exclude_from_backup
touch /proc/.exclude_from_backup

Then,

tar -czf backup.tar.gz --exclude-tag-all=.exclude_from_backup *
Stephen Donecker
  • 2,361
  • 18
  • 8
7

In this case you might want to use:

--anchored --exclude=sys/\*

because in case your tar does not show the leading "/" you have a problem with the filter.

pjv
  • 10,658
  • 6
  • 43
  • 60
  • This did the trick for me, thank you! I wanted to exclude a specific directory, not all directories/subdirectories matching the pattern. bsdtar does not have "--anchored" option though, and with bsdtar we can use full paths to exclude specific folders. – Savvas Radevic May 09 '13 at 10:44
  • 4
    ah found it! in bsdtar the anchor is "^": `bsdtar cvjf test.tar.bz2 --exclude myfile.avi --exclude "^myexcludedfolder" *` – Savvas Radevic May 09 '13 at 10:58