18.2. The backup, restore, rollforward, and recover CommandsIn order to be able to recover your DB2 database after a serious failure, you are going to need to have a solid backup and recovery plan. This plan is built around the backup and restore commands, which then enable you to do different types of recoveries. 18.2.1. The backup CommandThe DB2 backup command (as its name suggests) backs up a database (or tablespace) to one or more devices or directories on the DB2 server machine.
If you enabled archive logging for the database you are trying to back up, you can perform an online backup (that is, you can back up the database when applications are connected to it). With archive logging, you also don't have to back up the whole database. Rather, the backup process can be performed at the tablespace level. If you want to keep your enterprise database highly available, and you cannot afford large scheduled maintenance windows that render a database unreachable by consuming applications, you should consider using online backups. When performing the backup command, you can specify the following:
Here's an example of a backup command: C:> db2 backup db sample userid db2admin using password to c:\backup This command creates a full database backup of the database named sample to a directory named C:\backup. The database user db2admin with a password of password performs the backup command. As stated earlier, it is assumed that db2admin has the sysadm, sysctrl, or sysmaint database user authority. This backup command is performed offline (the default mode of operation). You can change it to an online backup by adding the word online just after the password phrase. (You must have archive logging enabled to do this.) Here's another example: C:> db2 backup db sample user db2admin using password tablespace(userspace1) online to c:\backup This command backs up only the userspace1 tablespace of the sample database, showcasing the ability to back up at the tablespace level. This is highly useful when a database has a subset of tablespaces that change more frequently than the rest. It is important to note that you cannot back up temporary tablespaces using this command. It is a good practice to back up related tablespaces together. Examples of such related tablespaces are tablespaces containing tables that have referential constraints between the tables. Another example of related tablespaces involves a typical topology in which the indexes of a table and LOBs are housed in different tablespaces from the main table data. Going back to our example, the tablespace backup is performed online, so it is assumed that archive logging has been enabled for the sample database. After issuing such a backup command, you should receive a message similar to the following, telling you that the backup operation was successful: Backup successful. The timestamp for this backup image is: 20061119181253. 18.2.1.1. Backup levelsWhen running the backup command, you can opt for a full, incremental, or delta backup. A full backup, as its name implies, contains all of the data of the database or tablespace being backed up. An incremental backup (also called a cumulative backup) makes a copy of all database data that has changed since the most recent successful full backup. On the other hand, a delta backup copies all database data that has changed since the most recent successful backup (full or other). Incremental and delta backups are therefore reliant on previous backup images and cannot be used by themselves to restore a database. This reliance on other, previously created, backup images makes it very important to save all the backup images needed to perform a database recovery. 18.2.1.2. Backup path and filenaming conventionDB2 automatically determines the pathname and filename for backup files. The generation of this name is far from random; it follows a strict naming convention. Dissecting the name of a database backup path and file can let you know a lot about the DB2 backup that created the backup file. The following information is contained in the path and/or name of a backup file:
In Windows, this naming convention is represented by a pathname and a filename. In Unix, the naming convention is represented only by the filename. On Windows, a four-level subdirectory tree houses the backup file: DB_alias.Type\Inst_name\NODEnnnn\CATNnnnn\yyyymmdd\hhmmss.Seq_num A backup filename in Windows might be: SAMPLE.0\DB2INST\NODE0000\CATN0000\20060227\145655.001 For Linux and Unix, rather than dealing with a four-level subdirectory tree, all the information is built into the filename itself: DB_alias.Type.Inst_name.NODEnnnn.CATNnnnn.timestamp.Seq_num Using the same parameters as the Windows example, our Linux or Unix filename example would be: SAMPLE.0.DB2INST.NODE0000.CATN0000.20060227.145655.001
18.2.1.3. Discovering the history of your backup operationsBackup, restore, and rollforward operations performed on a database are logged in a recovery history file named db2rhist.asc. Each database has its own recovery history file, located in the same directory as the database. The list history command shows the backup, restore, and rollforward operations performed on a database. For example, to learn which backup and restore operations have been performed on the sample database, you can use this command: C:> db2 list history backup all for db sample The report generated by issuing this command contains a symbol indicating the operation performed (B for backup, R for restore). Here is a sample of the output from the db2 list history command: List History File for sample Number of matching file entries = 1 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log Backup ID -- --- ------------------ ---- --- ------------ ------------ -------------- B D 20060409190953001 F D S0000000.LOG S0000000.LOG ---------------------------------------------------------------------------- If the operation was a backup, a symbol indicates the type of backup: F Offline Backup N Online Backup I - Incremental Offline Backup O - Incremental Online Backup D - Delta Offline Backup E - Delta Online Backup The backup report also tells you which tablespaces were backed up as well as the location of the backup image. This information can come in quite handy when trying to recover your database. To learn which rollforward operations have been performed on the sample database, use this command: C:> db2 list history rollforward all for db sample The report generated by issuing this command contains an F, indicating that a rollforward operation was performed. The type column of the report indicates whether the rollforward was performed to the end of the logs (indicated by an E) or to a point in time (indicated by a P). 18.2.1.4. Automatic maintenanceDB2 has components with autonomic features and automated backup utilities. These components include database configuration parameters, the health monitor and its indicators, and a number of GUI interfaces (including the Health Center, the Web Health Center, the Task Center, and the Configure Automatic Maintenance wizard). If you are a power user, you can skip the GUIs and configure automatic backup using the appropriate command-line processor (CLP) commands. See the IBM DB2 Universal Database System Monitor Guide and Reference for detailed instructions. This section provides a high-level overview. The DB2 health monitor runs on the database server. The health monitor provides the functionality required for automatic database backup and maintenance. The health monitor gathers information about the health of the system using health indicators. Using these indicators does not impose a performance penalty. They are not the same thing as snapshot monitor switches. Health indicators exist at the instance, database, tablespace, and tablespace container levels. The DBA configures health indicators using the Health Center, the Web Health Center, the CLP, or APIs. DB2 monitors these indicators and can take actions identified by the DBA. These actions can include:
Health indicators fall into four categories:
The db.db_backup_req health indicator is a state-based, database-level indicator. This indicator determines when a database backup is required based on either the time elapsed or the amount of data changed since the last backup. The auto_db_backup database configuration parameter must be set to on for automatic backups to occur. Automatic database backups can be either offline (cold) or online (hot). The db.tb_reorg_req and db.tb_runstats_req indicators are set after a database is restored. Here is the recommended procedure for restoring a DB2 database:
Following this procedure helps enhance the performance of your applications. Reorg and runstats can be run at any time in offline (cold) or online (hot) mode. They do not require a database restore operation. However, you should include these steps in your database restore or recovery operating procedures. A backup policy specifies automated maintenance behavior to ensure that the database is backed up regularly. The backup policy for a database is created automatically when the DB2 Health Monitor first runs. You can still perform manual backup operations when automatic maintenance is configured. DB2 performs automatic backup operations only if they are required. You define the time periods, or windows, for online and offline maintenance with the DB2 Health Center. DB2 then determines the need to perform a backup operation based on one or more of the following criteria:
You configure the requested time or number of log pages between backups, the backup media, and the backup type (online or offline) using the Configure Automatic Maintenance wizard in the Control Center or Health Center. Automatic database backup can be enabled for either online (hot) or offline (cold) backup if the database is enabled for rollforward recovery (archive logging); otherwise, only offline backup is available. Automatic database backup supports disk, tape, Tivoli® Storage Manager (TSM), and vendor DLL media types. If you select Backup to Disk, the automatic backup feature regularly deletes backup images from the directory specified in the Configure Automatic Maintenance wizard. Only the most recent backup image is guaranteed to be available at any given time. This directory should be kept exclusively for the automatic backup feature and not be used to store other backup images. Offline backup, restore, reorg, and runstats restrict access to the database, as does online reorg and, to a certain extent, online restore. Offline database backups and table and index reorganization are run in the offline maintenance time period that you define. These features run to completion even if they go beyond the time period specified. DB2's internal scheduling mechanism learns over time and estimates job completion times. If the offline time period you define is too small for a particular database backup or reorganization activity, the scheduler does not start the job the next time around. Instead, the health monitor notifies you if you need to increase the offline-maintenance time period. You can set up automated backup, reorg, and runstats using the DB2 Health Center:
The Health Center now shows two health indicators: Database backup required and Update statistics required. You should verify that automatic maintenance parameter settings for the database are enabled, substituting db_name with the name of the database for which you have created the automatic maintenance policy:
If the database parameters are set to off, you can enable them with the following commands, which work on either platform (shown here on Windows): C:> db2 connect to db_name C:> db2 update db cfg using auto_maint on C:> db2 update db cfg using auto_db_backup on C:> db2 update db cfg using auto_tbl_maint on C:> db2 update db cfg using auto_runstats on C:> db2 update db cfg using auto_reorg on C:> db2 connect reset To reset the Database Backup Required Indicator, use this command: C:> db2 backup db db_name You have now automated the backup and maintenance of your database. Refer to the IBM DB2 Universal Database System Monitor Guide and Reference for information on setting other health indicators. 18.2.1.5. Using db2lookIn addition to performing a backup, it's a good idea to run the db2look command against your database and save that output. db2look will display the statements necessary to reproduce the database objects of a database. The command below would create such an output for the sample database, and send it to path/file. db2look -d sample -a -m -l -x -xd -f -o path/file The output stored in path/file can come in very handy when recovering a DB2 database. It's a good idea to run this as soon as you create a database, or any time you make any structural changes to the database. Then place the output of this command someplace where you can easily retrieve it during a recovery operation. 18.2.2. Recovery TypesBefore explaining the commands designed to recover a DB2 database, it's important to understand the different types of recoveries. DB2 UDB identifies the following three types of recoveries:
18.2.2.1. Crash recoveryCrash recovery ensures that the database is brought to a consistent state after a software failure or power outage. Fortunately, crash recovery in DB2 is quite simple, and nothing special has to be done to a database to prepare it for crash recovery. If you would rather have the database wait for you after a crash, you can turn off crash recovery by setting the autorestart database configuration parameter to off. If you change this setting, you must run the restart command to start the database after a crash: % db2 restart db sample The restart command effectively establishes a database connection. The database logs are then used to restore the database to a transaction-consistent state. Consequently, database changes made by committed transactions before the failure are made effective. On the other hand, rolled back transactions are rolled back in the database as are transactions that were in flight during the failure. At the end of the crash recovery, the DB2 database is restored to a transaction-consistent state. 18.2.2.2. Version recoveryIf a database is damaged beyond the point where crash recovery can repair it (for example, the loss of a container), it must be restored from backup. Since this restores the database to a previous version, this is referred to as version recovery. 18.2.2.3. Rollforward recoveryA version recovery restores the database to the state it was in at the time the database backup was performed. Changes made to the database after a backup was taken are lost unless rollforward recovery is enabled prior to the failure and a rollforward command is issued after the restore (see the section "Performing a Rollforward Recovery" later in this chapter). Accordingly, in practice it's common to see a restore command used in tandem with a rollforward command. DB2 8.2 introduced the recover command, which automatically performs a version recovery followed by a rollforward recovery. 18.2.3. The restore CommandA restore can be performed for a full database or a tablespace. It is important to note that a database restore must be performed offline whereas a tablespace restore (of any tablespace other than the one containing the system catalog tables) can be performed either online or offline. As mentioned earlier, an online tablespace restore is more desirable for enterprises wanting to minimize (if not eliminate) a database's downtime during the recovery of a database.
The following information can be specified with the restore command:
By default, if archive logging was enabled for a database, when you issue the restore command, the database is left in a rollforward pending state. The database or tablespace in a rollforward pending state cannot be used until it is brought out of this state by applying the rollforward command, as described later in this chapter. The rollforward command can account for the transactions that were committed after the backup occurred.
18.2.4. The rollforward CommandOnce a database has been restored with the restore command, it can be rolled forward with the db2 rollforward command. The command allows you to roll forward to the last transaction log found or to a particular point in time prior to the last transaction.
18.2.5. The recover CommandDB2 version 8.2 introduced the recover command. This command combines the restore and rollforward commands into a single command; thus it has some options from both. It has a few caveats, though:
To recover a database using the recover command, use this command: C:> db2 recover db DATABASE_NAME_OR_ALIAS This automatically uses the best available backup image as determined from the recovery history file (described in the next section) and recovers the database to the last transaction recorded in the transaction log. To recover to a particular point in time rather than to the end of logs, you can use this syntax: recover DB DATABASE_NAME_OR_ALIAS TO POINT_IN_TIME To recover the database sample to the point in time 2006-04-10-00.16.52, use the following command: C:> db2 recover db sample to 2006-04-10-00.16.52 The point in time is specified in local time, not UTC time. With the recover commands shown so far, the point in time must be contained in the current history file. If you need to restore to a point in time not contained in the current history file, explicitly point to an external history file. For example, to point to a history file archived in the directory /home/user/archives/, use this command: C:> db2 recover db sample to 2006-04-10-00.16.52 using history file (/home/user/archives/db2rhist.asc) |