Friday, November 27, 2009

tar exclude directory or files

SkyHi @ Friday, November 27, 2009
it`s very annoying to use exclude like this

#tar \
--exclude… \
--exclude… \
--exclude… \
--exclude… \
-cvpzf home_bup.tgz /home/username


I suggest such manner

for fedora core

#tar cvpzPf /tmp/backup.tar.gz –exclude={/proc/*,/sys/*,/tmp/*,/dev/*} /

for Debian

#tar cvfpP /tmp/debian2.tar –exclude={“/proc/*”,”/sys*”,”/tmp/*”,”/home/user/*”} /

and more common if i need backup using ssh

#ssh root@192.168.0.1 “cd /;nice -n 10 tar cvpP –exclude={“/proc/*”,”/sys*”,”/tmp/*”,”/home/user/*”} /”>backup.tar.gz



TEST:
[user1@home html]$man tar
--exclude PATTERN
exclude files based upon PATTERN

-X, --exclude-from FILE
exclude files listed in FILE


[user1@home html]$pwd
/var/www/html

[user1@home html]$ mkdir test{1,2,3}
drwxrwsr-x  2 user1 group1     4096 Nov 27 15:32 test1
drwxrwsr-x  2 user1 group1     4096 Nov 27 15:32 test2
drwxrwsr-x  2 user1 group1     4096 Nov 27 15:32 test3

[user1@home html]$ ll
total 10000
-rw-r--r--  1 user1 group1      493 Nov  2 16:09 1.php
-rw-r--r--  1 user1 group1      493 Nov  2 16:09 1.php.bak
-rw-r--r--  1 user1 group1      466 Nov  2 15:36 2.php
-rw-r--r--  1 user1 group1     1090 Nov  3 14:22 3a.php
-rw-r--r--  1 user1 group1     1047 Nov  3 14:16 3a.php.bak
-rw-r--r--  1 user1 group1      513 Nov  3 13:58 3.php
-rw-r--r--  1 user1 group1      525 Nov  2 16:24 3.php.bak
-rw-r--r--  1 user1 group1      160 Nov  2 16:56 4.php
-rw-r--r--  1 user1 group1      160 Nov  2 16:56 4.php.bak
-rw-r--r--  1 user1 group1     1013 Nov  3 12:35 5.php
-rw-r--r--  1 user1 group1     1013 Nov  3 12:33 5.php.bak
-rw-r--r--  1 user1 group1      295 Nov  3 12:49 6.php
-rw-r--r--  1 user1 group1      232 Nov  3 12:48 6.php.bak

[user1@home html]$ cp [1-4]*.php test1
[user1@home html]$ cp [5-7]*.php test2

##create a tarball of all the test directories except test1 directory

[user1@home html]$ tar cvpzPf testnov27.tar.gz --exclude=test1 test*
test2/
test2/6.php
test2/5.php
test3/


[user1@home html]$ tar tvf testnov27.tar.gz
drwxrwsr-x user1 group1      0 2009-11-27 15:33:36 test2/
-rw-r--r-- user1 group1      295 2009-11-27 15:33:36 test2/6.php
-rw-r--r-- user1 group1      1013 2009-11-27 15:33:36 test2/5.php
drwxrwsr-x user1 group1      0 2009-11-27 15:32:44 test3/


##camera server
[/var/www] # tar -cvzf camerawww.tar.bz2 --exclude=html/pics html/

Reference: http://hobbylobby.wordpress.com/2007/11/04/exclude-directories-when-tarring-with-tar/