If you want to have more flexibility with filenames and compression options, you can use:
find /my/path/ -type f -name "*.sql" -o -name "*.log" -exec \
tar -P --transform='s@/my/path/@@g' -cf - {} + | \
pigz -9 -p 4 > myarchive.tar.gz
find
find /my/path/ -type f -name "*.sql" -o -name "*.log" -exec
This command will look for the files you want to archive, in this case /my/path/*.sql
and /my/path/*.log
. Add as many -o -name "pattern"
as you want.
-exec
will execute the next command using the results of find
: tar
tar
tar -P --transform='s@/my/path/@@g' -cf - {} +
--transform
is a simple string replacement parameter. It will strip the path of the files from the archive so the tarball's root becomes the current directory when extracting. Note that you can't use -C
option to change directory as you'll lose benefits of find
: all files of the directory would be included.
-P
tells tar
to use absolute paths, so it doesn't trigger the warning "Removing leading `/' from member names". Leading '/' with be removed by --transform
anyway.
-cf -
tells tar
to use the tarball name we'll specify later
{} +
uses everyfiles that find
found previously
pigz
pigz -9 -p 4
Use as many parameters as you want.
In this case -9
is the compression level and -p 4
is the number of cores dedicated to compression.
If you run this on a heavy loaded webserver, you probably don't want to use all available cores.
> myarchive.tar.gz
Finally.