DB2 Database Backup and Restore Using the Command Line

DB2 Database Backup and Restore Using the Command Line

I was recently working with an off-shore developer, trying to get their WebSphere Commerce database updated to a state that it can be used with the current state of the WebSphere Commerce project I’m currently working on. They were having permission issues when attempting to run a database backup and restore through the IBM Control Center for DB2 on Windows. As a result of the need to control the user credentials that will be backing up and restoring the database, I put together the following commands that can be ran on the command line to perform database backup and restore operations, and figured I’d share these with everyone.

The following commands are designed to be run from the DB2 Command Line Processor. If you wish to run these from the DB2 Command Window or Windows Shell, you will need to prefix each line with the DB2 directive (db2).

To backup your database, run the following commands in order substituting values where appropriate. The values that need substitution are surround with { }. The following variables are used in the backup commands.

  • {DBNAME}: The database name to be connected to and backed up.
  • {USER}: A database user with appropriate permissions.
  • {PASSWORD}: The database user’s password.
  • {C:\}: This is the path you want the backup file output to.
CONNECT TO {DBNAME} USER {USER} USING {PASSWORD};
QUIESCE DATABASE IMMEDIATE FORCE CONNECTIONS;
CONNECT RESET;
BACKUP DATABASE {DBNAME} USER {USER} USING {PASSWORD} TO "{C:\}" WITH 2 BUFFERS BUFFER 1024 PARALLELISM 1 COMPRESS WITHOUT PROMPTING;
CONNECT TO {DBNAME} USER {USER} USING {PASSWORD};
UNQUIESCE DATABASE;
CONNECT RESET;

To restore your database, you’ll want to run the following command substituting values where appropriate. Once again, the values that need substitution are surround with { }. The following variables are used in the backup commands.

  • {DBNAME}: The database name to be connected to and backed up.
  • {USER}: A database user with appropriate permissions.
  • {PASSWORD}: The database user’s password.
  • {C:\}: This is the full path to where your database is currently backed up.
  • {20121116165148}: This is the timestamp of your database backup. The format is: YYYYMMDDHHMMSS.
  • {C:}: This is the target database location. Note: On Windows operating systems, when using this parameter, specify only the drive letter. If you specify a path, an error is returned.
RESTORE DATABASE {DBNAME} USER {USER} USING {PASSWORD} FROM "{C:\}" TAKEN AT {20121116165148} TO "{C:}" INTO {DBNAME} WITH 2 BUFFERS BUFFER 1024 PARALLELISM 1 WITHOUT PROMPTING;

If you have any changes, recommendations, or comments, please let me know by using the comment form below.

Author: daharveyjr

I’m a solution architect responsible for the design, development, implementation, testing, and maintenance of e-commerce operations and applications using the Hybris and WebSphere Commerce product suites and other web technologies such as Java, J2EE/JEE, Spring, PHP, WordPress and more. Twitter | Facebook | LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *