Operating System - HP-UX
1820479 Members
2550 Online
109624 Solutions
New Discussion юеВ

tar and exclude directories

 
Aaron_82
Frequent Advisor

tar and exclude directories

We need to tar up the directory /home/customers. Under the /home/customers there are directories for all the customers then their data under that. Within every customer directory there is a archive directory that I would like to exclude from the tar. Is there a way I can exclude all /home/customers/*/archive and get all the rest of the data?
8 REPLIES 8
Leif Halvarsson_2
Honored Contributor

Re: tar and exclude directories

Hi,
There is no exclude option in tar.
Aaron_82
Frequent Advisor

Re: tar and exclude directories

Ok. Can't I pipe a find command? Does find have a exclude option?
Mel Burslan
Honored Contributor

Re: tar and exclude directories

find command does not have an explicit exclude directive but you can use it hand in hand with grep -v. But again, forming of the regexp will require some high level of finesse as you do not accidentally want to leave a file out of the backup inadvertently.
________________________________
UNIX because I majored in cryptology...
Leif Halvarsson_2
Honored Contributor

Re: tar and exclude directories

Hi,
Yes with find you can always "reverse" the options with ! (e.g find . ! -name xyz ).

Florian Heigl (new acc)
Honored Contributor

Re: tar and exclude directories

GNU tar can exclude directories, but it's effectively a bit incompatible with real tar.
That's acceptable IMO as long as You can be sure the target host also has a GNU tar installed.

Otherwise, You could go with find / [options/excludes] -cpio or tar and xargs...
yesterday I stood at the edge. Today I'm one step ahead.
Aaron_82
Frequent Advisor

Re: tar and exclude directories

I'm going from a SCO box to a HPUX box. Still trying to figure out the find command. Any suggestions?
Mel Burslan
Honored Contributor

Re: tar and exclude directories

involving a little bit of manual effort:


find /home/customers | grep -v "/archive/" > /tmp/files2xfer

find /home/customers | grep "/archive/" > /tmp/files2xclude

as you can see, the output of these two commands make up the whole /home/customers directory contents. So, scan the /tmp/files2xclude file to see it is not excluding something that you want backed up otherwise. If you find such a file, move the line to /tmp/files2xfer

then

for file in `cat /tmp/files2xfer`
do
tar -rvf /dev/rmt/0mn $file
done

it will take a while as it will append every file one by one but I think this will do the job. Keep in mind that this is a theory. I have never done this for myself in my sysadmin life. So, be cautious and test it in a smaller directory structure first to see if the times are acceptable.

HTH
________________________________
UNIX because I majored in cryptology...
Geoff Wild
Honored Contributor

Re: tar and exclude directories

Can't exclude with tar - but you can be specific....

What I do on my Linux servers:

#!/bin/bash
# Script to backup Home Directories
#
LOGFILE=/tmp/dumphome.log
GZFILE=`date |awk '{print ($1)}'`.tar.gz
#GZFILE=`date |awk '{print ($3)}'`.tar.gz
TARFILE=`date |awk '{print ($1)}'`.tar
#TARFILE=`date |awk '{print ($3)}'`.tar

echo "Backup of Home Dir on Dune at " `date` >$LOGFILE 2>&1

# remove the old gzip file
if [ -f /home/backups/$GZFILE ]; then
rm -f /home/backups/$GZFILE >>$LOGFILE 2>&1
else
echo "no old $GZFILE found..." >>$LOGFILE 2>&1
fi

# backup the files
tar -cf /home/backups/$TARFILE `cat /root/backupfiles` >>$LOGFILE 2>&1

# gzip the tar file
gzip /home/backups/$TARFILE >>$LOGFILE 2>&1
chown a2b2000 /home/backups/$GZFILE
echo "/home/dumphome is complete " `date` >>$LOGFILE 2>&1

mail -s "Dune: Home Backup Complete" gjwild <$LOGFILE



cat /root/backupfiles
/bin
/boot
/dev
/etc
/home/aquota.user
/home/ftp
/home/httpd
/home/info
/home/webmaster
/initrd
/lib
/misc
/mnt
/opt
/proc
/root
/sbin
/scripts
/tmp
/usr
/var


Notice, that I don't backup /home/backups.....

So, you would have to explicitly set all file in the customer directories...

/home/customers/user1/files
/home/customers/user1/morefiles
/home/customers/user1/anotherdir
/home/customers/user1/.profile

etc...

Rgds...Geoff

Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.