Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tar exclude single files/directories, not patterns

Tags:

tar

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)

like image 655
Udo G Avatar asked May 09 '12 07:05

Udo G


People also ask

How do I extract one file from a tar file?

For that, you need to make use of the “tar” command with the “-xvf” option and the name of a “tar” file while we are currently located at the “Downloads” folder. The option “x” is used for extraction, “-v” is used to display in ascending order, and “-f” is used to perform the extraction forcefully.

How do I exclude a file from a tar file?

Use the `--exclude-from' option to read a list of patterns, one per line, from file; tar will ignore files matching those patterns. Thus if tar is called as `tar -c -X foo .' and the file `foo' contains a single line `*.o', no files whose names end in `.o' will be added to the archive.

What causes tar to ignore files that match the pattern?

Causes tar to ignore files that match the patterns listed in file. Use the `--exclude-from' option to read a list of patterns, one per line, from file; tar will ignore files matching those patterns. Thus if tar is called as `tar -c -X foo .' and the file `foo' contains a single line `*.o', no files whose names end in `.o' will be added to the ...

What does `--exclude=pattern` do in tar?

`--exclude=pattern'. Causes tar to ignore files that match the pattern. The `--exclude=pattern' option prevents any file or member whose name matches the shell wildcard (pattern) from being operated on.

How do I ignore a specific line in tar?

Use the ‘ --exclude-from ’ option to read a list of patterns, one per line, from file; tar will ignore files matching those patterns. Thus if tar is called as ‘ tar -c -X foo . ’ and the file ‘ foo ’ contains a single line ‘ *.o ’, no files whose names end in ‘ .o ’ will be added to the archive.


2 Answers

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

tar --exclude=/sys --exclude=/proc /
like image 151
CharlesB Avatar answered Oct 07 '22 06:10

CharlesB


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 *
like image 7
Stephen Donecker Avatar answered Oct 07 '22 06:10

Stephen Donecker