Archiving with Pax
Pages: 1, 2, 3
The pax utility can also restore tar and cpio archives. It is able to automatically detect the correct format for you; however, you should
use the file utility before attempting the restore to determine
whether or not the archive is compressed. If it is, you'll need to include the
z switch.
As an example, I have a file called backup.old located in my home
directory (~). I'll first use the file utility:
file backup.old
backup: gzip compressed data, deflated, last modified:
Sat Aug 17 14:21:12 2002, os: Unix
Since this backup is compressed, I'll use this command to restore it to
the test directory:
cd test
pax -rvzf ~/backup.old
I have another file in my home directory called backup:
file ~/backup
backup: cpio archive
This file isn't compressed, so I'll restore it, like so:
pax -rvf ~/backup
The fact that the first backup happened to be a tar archive and the second
a cpio archive didn't confuse pax; however, I would have received some
strange error messages if I had forgotten to inform pax that the first
archive was compressed.
You can do some pretty funky things when restoring with pax. For
example, you can do an interactive rename/restore by including the i
switch. Issuing the following command:
pax -rif ~/backup
will start an interactive restore of the archive named backup into the
current directory. In interactive mode, pax will display the name of each file, one
at a time, and prompt you to either rename it as it's restored, restore it
with the original name, or to skip it and not restore it:
ATTENTION: pax interactive file rename operation.
drwxr-xr-x Aug 17 15:08 .
Input new name, or a "." to keep the old name, or a "return" to skip this file.
Input >
Skipping file.
Here, I pressed enter as I didn't want to change the name of "." or the
current directory.
ATTENTION: pax interactive file rename operation.
drwxr-xr-x Jul 26 16:10 file1
Input new name, or a "." to keep the old name, or a "return" to skip this file.
Input > old
Processing continues, name changed to: old
ATTENTION: pax interactive file rename operation.
-rw-r--r-- Jun 11 00:20 file2
Input new name, or a "." to keep the old name, or a "return" to skip this file.
Input > .
Processing continues, name unchanged.
You'll note that I changed the name of file1 to old and kept file2 as is. A listing of the restored directory will show two files: one named old and one named file2.
One of the most powerful features of pax is that it is able to very
quickly copy a complete directory structure to another portion of your
hard drive, using copy mode. To use copy mode:
cdinto the source directory.- Ensure the destination directory exists; if it doesn't, use
mkdirto create it. - Issue this command:
pax -rw . destination_directory
Note that you don't include the f switch in copy mode, as an archive
file doesn't get created. Instead, the old directory structure is directly
recreated into the new directory structure.
Also note that you never want to do this:
cd
mkdir test
pax -rw . test
In the above example, I cd'd into my home directory, made a subdirectory
named test, then invoked copy mode. In doing so, I ended up in an
endless loop of test subdirectories, each containing the contents of my
home directory. If I hadn't interrupted this cycle with a CTRL-C, pax
would have continued ad infinitum, where infinitum is defined as the point
where I run out of disk space.
That's what this section of man pax refers to:
Warning: The destination directory must not be one of the file operands or a member of a file hierarchy rooted at one of the file operands. The result of a copy under these conditions is unpredictable.
However, this works beautifully and almost instantaneously:
su
Password:
cd ~user1/big_project
mkdir ~user2/big_project
chown user2 ~user2/big_project
pax -rw . ~user2/big_project
Voila, the entire big_project directory structure is now also in the
second user's home directory. When using copy mode, you'll have to become the
superuser as you'll be copying out of your home directory, so you can
avoid the endless loop situation. If you have to make the new
directory, it will be owned by root; if need be, use the chown
command like I did to ensure that it has the desired ownership before
doing the copy operation. You'll also want to take a look at man
pax first to see how you want to handle the permissions of the copied
directory structure.