tar exclude single files/directories, not patterns

廉价感情. 提交于 2019-12-04 15:45:27

问题


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)


回答1:


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

tar --exclude=/sys --exclude=/proc /



回答2:


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.




回答3:


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 *


来源:https://stackoverflow.com/questions/10511406/tar-exclude-single-files-directories-not-patterns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!