3.12. Backing Up and Restoring with the ditto Utilityditto 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 UpThe 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:
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 )
3.12.2. The Options to the ditto Commandditto 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:
3.12.3. Syntax of ditto when RestoringRestoring 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 archiveTo 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 |