Db2 简明教程
DB2 - Backup and Recovery
本章介绍数据库的备份和恢复方法。
This chapter describes backup and restore methods of database.
Introduction
备份和恢复方法旨在保证我们信息的安全性。在命令行界面 (CLI) 或图形用户界面 (GUI) 中使用备份和恢复实用程序,您可以在 DB2 UDB 中备份或恢复数据库的数据。
Backup and recovery methods are designed to keep our information safe. In Command Line Interface (CLI) or Graphical User Interface (GUI) using backup and recovery utilities you can take backup or restore the data of databases in DB2 UDB.
Logging
日志文件包含错误日志,这些错误日志用于从应用程序错误中恢复。日志保留数据库中更改的记录。如下所述,日志记录有两种类型:
Log files consist of error logs, which are used to recover from application errors. The logs keep the record of changes in the database. There are two types of logging as described below:
Circular logging
这是一种方法,即在需要分配新事务日志文件时,覆盖旧的事务日志,从而擦除日志文件序列并重新使用它们。您只允许在脱机模式下进行完全备份。即,数据库必须脱机才能进行完全备份。
It is a method where the old transaction logs are overwritten when there is a need to allocate a new transaction log file, thus erasing the sequences of log files and reusing them. You are permitted to take only full back-up in offline mode. i.e., the database must be offline to take the full backup.
Archive logging
此模式支持使用称为向前恢复的日志文件进行在线备份和数据库恢复。可以通过将 logretain 或 userexit 设置为 ON,将备份模式从循环更改为归档。对于归档日志记录,备份设置数据库需要一个对 DB2 进程可写的目录。
This mode supports for Online Backup and database recovery using log files called roll forward recovery. The mode of backup can be changed from circular to archive by setting logretain or userexit to ON. For archive logging, backup setting database require a directory that is writable for DB2 process.
Backup
使用 Backup 命令,您可以复制整个数据库。此备份副本包括数据库系统文件、数据文件、日志文件、控制信息等。
Using Backup command you can take copy of entire database. This backup copy includes database system files, data files, log files, control information and so on.
可以同时在脱机和联机时进行备份。
You can take backup while working offline as well as online.
Offline backup
Syntax: [列出活动应用程序/数据库]
Syntax: [To list the active applications/databases]
db2 list application
Output:
Output:
Auth Id Application Appl. Application Id
DB # of
Name Handle
Name Agents
-------- -------------- ---------- ---------------------
----------------------------------------- -------- -----
DB2INST1 db2bp 39
*LOCAL.db2inst1.140722043938
ONE 1
Syntax: [强制应用程序使用应用程序。已处理 ID]
Syntax: [To force application using app. Handled id]
db2 "force application (39)"
Output:
Output:
DB20000I The FORCE APPLICATION command completed
successfully.
DB21024I This command is asynchronous and may not
be effective immediately.
Syntax: [要终止数据库连接]
Syntax: [To terminate Database Connection]
db2 terminate
Syntax: [要停用数据库]
Syntax: [To deactivate Database]
db2 deactivate database one
Syntax: [要获取备份文件]
Syntax: [To take the backup file]
db2 backup database <db_name> to <location>
Example:
Example:
db2 backup database one to /home/db2inst1/
Output:
Output:
Backup successful. The timestamp for this backup image is :
20140722105345
Online backup
首先,您需要将模式从 Circular logging 更改为 Archive Logging 。
To start, you need to change the mode from Circular logging to Archive Logging.
Syntax: [要检查数据库是否使用循环记录还是存档记录]
Syntax: [To check if the database is using circular or archive logging]
db2 get db cfg for one | grep LOGARCH
Output:
Output:
First log archive method (LOGARCHMETH1) = OFF
Archive compression for logarchmeth1 (LOGARCHCOMPR1) = OFF
Options for logarchmeth1 (LOGARCHOPT1) =
Second log archive method (LOGARCHMETH2) = OFF
Archive compression for logarchmeth2 (LOGARCHCOMPR2) = OFF
Options for logarchmeth2 (LOGARCHOPT2) =
在上述输出中,高亮显示的值 [logarchmeth1 和 logarchmeth2] 处于关闭模式,这意味着当前数据库处于“循环记录”模式。如果您需要使用“存档记录”模式,则需要更改或在配置文件中存在的变量 logarchmeth1 和 logarchmeth2 中添加路径。
In the above output, the highlighted values are [logarchmeth1 and logarchmeth2] in off mode, which implies that the current database in “CIRCULLAR LOGGING” mode. If you need to work with ‘ARCHIVE LOGGING’ mode, you need to change or add path in the variables logarchmeth1 and logarchmeth2 present in the configuration file.
Updating logarchmeth1 with required archive directory
Syntax: [要创建目录]
Syntax: [To make directories]
mkdir backup
mkdir backup/ArchiveDest
Syntax: [要为文件夹提供用户权限]
Syntax: [To provide user permissions for folder]
chown db2inst1:db2iadm1 backup/ArchiveDest
Syntax: [要更新配置 LOGARCHMETH1]
Syntax: [To update configuration LOGARCHMETH1]
db2 update database configuration for one using LOGARCHMETH1
'DISK:/home/db2inst1/backup/ArchiveDest'
您可以进行离线备份以确保安全,激活数据库并连接到数据库。
You can take offline backup for safety, activate the database and connect to it.
Syntax: [要执行联机备份]
Syntax: [To take online backup]
db2 backup database one online to
/home/db2inst1/onlinebackup/ compress include logs
Output:
Output:
db2 backup database one online to
/home/db2inst1/onlinebackup/ compress include logs
使用以下命令验证备份文件:
Verify Backup file using following command:
Syntax:
Syntax:
db2ckbkp <location/backup file>
Example:
Example:
db2ckbkp
/home/db2inst1/ONE.0.db2inst1.DBPART000.20140722112743.001
列出备份文件的历史记录
Listing the history of backup files
Syntax:
Syntax:
db2 list history backup all for one
Output:
Output:
List History File for one
Number of matching file entries = 4
Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log
Backup ID
-- --- ------------------ ---- --- ------------ ------------
--------------
B D 20140722105345001 F D S0000000.LOG S0000000.LOG
------------------------------------------------------------
----------------
Contains 4 tablespace(s):
00001 SYSCATSPACE
00002 USERSPACE1
00003 SYSTOOLSPACE
00004 TS1
------------------------------------------------------------
----------------
Comment: DB2 BACKUP ONE OFFLINE
Start Time: 20140722105345
End Time: 20140722105347
Status: A
------------------------------------------------------------
----------------
EID: 3 Location: /home/db2inst1
Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log
Backup ID
-- --- ------------------ ---- --- ------------ ------------
--------------
B D 20140722112239000 N S0000000.LOG S0000000.LOG
------------------------------------------------------------
-------------------------------------------------------------
-------------------------------
Comment: DB2 BACKUP ONE ONLINE
Start Time: 20140722112239
End Time: 20140722112240
Status: A
------------------------------------------------------------
----------------
EID: 4 Location:
SQLCA Information
sqlcaid : SQLCA sqlcabc: 136 sqlcode: -2413 sqlerrml: 0
sqlerrmc:
sqlerrp : sqlubIni
sqlerrd : (1) 0 (2) 0 (3) 0
(4) 0 (5) 0 (6) 0
sqlwarn : (1) (2) (3) (4) (5) (6)
(7) (8) (9) (10) (11)
sqlstate:
Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log
Backup ID
-- --- ------------------ ---- --- ------------ ------------
--------------
B D 20140722112743001 F D S0000000.LOG S0000000.LOG
------------------------------------------------------------
----------------
Contains 4 tablespace(s):
00001 SYSCATSPACE
00002 USERSPACE1
00003 SYSTOOLSPACE
00004 TS1
-------------------------------------------------------------
----------------
Comment: DB2 BACKUP ONE OFFLINE
Start Time: 20140722112743
End Time: 20140722112743
Status: A
-------------------------------------------------------------
----------------
EID: 5 Location: /home/db2inst1
Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log
Backup ID
-------------------------------------------------------------
----------------
R D 20140722114519001 F
20140722112743
------------------------------------------------------------
----------------
Contains 4 tablespace(s):
00001 SYSCATSPACE
00002 USERSPACE1
00003 SYSTOOLSPACE
00004 TS1
------------------------------------------------------------
----------------
Comment: RESTORE ONE WITH RF
Start Time: 20140722114519
End Time: 20140722115015
Status: A
------------------------------------------------------------
----------------
EID: 6 Location:
Restoring the database from backup
要从备份文件恢复数据库,您需要按照给定的语法进行操作:
To restore the database from backup file, you need to follow the given syntax:
Syntax:
Syntax:
db2 restore database <db_name> from <location>
taken at <timestamp>
Example:
Example:
db2 restore database one from /home/db2inst1/ taken at
20140722112743
Output:
Output:
SQL2523W Warning! Restoring to an existing database that is
different from
the database on the backup image, but have matching names.
The target database
will be overwritten by the backup version. The Roll-forward
recovery logs
associated with the target database will be deleted.
Do you want to continue ? (y/n) y
DB20000I The RESTORE DATABASE command completed successfully.
向前滚动日志目录中位于的所有日志,包括磁盘驱动器故障前的最新更改。
Roll forward all the logs located in the log directory, including latest changes just before the disk drive failure.
Syntax:
Syntax:
db2 rollforward db <db_name> to end of logs and stop
Example:
Example:
db2 rollforward db one to end of logs and stop
Output:
Output:
Rollforward Status
Input database alias = one
Number of members have returned status = 1
Member ID = 0
Rollforward status = not pending
Next log file to be read =
Log files processed = S0000000.LOG -
S0000001.LOG
Last committed transaction = 2014-07-22-
06.00.33.000000 UTC
DB20000I The ROLLFORWARD command completed successfully.