Restoring Db2 from an offline backup

If you created an offline backup, you can use a provided script file to restore Db2 backup images (including different form factor or version), or you can use restore commands when you are restoring from a Db2 backup in IBM Software Hub at a similar version.

Restoring Db2 from an offline backup by using the restore script

You can use a provided script file to restore Db2 backup images, with or without encryption.

The restore script can restore the following backups:

  • An encrypted backup
  • An unencrypted backup
  • An external Db2 database from the same or different form factor (such as a bare metal Db2 deployment within the same platform family)
  • The backup from an earlier Db2 version

The following sections provide usage information for the db_restore_extdb.sh script, and details on using the script to restore with different options.

Note: You can use the script to restore only full offline backup images. If you are using incremental or online backup images, use the native Db2 restore commands.

Syntax

Read syntax diagramSkip visual syntax diagram /db2u/scripts/db_restore_extdb.sh --bkp-dirdirectory--bkp-tarfilename --bkp-timestamptimestamp --dbnamedbname--dblistlist --keystore-dirdirectory--keystore-tarfilename --drop-default-db --replace --cleanup --verbose --help

Options

--bkp-dir directory
Specifies the directory where the backup images are located. Choose this option or the --bkp-tar option.
--bkp-tar filename
Specifies a .tar file that contains the backup images.
--bkp-timestamp timestamp
The timestamp of the backup image to use for the restore.
--dbname dbname
The name of the database to use when you restore the backup image.
--dblist
A comma-separated list of database names to use when you restore the backup image.
--keystore-dir directory

The directory that contains the backup native encryption keystore. This directory must contain only the keystore files. Choose this option or the --keystore-tar option.

--drop-default-db
Drops the existing databases.
--replace
Replaces the existing database.
--keystore-tar
Specifies a .tar file that contains the backup native encryption keystore.
--cleanup
Specifies to clean up all backup images.
--verbose
Use this flag to view explicit commands that the script is performing or to view errors.
--help
Use this flag to display help for the tool.

Restoring an encrypted backup

To restore an encrypted backup, follow these steps:

  1. Save the Db2 database backup images and the native encryption keystore in a single location. For example, /mnt/blumeta0/db2/backup/db2ubkp, which is equivalent to ${BACKUPDIR}/db2ubkp.
  2. The Db2 instance owner or group must have read/write access to the backup directory where the backup images and keystore files are saved. You can set the required mode-bits and permissions by running the following command:
    chmod 755 ${BACKUPDIR}/db2ubkp
    chown db2inst1:db2iadm1 ${BACKUPDIR}/db2ubkp
  3. Run the following command to restore the backup:
    db_restore_extdb --bkp-dir ${BACKUPDIR}/db2ubkp --dbname dbname --bkp-timestamp timestamp --replace --keystore-dir ${BACKUPDIR}/db2ubkp

    Replace dbname with the case-sensitive database name, and timestamp with the timestamp of the backup image. You can obtain the output log for the restore command by running cat ${SUPPORTDIR}/db_restore_extdb.log.

    Note: If your backup was copied from a different instance or remote cluster, you must use the --drop-default-database parameter, instead of the --replace parameter.

Restoring an unencrypted backup

To restore an unencrypted backup, follow these steps:

  1. Save the Db2 database backup images in a single location. For example, /mnt/blumeta0/db2/backup/db2ubkp, which is equivalent to ${BACKUPDIR}/db2ubkp.
  2. The Db2 instance owner or group must have read/write access to the backup directory where the backup images and keystore files are saved. You can set the required mode-bits and permissions by running the following command:
    chmod 755 ${BACKUPDIR}/db2ubkp
    chown db2inst1:db2iadm1 ${BACKUPDIR}/db2ubkp
  3. Run the following command to restore the backup:
    db_restore_extdb --bkp-dir ${BACKUPDIR}/db2ubkp --dbname dbname --bkp-timestamp timestamp --replace

    Replace dbname with the case-sensitive database name, and timestamp with the timestamp of the backup image. You can obtain the output log for the restore command by running cat ${SUPPORTDIR}/db_restore_extdb.log.

Restoring an external database

The backup image (and keystore, if encrypted) can also be restored from an external source. For example, you might want to reinitialize Db2 by using a database from an on-premises deployment.

You can use the --dbname dbname option to set the database name to the same as the source in this scenario.

Restoring Db2 by using commands

This process restores a database that was backed up by using the db2 backup command. The restored database starts back in the same state that it was in when the backup was made.

Before you begin

  • You must have an existing Db2 backup that is accessible on the IBM Cloud Pak for Data System.
  • You must identify the Db2 catalog node and data nodes and have an SSH connection open to each node.
Note: The following commands use the environment variable that contains the default database name: ${DBNAME}. If you have multiple databases, update the commands or variable value.

About this task

The restore script is the preferred strategy to restore because it provides:

  • A simple flow.
  • A restore strategy for full offline backup images.
  • Native support for restoring encrypted databases.

The commands in this procedure are illustrative only and are only applicable when you are restoring from a Db2 backup in IBM Software Hub at a similar version. Further commands are required for restoring an encrypted database. For most restore operations, the best practice is to use the procedure in Restoring Db2 from an offline backup by using the restore script.

Note: If your back up and restore strategy is based on full offline backup images, you can use a script for restoring instead of using the following procedure. These commands are only applicable when you are restoring from a Db2 backup in IBM Software Hub at a similar version. Otherwise, other commands are required, as detailed in Restoring data. For most restore operations, the best practice is to use the procedure in Restoring Db2 from an offline backup by using the restore script.

For a Db2 deployment, the db2 restore command can be started only from the catalog node, but each available multiple logical node (MLN) participates in the restore operation.

Restriction: A full database restore operation can be run only in offline mode.

Procedure

  1. Exec into the Db2 pod.
  2. Temporarily disable the built-in HA:
    sudo wvcli system disable -m "Disable HA before Db2 maintenance" 
  3. From a bash shell on each of the nodes specified in the db2nodes.cfg file, switch to the database instance owner. Confirm that the directory that contains the database backup exists and is mounted. For example:
    su - db2inst1
    cd /backup_dir/backup_nnn

    Where backup_dir is the directory that you create to hold backup images and nnn is an incremental value that was used to put each backup in a separate directory. The directory that is shown is an example. The backups can be in any directory on the cluster that is shared by the container nodes and has sufficient space to hold the backups. If you restore a backup from a different directory location, alter these commands as necessary.

  4. Connect to the database:
    db2 connect to ${DBNAME}
  5. On the catalog node, find all the applications that are connected to Db2:
    db2 list applications

    You can either stop all the connections by closing the applications, or you can enter the following command to disconnect all connections:

    db2 force application all
  6. Issue the terminate database command:
    db2 terminate
  7. Stop the database:
    db2stop force

    Ensure that the command completes on all nodes.

  8. Ensure that all Db2 interprocess communications are cleaned for the instance:
    ipclean -a
  9. Turn off all communications to the database by setting the value of the DB2COMM variable to null:
    db2set -null DB2COMM
  10. Restart the database in restricted access mode:
    db2start admin mode restricted access
  11. Run the restore operation. On the catalog node, run the following command:
    db2 RESTORE DATABASE ${DBNAME} FROM backup_dir TAKEN AT backup_image_timestamp INTO ${DBNAME} REPLACE EXISTING WITHOUT ROLLING FORWARD

    Where backup_dir is the full path to the directory where your backup images are located, and backup_image_timestamp is the associated timestamp on those backup image files.

  12. Stop the database:
    db2stop force

    Ensure that the command completes on all nodes.

  13. Ensure that all Db2 interprocess communications are cleaned for the instance:
    ipclean -a
  14. Reinitialize the Db2 communication manager to accept database connections:
    db2set DB2COMM=TCPIP,SSL
  15. Restart the database for normal operation:
    db2start

    The offline database restore operation is complete.

  16. Activate the database:
    db2 activate db ${DBNAME}
  17. Re-enable the Wolverine high availability monitoring process:
    wvcli system enable -m "Enable HA after Db2 maintenance"
  18. Connect to the database:
    db2 connect to ${DBNAME}

What to do next

For more information about advanced command options, see the RESTORE DATABASE command