By Kristopher M. March
This article reviews some of the common backup and restore tools available on the Solaris Operating System. The examples provided here allow you to start using a utility immediately, without having to understand all of its features. Having some knowledge of these tools is crucial to general system administration. The examples shown here were tested on the Solaris 5.8 OS. They function on other versions as well.
In this article, I only list basic uses of each of these commands. If you need more information I suggest you reference the man pages, which go into much more detail for each utility.
More specifically, this article covers the following backup and recovery utilities:
tar — Create Tape and File Archives, Restore Files and Directories
tar command is found on many UNIX platforms. It is a quick and easy tool to use for archiving files to tape.
tar can also be used to archive many files to one file — known as a tar file — that is portable for use on other systems. For example, entire user home directories or installation directories can be copied to a single tar file and moved over to a server running HP-UX if needed.
Numerous options are available for the
tar command, but I will only discuss the three options used to create, extract or restore, and list tar files.
tar Command Options
-c: Create a tar file
-t: List the contents of a tar file
-x: Extract or restore a tar file
-v: Verbose (display the actions
tar is taking)
The basic syntax for
tar is as follows:
tar <options> <tar filename> <file list>
Note: Sometimes you will see examples of the
tar command using a dash to precede the options. This is not necessary for
tar to function correctly.
- Create a new tar file:
% tar cvf filename.tar filelist
filename.taris the name of your tar file.
filelistis the list of files you want to back up. Wildcards can be used to specify your list.
- List the contents of a tar file:
% tar tvf filename.tar filelist
In this case,
filename.tarcan be substituted for a tape device file. For example:
% tar cvf filename.tar /dev/rmt/0mn
- Extract the tar file to the current location:
% tar xvf filename.tar
dd — Convert and Copy a File
dd command is most commonly used to copy a complete file system to another file system or to copy a hard disk drive to another disk drive.
dd can also be used to copy a file system to tape, and vice versa.
dd is a relatively quick copy tool: It creates an exact copy (byte for byte) as it transfers the data. Several options are used with
dd to specify buffer sizes, block sizes, and data conversions. The basic syntax for
dd <inputfile>= <outputfile>= options
The following example copies the entire contents of c0t1d0s2 to a second disk, c0t4d0s2, using a block size of 128. This works great if you have a spare disk available and want to have a backup disk ready to swap out in case of a disaster. Slice 2 is specified in this example because it represents the entire disk in the Solaris OS.
% dd if=/dev/rdsk/c0t1d0s2 of=/dev/rdsk/c0t4d0s2 bs=128
To copy the contents of one tape device to another, use the following example. (Note: You must have two drives available.)
% dd if=/dev/rmt/#1drive of=/dev/rmt/#2drive
The man pages on
dd give many other options available for use. I suggest viewing the man pages for
dd and also reading the Notes section. There you will find some limitations to
dd, as well as a reminder to use the raw character device when copying data from disk devices.
cpio — Copy File Archives In and Out
cpio stands for “copy in, copy out” and is used to copy data from one location to another. There are several advantages to using
cpio over other UNIX utilities:
cpiocan back up and restore individual files, not just whole file systems.
- File header information on files created by
cpiois smaller, resulting in smaller-sized backups.
- Unlike tar, which is limited to a single tape device,
cpiocan span multiple tapes.
cpio operates in three modes: copy-out (
cpio -o), copy-in (
cpio -i), and pass mode (
cpio -p), which is used to copy files from one location to another on disk and not tape. The following section lists several commonly used options.
cpio Command Options
These options apply to copy-out and copy-in modes, unless otherwise noted.
-c: Write header information in ASCII format, for portability.
-d: Create as many directories as needed.
-v: Verbose (report the names of the files as they are processed).
-V: Same as the preceding, except a “
.” is printed for each file copied.
-u: Use for an unconditional copy; old files will not replace newer versions.
-m: Retain the previous file modification time. This option will not work on directories being copied.
To copy a directory and its subdirectories to tape, use the following example:
% ls -R | cpio -oVc > /dev/rmt/0
The previous example will copy the directory that you are currently in — along with all subdirectories within it — to a tape device located at
-o specifies that we are in copy-out mode. The
-V option is for verbose and will display dots as a progress indicator. The
-c option create an ASCII header file.
To copy from tape back to a directory, use the following example:
% cpio -icvD < /dev/rmt/0
In the preceding example, our command will use several options to restore the contents of data on the tape. The
-i option puts us in copy-in mode. The
-d option creates all the directories needed as it copies the data back.
Backing Up Files With
cpio (Copy-Out Mode)
There are several ways to back up files with
cpio. I’ll cover two of them here in the article.
% cpio -ov list > /dev/rmt/0
This command reads from the file “list” and copies them over to a tape device.
% cpio -o /dev/rmt/0
This command will allow you to specify files to be backed up. After you are finished entering file names, hit Ctrl+D to execute the command. Here’s an example:
% cpio -o > /dev/rmt/0 filename.txt file1name.txt
Restoring Files With
cpio (Copy-In Mode)
The restore process with
cpio is similar to backing up files. Instead of the
-o for copy-out, we use the
-i mode to copy back in from tape.
% cpio -icvum < /dev/rmt/0
This example utilizes similar options used earlier, as well as the
-m option, which will preserve file modification times. Use the following example to view a list of contents on the tape archive:
% cpio -ict < /dev/rmt/0
Pass mode can be used to copy directories on a disk device. It will not work against a tape.
cpio is preferred over
cp when copying files and directories because it preserves ownership and modification times.
The following example copies all files and directories to a directory called
% ls * | cpio -pdumv bkup
pax — Portable Archive Interchange (Extracts, Writes, and Lists Archive Files)
pax utility can be found on many UNIX flavors that are POSIX compliant. It has been included in Solaris OS releases since 2.5.
pax has the ability to read and write
cpio archives. Depending on how you choose to use
pax, it will operate in one of four modes: read, write, list, or copy. To set the mode, you use either
-w, a combination of the two, or no option at all.
pax Mode Options
pax Command Options
The following example copies files in the present directory to a tape archive:
% pax -w -f /dev/rmt/0
Notice how we used the
-w option to write the files to tape. The
-f option is used to specify the archive, which in this case is a tape device.
The following example lists a verbose table of contents for an archive stored on our tape device
% pax -v -f /dev/rmt/0
Again we use the
-f option to specify the location of the
pax archive. Because we want to list the archive we exclude the
-w options. This puts
pax in list mode and the
-v will verbosely display anything found to standard output.