1836642 Members
2509 Online
110102 Solutions
New Discussion

About "Tar" function

 
eric_204
Frequent Advisor

About "Tar" function

I would like to use "Tar" to backup a directoy , but there are many sub-directories under this directory , how can I skip one of directory to be backup ? thx

eg.
# cd /home
# ls
ACC
EDP
ENG
MKT
SAL

how can I use tar to backup all the /home directory , but skip the dirctory "EDP" ? thx.
9 REPLIES 9
Denver Osborn
Honored Contributor

Re: About "Tar" function

This will do it

tar -cvf /dev/rmt/0m `find /home | grep -v /home/EDP`


-Denver
Elmar P. Kolkman
Honored Contributor

Re: About "Tar" function

In that case I would suggest using pax. You can force pax to create tar files (-x ustar) and get the list of files to put into in from stdin, so your line would become something like this:
find . | grep -v '^EDP' | pax -w -f -x ustar -d

Every problem has at least one solution. Only some solutions are harder to find.
Mark Grant
Honored Contributor

Re: About "Tar" function

I really like Denver's answer!, I wouldn't have thought of that myself :)

However, I fear that you could end up having the old and irritating "too many arguments" error with the solution proposed. Just to be on the safe side, I would add the "-type d" to the arguments of "find" in Denvers solution, just to reduce the chances of this happening.
Never preceed any demonstration with anything more predictive than "watch this"
Elmar P. Kolkman
Honored Contributor

Re: About "Tar" function

But that has another problem: you would have directories and its subdirectories both in the output, which is not what you want.

In that case, you could do it like this:
tar cvf `ls | grep -v EDP`

Which gives a lot shorter command line, but still can give the same error with too many arguments.
Every problem has at least one solution. Only some solutions are harder to find.
Mark Grant
Honored Contributor

Re: About "Tar" function

Yes Elmar, you are correct and your "ls" version does exactly as is required. I would go for that.

GNU "tar" has a "-T filename" option where filename is a file containing the names of the files to backup or restore. If using GNU tar you could just have the names of the directories in there. I remember this was "-F" on SCO "tar" about 100 years ago.
Never preceed any demonstration with anything more predictive than "watch this"
eric_204
Frequent Advisor

Re: About "Tar" function

thx all replies, however , in my real situation , there are many sub-directories that I don't want to backup , could suggest what is the best way ? thx.
Elmar P. Kolkman
Honored Contributor

Re: About "Tar" function

In that case, create a pattern file and use that with the grep command, depending on what solution you use. If you go for the last one, it should be something like this:
tar cf `ls | grep -v -f `
Every problem has at least one solution. Only some solutions are harder to find.
Cheryl Griffin
Honored Contributor

Re: About "Tar" function

# find /home/ACC /home/ENG /home/MKT /home/SAL -type f -mtime -1 >/tmp/home.out

# tar -cvf /dev/rmt/0m 'cat /tmp/home.out'
"Downtime is a Crime."
Denver Osborn
Honored Contributor

Re: About "Tar" function

assuming that you want to archive everything in /home but exclude specific sub directories... use this syntax.

tar cvf /dev/rmt/0m `find /home -type d |grep -v -E '^/home/dir1$|^/home/dir1/subdir2$|^/home/dir3'`


the above will archive all that is in home, /home/dir1/subdir2 and /home/dir3 will not be archived.

To test it out for yourself, run a few find commands piped to grep w/out using tar. What ever is displayed to your screen is what tar would include in the archive.

Hope this helps,
-Denver