Catatan kerja

Database beberapa permasalahan yang terjadi di tempat kerja

Backup and Recovery Utilities for the Solaris OS October 28, 2008

Filed under: Unix — aderahman @ 3:45 am

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
  • dd
  • cpio
  • pax
tar — Create Tape and File Archives, Restore Files and Directories

The 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

    Here, filename.tar is the name of your tar file. filelist is 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.tar can 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

The 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 is:

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:

  • cpio can back up and restore individual files, not just whole file systems.
  • File header information on files created by cpio is smaller, resulting in smaller-sized backups.
  • Unlike tar, which is limited to a single tape device, cpio can 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 /dev/rmt/0. The -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

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

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 bkup:

% ls * | cpio -pdumv bkup
pax — Portable Archive Interchange (Extracts, Writes, and Lists Archive Files)

The 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 tar and 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 -r, -w, a combination of the two, or no option at all.

pax Mode Options
The standalone -r option is for read mode only. It will allow you to read from disk or tape.
If neither -r or -w are supplied, pax will be placed in list mode. It will read from disk, tape, or standard input and display files and directory hierarchies.
The standalone -w option tells pax to be in write mode. It will copy the current directory and all subdirectories to standard output according to the format specified by -x option. (See the following example). If no files are specified, a list can be read in, one file at a time from standard input.
With both the -r and the -w option specified, chosen files are copied to the destination directory.

pax Command Options
Appends files to the end of an archive that was previously written.
Block size. The block size can be as low as 512 bytes, or any larger size in increments of 512 bytes, with a maximum of 32256 bytes.
Matches all file or archive members except those specified by the pattern and file operands.
-f <archive>
Specifies <archive> as the pathname of the input or output archive. An archive can span multiple devices. pax prompts you for the pathname of the next device in the archive.
Interactively renames files or archive members. Used to supply file names via standard input for selective write or copy.
-p <string>
Specifies one or more file-characteristic options to be used when extracting or archiving a file.

a Does not preserve file access times
e Preserves everything: user ID, group ID, file mode bits, file access times, and file modification
m Does not preserve file modification times
o Preserves the user ID and group ID
P Preserves file mode bits
v Verbose mode
Specifies the output archive format. The default format is ustar. pax can support cpio, tar, bcpio, ustar, sv4crc, and sv4cpio.

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 /dev/rmt/0:

% 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 -r or -w options. This puts pax in list mode and the -v will verbosely display anything found to standard output.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s