Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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)?

share|improve this question
add comment

4 Answers

up vote 1 down vote accepted

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
share|improve this answer
    
excellent work you great man!! –  neolix Jul 9 '10 at 13:34
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.