Linux / UNIX: tar Command Stay In Local / File System When Creating Archive

Posted on in Categories , , , , , last updated December 5, 2009

I‘d like to make / file system backup but the problem is there are other file systems mounted under / such as:

/mnt/cdrom
/mnt/nfs
/iscsi
/boot
/var

I’d like to skip all other file systems mounted at / and only make backup of local / and not /boot, /var, /iscsi, and /mnt/cdrom etc. How do I force tar to use a single file system under Linux / UNIX / BSD operating systems?

The –one-file-system option ask the tar command not to cross mount points and stay in local file system when creating / updating archives. The syntax is as follows:

tar zcvf /path/to/file.tar.gz  --one-file-system fileSystems
tar zcvf /path/to/file.tar.gz  --one-file-system /

In this example, make a backup of / file system to /dev/st0 without crossing file system boundaries, enter:

tar cvf /dev/st0  --one-file-system /

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

8 comment

  1. Old habit for me is rather than using absolute pathname for root, you cd there and use the relative pathname “.” instead. Old versions of tar would restore the file exactly as it was created in the archive, so if you did this:

    tar cvf /dev/st0 /

    it created a tar file that could only be restored in /. Whereas if you do this:

    cd /
    tar cvf /dev/st0 .

    you get a tar file that can be restored anywhere (including root as long as you cd there first).

    Yes, many versions of tar have fixed this and Linux strips leading /s by default, but unless you’re sure and there’s no chance you may ever take your tar file to another machine (say an old Unix box), this is an easy way to be sure.

  2. I seem to recall if you explicitly call a directory, tar will ignore your one-file-system argument for that case.

    Ie if you execute
    tar cvzf /path/to/file.tar.gz --one-file-system /*
    You’ll end up with /sys and /proc et al, because you globbed them in!

    Definitely test it before you send it after 1TB of data…

  3. would like to put my root to tape on the hp dds-4c5718A. using the unix command, how do i do it. when using the tar cvf /dev/st0 /* and Iam getting an error message
    tar: Error exit delayed from previous errors.

Leave a Comment