Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
tar cvf backup.tar  -C /  /   # or
tar cvf backup.tar    /

produces an archive like

etc/

and gives warnings

tar: Removing leading `/' from member names
tar: Removing leading `/' from hard link targets

on the other hand

tar cvf backup.tar  -C /  . 

produces an archive like

./etc/

and gives no warnings. I got the last one from a Debian tutorial.

Which is best for a system backup, do they work exactly the same?


Is there no difference between tar -tvh producing

./etc/

from an archive

and tar -tvh producing

etc/

from an archive

?

share|improve this question

migrated from stackoverflow.com Jun 9 '11 at 15:20

This question came from our site for professional and enthusiast programmers.

    
I don't see that using execve precludes you from using shell commands. Couldn't execve call /bin/sh with option -c followed by a command line that includes pipes, globs, or whatever else might be needed to create the tar file the way you want? –  Marnix A. van Ammers Jan 25 '11 at 19:23
    
Had I been using Unix only, it would have been acceptable. But utility must be portable, and sh doesn't exist on Windows, for example –  Daniel Jan 25 '11 at 19:36

3 Answers 3

up vote 0 down vote accepted

Both the examples you give are effectively equivalent - you end up with relative paths in the archive.

If you want to include absolute paths in the archive use -P, but beware that it will be inconvenient to restore to anywhere other than the root.

share|improve this answer
    
Is there no difference between tar -tvh producing ./etc/ from an archive and tar -tvh producing etc/ from an archive ? –  sabgenton Jun 10 '11 at 1:20
    
They are different spellings of the same relative path. –  Richard Kettlewell Jun 11 '11 at 14:08

What you want to use depends on what you're doing.

If you're just trying to make a backup of your files, then the tar command will work for you (more on that below). If you're trying to make an image of your harddrive, then use the dd command. Please know that you can't just put your tar backup on a new drive and boot with it.

The issue you'll run into with the tar command is that your file will be placed under the / directory, which is what you're backing up. So you'll need to exclude your backup file.

cd /
tar -cvpzf backup.tar.gz --exclude=/backup.tar.gz /

share|improve this answer
    
I know it won't get the MBR but if I have a /boot partition and I just tarballed the partition the bootloader points to won't that work? I do the same thing with rsync, works fine. –  sabgenton Jun 10 '11 at 1:26
    
That should work if you're planning on restoring your backup to the same drive. I always plan for the worse (ie, the drive dying). Has the tar command produced what you were expecting? –  Chris Ting Jun 10 '11 at 14:13

I have found a sed solution, it's better than using tar twice.

tar cvfz backup.tar.gz -C /path/to/dir/ --transform 's/^\.//' .
share|improve this answer
    
Note that this will skip your dotfiles :) –  iElectric Sep 1 '11 at 10:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.