Backup & Recovery: Inexpensive Backup Solutions for Open Systems

3.12. Backing Up and Restoring with the ditto Utility

ditto is a Mac OS X recursive copying utility, which can also create archive files (like tar or cpio). What makes it interesting is that it's the one native tool with the ability to create full backups on all versions of Mac OS X since support for HFS+ features such as resource forks was added when the tool was brought forward from NEXTSTEP. (See the section "How Mac OS Filesystems Are Different" earlier in this chapter for more on HFS+.)

ditto can copy files and directories to one of three types of destinations: a directory, a ZIP archive file, or a cpio archive file. It does not support copying directly to tape. On the other hand, it doesn't come with Yet Another Archive Format, so you won't get stuck with backup archives in some format that might not be easily readable in a few years.

3.12.1. Syntax of ditto When Backing Up

The most common use of ditto is to make recursive copies of files and directories, like so:

$ ditto V --rsrc src... dest_dir

The V flag shows everything that ditto is copying. The -rsrc flag ensures that HFS+ attributes and resource forks are copied (which is the default from Mac OS X 10.4 onwards). Extra HFS+ information is stored in AppleDouble format, where the data for a file named filename is kept in ._ filename.

Using ditto like this is a lot like using cp R, with one big difference. Let's say you want to make a copy of a directory. Using cp R src_dir dest_dir, you'd end up with the contents of src_dir under dest_dir/src_dir/. With ditto src_dir dest_dir, the contents of src_dir end up directly under dest_dir/, which can be somewhat confusing if you don't expect it. Also, ditto creates dest_dir/ if it doesn't already exist.

In most cases, ditto makes an exact duplicate of the source. However, there are a few things that ditto won't copy, in which case you'll be missing some information:

  • Named sockets; see the socket(2) and bind(2) manpages (which don't appear to exist in Mac OS X 10.4 for some reason, although they do in earlier versions). However, sockets should be created dynamically by programs that use them.

  • Named pipes (or FIFOs); see the mkfifo manpage. Fortunately, Mac OS X itself doesn't employ any named pipes.

  • BSD flags; see the chflags manpage. Again, Mac OS X doesn't come with BSD flags set on any files.

  • Extended ACLs; see the chmod and fsaclctl manpages. By default, filesystems don't have extended ACL functionality enabled.

These are apparently limitations of the underlying bill-of-materials (or BOM) framework employed by ditto. (See the bom, mkbom, and lsbom manpages.) mkbom also doesn't get named sockets or pipes, and the BOM file format doesn't include fields for BSD flags or extended ACLs.

In addition to making straight copies of files and directories, ditto can copy them into an archive file. To create a cpio file (with optional gzip compression), use the command:

$ ditto V -rsrc c -z src_dir dest.cpgz

To create a ZIP file, use the command:

$ ditto V -rsrc c -k src_dir dest.zip

When creating a ZIP file, using the -sequesterRsrc flag stores extra HFS+ data in a directory named _ _MACOSX; PKZIP-compatible utilities (other than ditto itself) may handle this better than AppleDouble.

As when making recursive copies, src_dir is lost from pathnames stored in an archive file. To retain src_dir in the archived pathnames, use the -keepParent flag.

One thing you can't do with ditto is selectively archive only part of a directory's contentsfor example, you can't use a filename pattern or make incremental backups. ditto is suitable only for archiving entire directory trees.

You can use ssh and dd to make backups to remote systems, the same way you can with tar or cpio. For example:

$ ditto V -rsrc c -k src_dir - | ( ssh remote_host dd of=dest.zip )

Note that in this example, ditto can archive to standard output; it can also accept standard input as the source. It's possible this functionality could be used for tape-based backup and restore (if suitable tape device drivers are available), but this hasn't been tested.

3.12.2. The Options to the ditto Command

ditto is a very simple command, with relatively few options and a straightforward argument syntax. Here are some of the options you can use; refer to the manpage for more:

-v

Prints the name of each source directory as it's copied.

-V

Prints a line for each file and directory copied by ditto.

-c

Instead of copying the contents of the source directory to another directory, copies to an archive file. This is a cpio archive by default, unless the k flag is used.

-z

Uses gzip to compress the cpio archive.

-k

Creates a compressed ZIP archive instead of a cpio archive.

-X

Prevents ditto from crossing partition boundaries when copying.

--keepParent

Includes the source directory in the pathnames saved to the archive.

--rsrc

Copies HFS+ attributes and resource forks, in addition to standard Unix attributes and data forks. This is the default for Mac OS X 10.4 and later. Can also be specified as rsrcFork.

--norsrc

Prevents ditto from copying HFS+ attributes and resource forks. This is the default for Mac OS X 10.3 and earlier, or for Mac OS X 10.4 and later if the DITTONORSRC environment variable is set.

--sequesterRsrc

Saves HFS+ data and resource forks in a directory named _ _MACOSX, instead of in AppleDouble format.

--arch

When making a copy of an application with support for multiple CPU architectures (what used to be called fat binaries, and which Apple now calls Universal applications), copy only the elements for the specified architecture. The architecture can be either ppc (for PowerPC) or i386 (for Intel, a reference to the first Intel CPU supported by NEXTSTEP).

--bom

Copy only the items listed in the specified bill-of-materials file. (You can create a BOM file with mkbom directory; see the manpage for more.) BOMs are used in Apple's Installer packages and record permissions, ownership, and a checksum for each item installed by a package.

3.12.3. Syntax of ditto when Restoring

Restoring the contents of a ditto-created archive is done with the x flag (for "extract"). To restore from a compressed cpio archive, use the command:

$ ditto V -rsrc x src.cpgz dest_dir

The destination directory is created if it doesn't already exist. Note that the z flag isn't required; ditto automatically handles compressed cpio files.

To restore from a ZIP archive, use the command:

$ ditto V -rsrc x k src.zip dest_dir

There's nothing special about archive files created by ditto; you could run extractions from any cpio or ZIP file using ditto.

If you want to restore only selected parts of an archive, use either cpio or unzip directly because you have no way of specifying that with ditto.

3.12.3.1. Listing the files in a ditto archive

To list the files in a compressed cpio archive, use the command:

$ cpio itvz < src.cpgz

To list the files in a ZIP archive, use the command:

$ unzip lv src.zip

Категории