I want to backup my server, but not bother with images /videos etc.
how can i tar.gz all those types of files i want (basically text files)?
Use the --exclude=PATTERN
from tar.
That would be a 'negative enumeration'. For a 'positive enumeration' you can do this:
find . -name "*.php" -o -name "*.txt" -o -name "*.inc" -o -name "*.js" -o -name "*.css" -o -name "*.php3" -print0 | xargs -0 tar zcvf backup.tar.gz
-print0
and -0
are used for filenames with spaces on them.
You can use the find command to create an input list for most versions of tar.
Depending on the flavor of Unix are you using the command syntax may differ. For example on IBM AIX it would be something like this:
$ cd /directory/you/want/to/archive
$ find . \( -name '*.php' -o \
-name '*.txt' -o \
-name '*.inc' -o \
-name '*.js' -o \
-name '*.css' -o \
-name '*.php3' \) -print > /tmp/input.list
$ tar -c -L/tmp/input.list -f - | gzip -c > /path/to/backup.tar.gz
If you have the fancy GNU(/Linux) utils you can do:
$ cd /directory/you/want/to/archive
$ find . -regex '.*\.\(php\|txt\|inc\|js\|css\|php3\)' -print > /tmp/input.list
$ tar -c -z -T /tmp/input.list -f /path/to/backup.tar.gz
If you use bash4
or zsh
as your shell, you could also perform this task without resorting to find
.
First, you need to enable fancy file globbing. In Bash, this can be done like this:
shopt -s extglob globstar
In Zsh, the corresponding command is:
setopt extendedglob
The rest is a piece of cake:
tar zcvf archive.tgz /path/to/dir/**/*(*.php|*.txt|*.inc|*.js|*.css|*.php3)
The **
is a recursive wildcard, which matches all levels of subdirectories. The parenthesis is called a glob modifier, and restricts the matches of the preceding *
wildcard to those that also match one of *.php
, *.txt
, etc.