BSD DevCenter
oreilly.comSafari Books Online.Conferences.


Archiving with Pax
Pages: 1, 2, 3

It is also possible to interactively copy a directory structure by including the i switch:

pax -rwi .  ~user2/big_project

Similarly to the previous interactive example, pax will display each filename, one at a time, so you can decide which files to copy over and which files to rename as you do so.

Now, let's do something useful with the pax command. I'll demonstrate how to create an incremental backup system. In this example, the user "genisis" would like to back up any changes she made to her home directory on a daily basis.

First, I'll become the superuser to create a directory to hold the backups:

mkdir /usr/backups

I'll then create a subdirectory and give the user "genisis" ownership of that subdirectory:

mkdir /usr/backups/genisis
chown genisis /usr/backups/genisis

I'll then leave the superuser account and as the user "genisis," cd into my home directory:


I'll then do a full backup of my home directory and save it to an archive file called Monday:

pax -wvf /usr/backups/genisis/Monday .

Now that I have a full backup, I can take daily incremental backups to just back up each day's changes. So when I'm finished with my work on Tuesday, I'll issue this command:

pax -wv -T 0000 -f /usr/backups/genisis/Tuesday .

Notice that I included the time (T) switch and specified a time of midnight (0000). This tells pax to only back up the files that have changed since midnight, so it will catch all of the files that changed today. On Wednesday, I'll repeat that command but will change the archive name to Wednesday.

Also in FreeBSD Basics:

Fun with Xorg

Sharing Internet Connections

Building a Desktop Firewall

Using DesktopBSD

Using PC-BSD

If you have the disk space and want to keep backups for longer than a week, modify your archive names to something like: Aug01, Aug02, etc. It's still a good idea to do a full backup once a week, followed by incremental backups the other days of that week. If disk space is an issue, include the z switch so the backups will be compressed. Also note that the T switch can be much pickier than I've demonstrated; see man pax for the details.

You have to be a bit careful when restoring an archive. By default, pax will overwrite any existing files. If you don't want it to overwrite any files, include the k switch. If you want to be picky about which files are overwritten, use the i switch.

You don't have to restore every file in an archive. If you're going to be selective, it's a good idea to list the archive first to see what you want and don't want. For example:

pax -f ~/backup

To restore all of the files except file3, use this command:

pax -rvf ~/backup -c './file3'

The c switch is the exception switch. Note that your exception pattern (in my case, file3) needs to be enclosed in single quotes (the key next to your enter key). Either use the literal pattern like I did (to pax, this file is known as ./file3, not file3) or use a wildcard, like so:

pax -rvf ~/backup -c '*file3'

If you use a wildcard (*) at the beginning of your pattern as in the above example, you will exclude all files that end with "file3" -- for example: file3, myfile3, thatfile3.

You can also specify which file to restore by using the n, or pattern matching, switch. The following will just restore file2:

pax -rvf ~/backup -n './file2'

The n switch differs from the c switch in that it will only restore the first file that matches the pattern. This means that this command will not restore file3, myfile3, and thatfile3:

pax -rvf ~/backup -n '*file3'

Since file3 is the first file to match the expression, it will be the only file that will be restored.

The c and n switches are also useful when creating an archive; use them to specify which file you'd like to back up, or which file(s) you don't want to back up.

Hopefully, this archiving series has taken some of the mystique out of Unix backups, so that you can choose the utility that works best for you and implement a regular backup schedule for the files on your FreeBSD system. Hopefully, you'll never need to restore a backup, but if you do, you'll be glad that you took the time to master and use your favorite archiving utility.

Dru Lavigne is a network and systems administrator, IT instructor, author and international speaker. She has over a decade of experience administering and teaching Netware, Microsoft, Cisco, Checkpoint, SCO, Solaris, Linux, and BSD systems. A prolific author, she pens the popular FreeBSD Basics column for O'Reilly and is author of BSD Hacks and The Best of FreeBSD Basics.

Read more FreeBSD Basics columns.

Return to the BSD DevCenter.

Sponsored by: