Tar: How to ignore the archive itself

6

4

I was planning to do a quick backup of my website. So I tried to run the following command in my webroot:

tar cvfz backup.tar.gz .

That seemed to be working nicely for a while. Until I discovered it had started to backup backup.tar.gz...

Is there an easy way that I can get it to ignore the file I am archiving to?

Note: I would of course have put the archive file in the directory above it if I could. But I only have access to my home directory.

Svish

Posted 2009-12-20T13:06:06.957

Reputation: 27 731

What about access to /tmp? That's always been world writable on every machine I've ever touched. – Doug Harris – 2009-12-20T20:43:45.620

That could be. But that is also readable by everyone, isn't it? – Svish – 2009-12-21T17:04:48.337

Answers

7

Try

tar cvfz backup.tar.gz *

This way shell extends the *, vs the tar reading current directory. The difference is that the produced archive does not contain root folder.

If you also need to include hidden files (.files) you can try

tar cvfz backup.tar.gz * .??*

This includes .files and prevents inclusion of parent directories.

Ahe

Posted 2009-12-20T13:06:06.957

Reputation: 969

To expand on this a bit, add a ./ before the *. This makes it possible to add --exclude statements that are relative to the root path of the tar command.

tar cvfz backup.tar.gz ./* --exclude "./relative/path" – kirkmadera – 2017-04-18T20:35:59.433

Interesting. What does .??* do exactly? – Svish – 2009-12-20T13:25:04.097

? is a 'any character' when the * is 'any character sequence'. Shell does not normally include .files with *, so .?? is simply any file beginning with a dot and having two or more characters after the dot, thus preventing inclusion of . and .. – Ahe – 2009-12-20T13:35:24.850

2.??* of course has a problem, it does not include .a and so on, but it is faster to write than .[^.]* and files with a dot and one character are rare. – Ahe – 2009-12-20T13:39:56.753

Ahaa. Good stuff. Seems to be working nicely :) – Svish – 2009-12-20T14:01:02.043

3

b=backup.tar.gz; tar --exclude=$b -zcf $b .

Alexander Shcheblikin

Posted 2009-12-20T13:06:06.957

Reputation: 620

2A little explanation of what's going on here and how your answer differs from the others already offered will greatly improve this answer. – music2myear – 2017-06-06T23:37:16.177

1

I'm sure there's a nicer way, but I always just do it from the a different directory.

tar cvfz backup.tar.gz /path/to/www

This link looks like it has the information you might want.

mrduclaw

Posted 2009-12-20T13:06:06.957

Reputation: 318

Yeah, but I can't do that since that directory is the only one I have access to. Added note about that to my question :) – Svish – 2009-12-20T13:17:44.207

the --exclude= option looks viable, as well. – mrduclaw – 2009-12-20T13:46:25.247