Solaris Backup and Restoration UtilitiesObjective: Explain how to perform incremental, full, and remote backups to tape for an unmounted file system using the ufsdump command or explain how to back up a mounted file system using UFS snapshot. Solaris provides the utilities listed in Table 7.3. These backup utilities can be used to copy data from disk to removable media and to restore it.
The tar UtilityThe primary use of the tar (which stands for tape archiver) command is to copy file systems or individual files between a hard disk and tape or from one file system to another. You can also use tar to create a tar archive on a floppy disk and to extract files from a tar archive on a floppy disk. The tar command is popular because it's available on most Unix systems. If the data you are backing up requires more than one tape, you should use the cpio, pax, or ufsdump commands, which are described in the following sections. The tar command has the following syntax: tar <options> <tar-filename> <file-list> You can replace options with the list of command options in Table 7.4.
For a more complete listing of command options, see the Solaris online man pages. <tar-filename> is used with the f option and can be any name you want. The filename can also be the name of a device, such as /dev/rmt/0 or /dev/rfd0. <file-list> is a list of files you want to include in the archive. tar ExamplesThe following examples illustrate the use of the tar command. To create a tape archive of everything in the /home/bcalkins directory on tape device /dev/rmt/0, you type the following: tar cvf /dev/rmt/0 /home/bcalkins To list the files in the archive, you type the following: tar tvf /dev/rmt/0 To restore the file /home/bcalkins/.profile from the archive, you type the following: tar xvf /dev/rmt/0 /home/bcalkins/.profile You use tar to create an archive file on disk instead of tape. The tar filename is files.tar, as follows: tar cvf files.tar /home/bcalkins To extract files that were created using the preceding example, you type the following: tar xvf files.tar Notice the use of the full pathname when creating an archive with tar. Using the full pathname to create an archive ensures that the files will be restored to their original locations in the directory hierarchy. You will not be able to restore them elsewhere. If you want to be able to restore files with a relative pathname in the preceding example, you can change to the /home/bcalkins directory and specify files to be archived as ./*. This puts the files into the archive, using a pathname that is relative to the current working directory rather than an absolute pathname (one beginning with a forward slash [/]). Files can then be restored into any directory. The use of relative pathnames is highly recommended so that you have the option of restoring an archive without overwriting files that exist but may be different from those in the archive. The dd UtilityThe main advantage of the dd command is that it quickly converts and copies files with different data formats, such as differences in block size, record length, or byte order. The most common use of dd is to transfer a complete file system or partition image from a hard disk to a tape. You can also use it to copy files from one hard disk to another. When you're using it to copy data, the dd command makes an image copy (an exact byte-for-byte copy) of any medium, which can be either tape or disk. The syntax for the dd command is as follows: dd if=<input-file> of=<output-file> <option=value> The command arguments for dd are described in Table 7.5. dd ExamplesThe next few examples illustrate the use of the dd command to copy data. The first example shows how the dd command is used to duplicate tapes: dd if=/dev/rmt/0 of=/dev/rmt/1 This procedure requires two tape drivesa source tape and a destination tape. The next example uses dd to copy one entire hard disk to another hard disk: dd if=/dev/rdsk/c0t1d0s2 of=/dev/rdsk/c0t4d0s2 bs=128K In this example, you need two disks, and both must have the same geometry. Disk geometry is discussed in Chapter 1. Caution Using dd to Copy Data Between Dissimilar Disk Drives Be careful when using dd to copy data between two different types of disk drives. We have used dd to move data from a 4GB disk to an 18GB disk, and the data transferred fine. We were able to access the data, and the option seemed to have completed correctly. Then we noticed that when we went into the format utility, the 18GB disk was labeled as a 4GB disk. This is because "everything" on the 4GB disk transferred to the 18GB diskincluding the disk label! All of our work was wasted. We had to re-identify the disk type, relabel, and repartition the disk to get it to recognize the disk as an 18GB disk. In this example, the option bs=128K specifies a block size. A large block size, such as 128KB or 4096KB, can decrease the time to copy by buffering large amounts of data. Notice in the example that the raw device is specified. For this technique to work properly, you must use the raw (character) device to avoid the buffered (block) input/output (I/O) system. You can use the dd command with tar to create an archive on a remote tape drive. In the next example, tar is used to create an archive on a remote system by piping the output to a tape drive called /dev/rmt/0 on a remote system named xena: tar cvf - <files> | rsh xena dd of=/dev/rmt/0 obs=128 Another example would be to read tar data coming from another Unix system such as older Silicon Graphics systems. The Silicon Graphics system swaps every pair of bytes, making a tar tape unreadable on a Solaris system. To read a tar tape from a Silicon Graphics system, you type the following: dd if=/dev/rmt/0 conv=swab | tar xvf - Note that the argument for the conv option is swab ("swap bytes") and not swap. In a similar way, a Solaris system can create a tar tape that a Silicon Graphics system can read: tar cvf - <files> | dd of=/dev/rmt/0 conv=swab The cpio UtilityThe cpio command is used to copy data from one place to another. cpio stands for copy input to output. When copying files with cpio, you present a list of files to the system's standard input and write the file archive to the system's standard output. The principal advantage of cpio is its flexible syntax. The command acts as a filter program, taking input information from the standard input file and delivering its output to the standard output file. You can manipulate the input and output by using the shell to specify redirection and pipelines. The following are the advantages of cpio over other Unix utilities:
cpio has more options and is therefore perceived as a more complex command than tar. The cpio utility operates in one of three modes: copy out (cpio -o), copy in (cpio -i), or pass (cpio -p). You use copy-out mode when creating a backup tape and copy-in mode when restoring or listing files from a tape. The pass mode is generally used to copy files from one location to another on disk. You must always specify one of these three modes. The command syntax for the cpio command is as follows: cpio <mode> <option> mode is -i, -o, or -p, and option is one of the options described in Table 7.6. cpio ExamplesThe following example shows how to copy the directory /work and its subdirectories to a tape drive with the device name /dev/rmt/0: cd /work -find . | cpio -ocB > /dev/rmt/0 In this example, the find command locates all of the files in the current working directory and pipes them to the cpio command. The -o option specifies copy-out mode, -c outputs the header information in ASCII format, and -B increases the blocking factor to 5,120 bytes to improve the speed. The following example shows how to copy the files located on a tape back into the directory named /work on a hard disk: cd /work cpio -icvdB < /dev/rmt/0 The -i option specifies copy-in mode, -d creates directories as needed to restore the data to the original location, and -v displays all the output. Backing Up Files with Copy-Out ModeTo use copy-out mode to make backups, you send a list of files to the cpio command via the standard input of cpio. You use the Unix find command to generate the list of files to be backed up. You specify copy-out mode by using the -o option on the cpio command line. In the following example, a file named list contains a short list of files to be backed up to tape: cpio -ovB < list > /dev/rmt/1 Normally, as indicated in Table 7.6, cpio writes files to the standard output in 512-byte records. By specifying the -B option, you can increase the record size to 5,120 bytes to significantly speed up the transfer rate, as shown in the previous example. You can use Unix commands to generate a list of files for cpio to back up in a number of other ways, as shown in the following examples. You can back up files by entering filenames via the keyboard. You press Ctrl+D when you have finished typing filenames. For example, enter the following: cpio -oB > /dev/rmt/1 File1.txt File2.txt Ctrl+d You can use the ls command to generate the list of files to be backed up by cpio. You type the following to back up all the files in the current directory but not the files in subdirectories: cd /home/bcalkins ls -d * | cpio -oB >/dev/rmt/1 You need to be careful when using ls to generate the list of files to back up. In particular, you should be sure that the ls command specifies the full path to the files that should be backed up. You will be dissatisfied with the results if you try to use ls -R or any other ls command on a directory unless you specify the -d option to ls. In general, the best command to use for generating a file list is find. You can use the find command to generate a list of files that the user bcalkins created and modified in the past five days. The following is the list of files to be backed up: find . -user bcalkins -mtime -5 -print | cpio -oB > /dev/rmt/1 If the current tape fills up, the cpio program prompts you for another tape. You see a message such as the following: If you want to go on, type device/file name when ready You should then change the tape and enter the name of the backup device (for example, /dev/rmt/1). Restoring Files with Copy-In ModeYou use the copy-in mode of cpio to restore files from tape to disk. The following examples describe methods used to restore files from a cpio archive. The following example restores all files and directories from tape to disk: cd /users cpio -icvumB < /dev/rmt/1 The cpio options specified restore files unconditionally (-u) to the /users directory and retain previous file modification times (-m). The following example selectively restores files that begin with database: cpio -icvdumB 'database*' < /dev/rmt/1 The -d option in this example creates directories as needed. Note Using Wildcards with cpio You must use standard shell escapes to pass the wildcard argument (*) to cpio. For example, the wildcard argument can appear within single quotes. To obtain a list of files that are on tape, you use the following code: cpio -ictB < /dev/rmt/1 The list of files on /dev/rmt/1 then appears onscreen. Using Pass ModePass mode is generally not used for backups. The destination must be a directory on a mounted file system, which means that pass mode cannot be used to transfer files to tape. However, you can use pass mode to copy files from one directory to another. The advantage of using cpio over cp is that with it, original modification times and ownership are preserved. You specify pass mode by using the -p option with cpio. The following example copies all files from /users to /bkup: cd /users mkdir /bkup find . | cpio -pdumv /bkup Files are listed onscreen as they are copied. The pax UtilityThe pax command has been included in Solaris since version 2.5. pax is a POSIX-conformant archive utility that can read and write tar and cpio archives. It is available on all Unix systems that are POSIX compliant, such as IBM's AIX, Hewlett-Packard's HP-UX, and some Linux distributions. pax can read, write, and list the members of an archive file and copy directory hierarchies. The pax utility supports a wide variety of archive formats, including tar and cpio. If pax finds an archive that is damaged or corrupted while it is processing, pax attempts to recover from media defects. It searches the archive to locate and process the largest possible number of archive members. The action to be taken depends on the presence of the -r and -w options, which together form the four modes of operation: list, read, write, and copy (as described in Table 7.7). The syntax for the pax command is as follows: pax <mode> <options> In addition to selecting a mode of operation, you can select one or more options to pax from Table 7.8.
For additional options to the pax command, see the Solaris man pages. When you use pax, you can specify the file operand along with the options from Table 7.7. The file operand specifies a destination directory or file pathname. If you specify a directory operand that does not exist, that the user cannot write to, or that is not of type directory, pax exits with a nonzero exit status. The file operand specifies the pathname of a file to be copied or archived. When the file operand does not select at least one archive member, pax writes the file operand pathnames in a diagnostic message to standard error and then exits with a nonzero exit status. Another operand is the pattern operand, which is used to select one or more pathnames of archive members. Archive members are selected by using the filename pattern-matching notation described by fnmatch. The following are examples of pattern operands:
When a pattern operand is not supplied, all members of the archive are selected. When a pattern operand matches a directory, the entire file hierarchy rooted at that directory is selected. When a pattern operand does not select at least one archive member, pax writes the pattern operand pathnames in a diagnostic message to standard error, and then exits with a nonzero exit status. pax ExamplesThe following examples illustrate the use of the pax command. To copy files to tape, you issue the following pax command, using -w to copy the current directory contents to tape and -f to specify the tape device: pax -w -f /dev/rmt/0 To list a verbose table of contents for an archive stored on tape device /dev/rmt/0, you issue the following command: pax -v -f /dev/rmt/0 The tape device in these two examples could have been a filename to specify an archive on disk. You use the following command to interactively select the files to copy from the current directory to the destination directory: pax -rw -i . <dest-dir> Because pax understands tar and cpio formats, it is a very helpful tool when a tar or cpio archive contains absolute pathnames and the files should not be restored to their original locations. The key is the -s option, which allows files to be programmatically renamed. The following example uses the -s option to extract files from a tar archive, stripping the leading slash from any absolute pathname: pax -r -s ',^/,,' -f file.tar As you become more familiar with the pax utility, you might begin to use it in place of tar and cpio for the following reasons:
The ufsdump UtilityObjective: Explain how to perform incremental, full, and remote backups to tape for an unmounted file system using the ufsdump command, or explain how to back up a mounted file system using UFS snapshot. Given a backup requirement, develop a backup strategy that includes scheduled backups, number of tapes required, naming conventions, command protocols, and backup frequency/levels. Whereas the other Solaris utilities discussed in this chapter can be used to copy files from disk to tape, ufsdump is designed specifically for backups and is the recommended utility for backing up entire Solaris file systems. The ufsdump command copies files, directories, or entire file systems from a hard disk to tape or from disk to disk. The only drawback of using ufsdump is that the file systems must be inactive (that is, unmounted or read-only) before you can conduct a full backup. If the file system is still active, nothing in the memory buffers is copied to tape, and you could end up with a corrupt backup. You should back up any file systems that are critical to users, including file systems that change frequently. Table 7.9 gives suggestions on the file systems to back up and the suggested frequency. The ufsdump command has many built-in features that the other archive utilities don't have, including the following:
Backing up a file system with ufsdump is referred to as dumping a file system. When a file system is dumped, a level between 0 and 9 is specified. A level 0 dump is a full backup and contains everything on the file system. Levels 1 through 9 are incremental backups and contain only files that have changed since previous dumps at lower levels. A recommended backup schedule involves a three-level dump strategy: a level 0 dump at the start of the month (manually), automated weekly level 5 dumps, and automated daily level 9 dumps. The automated dumps are performed at 4:30 a.m., for examplea time when most systems are typically idle. Automated daily dumps are performed Sunday through Friday mornings. Automated weekly dumps are performed on Saturday mornings. Backups are automated by creating a shell script and using cron to execute the script on a regular basis. Table 7.10 shows the dump level performed on each day of a typical month. Note that the level 0 dump at the start of the month is performed manually because the entire system must be idle before you can back up the root file system. One way to ensure that the system is not being used is to put the system in single-user mode. The level 9 and 5 dumps are automated with cron, but also must be conducted when the file systems are not being used. See Chapter 5, "Managing System Processes," for more information on cron.
The backup schedule in Table 7.10 accomplishes the following:
This dump schedule requires at least four sets of seven tapesone set for each week and one tape for the level 0 dump. Each set will be rotated each month. The level 0 tapes should not be overwritten and should be saved for at least a year, depending on your company's and jurisdiction's data-retention policy. Even with the backup schedule outlined in Table 7.10, data can still be lost. For example, if a hard disk fails at 3 p.m., all modifications since the preceding 4:30 a.m. backup will be lost. Also, files that were deleted midweek will not appear on the level 5 tapes. Or a user may accidentally delete a file and not realize it for several weeks, but when the user wants to use the file, it is not there. If he asks you to restore the file from backup, the only tape it appears on is the level 0 backup, and it could be too far out of date to be useful. By not overwriting the daily level 9 tapes frequently, you can minimize this problem. The syntax for the ufsdump command is as follows: /usr/sbin/ufsdump <options> <arguments> <files-to-dump> The options to the ufsdump command are described in Table 7.11.
Table 7.12 describes the options and arguments for the ufsdump command. The ufsdump command uses these options by default: ufsdump 9uf /dev/rmt/0 <files-to-back-up> ufsdump ExamplesThe following examples illustrate the use of the ufsdump command. The following is an example of a full backup of the /users file system: ufsdump 0ucf /dev/rmt/0 /users DUMP: Writing 63 Kilobyte records DUMP: Date of this level 0 dump: Thu Jul 25 10:43:25 2002 DUMP: Date of last level 0 dump: the epoch DUMP: Dumping /dev/rdsk/c0t1d0s0 (pyramid1:/users) to /dev/rmt/0. DUMP: Mapping (Pass I) [regular files] DUMP: Mapping (Pass II) [directories] DUMP: Estimated 10168 blocks (4.96MB). DUMP: Dumping (Pass III) [directories] DUMP: Dumping (Pass IV) [regular files] DUMP: Tape rewinding DUMP: 10078 blocks (4.92MB) on 1 volume at 107 KB/sec DUMP: DUMP IS DONE If you want to see how much space a backup is going to require, you issue the following command: ufsdump S <filesystem> The estimated number of bytes needed on tape to perform the level 0 backup is displayed. In the following example, the local /export/home file system on a Solaris 10 system is backed up to a tape device on a remote Solaris 10 system called sparc1: ufsdump 0ucf sparc1:/dev/rmt/0 /export/home DUMP: Date of this level 0 dump: Thu Jul 25 10:43:25 2002 DUMP: Date of last level 0 dump: the epoch DUMP: Dumping /dev/rdsk/c0t3d0s7 (/export/home) to /dev/rmt/0 \ on host sparc1 DUMP: mapping (Pass I) [regular files] DUMP: mapping (Pass II) [directories] DUMP: estimated 19574 blocks (9.56MB) DUMP: Writing 63 Kilobyte records DUMP: dumping (Pass III) [directories] DUMP: dumping (Pass IV) [regular files] DUMP: level 0 dump on Thu Jul 25 10:43:25 2002 DUMP: Tape rewinding DUMP: 19574 blocks (9.56MB) on 1 volume DUMP: DUMP IS DONE In this example, the -u option is used with the ufsdump command. This causes ufsdump to make an entry into the /etc/dumpdates file, which records the file system that was backed up, the level of the last backup, and the day, date, and time of the backup. Here's an example of looking into the /etc/dumpdates file: more /etc/dumpdates The system responds with the following: /dev/rdsk/c0t0d0s7 0 Mon Mar 25 10:47:46 2005 /dev/rdsk/c0t0d0s6 0 Mon Mar 25 10:48:04 2005 When incremental backups are made by using ufsdump, the ufsdump command consults the /etc/dumpdates file to find the date of the most recent backup at the next lower level. ufsdump then copies all files modified or added since the date of that lower-level backup. You can also determine whether backups are being done by viewing the contents of the /etc/dumpdates file. If a backup fails, it is not recorded in /etc/dumpdates. Another useful example is using ufsdump to copy the contents of one file system to another. In the section "The dd Utility," you learned how to copy data from one disk to another, but only when the disk geometry is exactly the same for each disk. In other words, dd works when you want to copy a 4GB disk to another 4GB disk. But if you want to replace an older 4GB disk with a new 18GB disk, you should not use dd to copy the data; a better option is to use ufsdump. Moving data from disk to tape and then back to disk again can be time consuming. Here's a way to move data directly to that file system by using ufsdump without going to tape: ufsdump 0f - /export/home | (cd /data; ufsrestore -xf - ) In this example, all data in the /export/home file system is copied to the /data file system. Instead of specifying a tape device, this example specifies a - (hyphen). The hyphen dumps the data to standard output and restores the data from standard input rather than from a file or device. This creates and extracts the dump file in memory, speeding up the entire process. The ufsrestore UtilityObjective: Explain how to perform UFS restores and special case recoveries. Tip Restoring File Systems You need to understand each step described in Step by Step 7.1 and Step by Step 7.2, along with the order in which each step is performed. Also, you need to understand what the restoresymtable file is used for. These topics are likely to be on the exam. The ufsrestore command copies files from backups created using the ufsdump command. As root, you can use ufsrestore to reload an entire file system from a level 0 dump and any incremental dumps that follow it, or to restore one or more single files from any dump tape. ufsrestore restores files with their original owner, last modification time, and mode (permissions). The syntax for the ufsrestore command is as follows: ufsrestore <options> <arguments> <filename(s)> The options for the ufsrestore command are described in Table 7.13.
Table 7.14 describes some of the most common options and arguments for the ufsrestore command.
For a full listing of options for the ufsrestore command, see the Solaris man pages. Table 7.15 lists the commands that can be used with ufsrestore when you're using interactive mode (that is, ufsrestore -i). ufsrestore ExamplesThe following examples illustrate how to restore data from a tape by using ufsrestore. You can use the ufsrestore command to display the contents of a tape: ufsrestore tf /dev/rmt/0 2 . 4249 ./users 12400 ./users/bill 12401 ./users/bill/.login 12402 ./users/bill/.cshrc 12458 ./users/bill/admin 12459 ./users/bill/junk You can use ufsrestore to restore a file from a backup that was created using ufsdump: ufsrestore f /dev/rmt/0 filename You can restore entire directories from a remote drive located on the system called sparc1 by adding sparc1: to the front of the tape device name, as illustrated in the following example: ufsrestore rf sparc1:/dev/rmt/0 filename Occasionally, a file system becomes so damaged that you must completely restore it from a backup. If you have faithfully backed up file systems, you can restore them to the state of the last backup. The first step in recovering a file system is to delete everything in the damaged file system and re-create the file system by using the newfs command. To recover a damaged file system, follow the procedure described in Step by Step 7.1. Recovering the Root (/) or /usr File SystemSometimes a careless administrator with root access accidentally deletes part or all of the root or /usr file system. Other times the file system can become unusable because of a faulty disk drive or a corrupted file system. You can follow the procedure described in Step by Step 7.2 if you ever need to recover the root or /usr file system.
The following example is an actual session that restores the root (/) file system from tape device /dev/rmt/0 to Small Computer System Interface (SCSI) disk target 3, slice 0, on controller 0: # mount /dev/dsk/c0t3d0s0 /mnt # cd /mnt # devfsadm -c tape # ufsrestore rf /dev/rmt/0 Note devfsadm The devfsadm command with the -c tape option creates the /dev entries for the tape drive only. It creates links in /dev/rmt to the actual tape device special files. The devfsadm command is covered in Chapter 1. Files are restored from tape. When this is complete, you are returned to a shell prompt. You can then remove the restoresymtable file, unmount the file system, and use fsck on the device: # rm restoresymtable # cd / # umount /mnt # fsck /dev/rdsk/c0t3d0s0 The system displays the fsck passes as the file system is checked: # installboot /usr/platform/'uname -i'/lib/fs/ufs/bootblk /dev/rdsk/c0t3d0s0 # ufsdump 0uf /dev/rmt/0 /dev/rdsk/c0t3d0s0 # shutdown -y -g0 -i0 The system is halted. At the ok prompt, you perform a reconfiguration reboot as follows: boot -r Performing a reconfiguration reboot ensures that all devices connected to the system have been configured properly in the kernel and in the /dev and /devices directories. Additional Notes About Restoring FilesWhen you restore files in a directory other than the root directory of the file system, ufsrestore re-creates the file hierarchy in the current directory. For example, if you restore to /home files that were backed up from /users/bcalkins/files, the files are restored in the directory /home/users/bcalkins/files. When you restore individual files and directories, it's a good idea to restore them to a temporary directory such as /var/tmp. After you verify that you've retrieved the correct files, you can move them to their proper locations. You can restore individual files and directories to their original locations; however, if you do so, you should be sure that you do not overwrite newer files with older versions from the backup tape. You should not forget to make regular backups of your operating system. Losing all the customization you dosuch as adding user accounts, setting up printers, and installing application softwarewould be disastrous. Whenever you make modifications that affect the root (/),/usr, /opt, or other operating system directories, you should bring down the system into single-user mode and perform a level 0 dump. |