Page 1 of 1

Tarball Contains... itself?

Posted: Tue Mar 20, 2012 11:01 am
by RCA86
Hi guys,

I'm the furthest thing you can imagine from a Linux pro, so this may be a bit of a noob question, but I'm running some backups on one of our servers (we don't have an actual SA), and I've recently encountered an annoying problem. When I run the following command to zip the entire current directory (which is normally httpdocs), it seems to include itself in the tarball: (run in httpdocs directory)

Code: Select all

tar -zcvf backup.tar.gz .
I think this has only started happening since I've been using the '.' at the end, instead of the an '*', as that wouldn't include .htaccess.

Any ideas? Alternatively, and this is a total noob question, can I run it from outside the directory like so:

Code: Select all

tar -zcvf backup.tar.gz httpdocs/.
Would that work?

EDIT: I'm guessing actually just tar -zcvf httpdocs would do the trick...?

Thanks

Re: Tarball Contains... itself?

Posted: Tue Mar 20, 2012 11:18 am
by requinix
- The file is created while tar is going about its business. Naturally it'll find that file when it's searching the directory.
- Using * means the "searching" is done by the shell instead. Happens before tar has a chance to create the file and thus it doesn't show up.
- If you specify the directory like that then the tar will contain the httpdocs folder too - not just what it contains.

There are a handful of options but my preferences lean towards what you had before with the wildcard. If you want hidden files then include ".*" in the list.

See also: tar(1)

Re: Tarball Contains... itself?

Posted: Tue Mar 20, 2012 12:05 pm
by RCA86
That was super fast, cheers!

I wasn't sure what you meant, so I tried the following command:

Code: Select all

 tar -zcvf test.tar.gz .*
This started going a bit crazy and adding a lot of files I didn't recognise though, so I'm not sure how exactly that works. Did I misunderstand what you meant?

Re: Tarball Contains... itself?

Posted: Tue Mar 20, 2012 1:01 pm
by requinix
I just realized that .* includes .. too. Try

Code: Select all

.[^.]*
There's something else I'm forgetting that's better but, of course, I'm forgetting it. Maybe piping a file list out from find?

Re: Tarball Contains... itself?

Posted: Tue Mar 20, 2012 9:27 pm
by Doug G
With Fedora's tar you can use --exclude=backup.tar.gz to prevent the tarball from including itself.

Re: Tarball Contains... itself?

Posted: Wed Mar 21, 2012 10:39 am
by RCA86
Thanks again for the replies.

@Requinix: hmm, I think I see what you mean, but that one doesn't work on my system.

@Doug: dang, that would be perfect, but I'm running CentOS, and the exclude command isn't working.

I'll keep fiddling with it, and see if I can't get some solution. I might even just use '*' to select the files, and add .htaccess files to the tarball afterwards. That should work... I think. I'm such a noob. :D

Re: Tarball Contains... itself?

Posted: Wed Mar 21, 2012 2:28 pm
by Doug G
From a terminal do man tar to get the exact syntax. CentOS and Fedora are very similar and should support the same syntax for --exclude.