当前位置:首页 > 数据库 > SQL其他

DB2数据库的备份与恢复

 

 

 

 

一、    查看数据库

[db2inst1@localhost ~]$ db2 list db directory

<img alt="技术分享" onload="if(this.width>650) this.width=650;" title="clip_image001" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; margin: 0px; padding-right: 0px" border="0" alt="clip_image001" src="http://s3.51cto.com/wyfs02/M02/92/B1/wKiom1kB0XbCka9nAAAWfTdf-3w456.png" "468" height="184" />

 

二、    创建测试数据库

$ db2 create database test

<img alt="技术分享" onload="if(this.width>650) this.width=650;" title="clip_image002" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; margin: 0px; padding-right: 0px" border="0" alt="clip_image002" src="http://s3.51cto.com/wyfs02/M02/92/AF/wKioL1kB0XjwDl2sAABBmnT7W4Y099.png" "490" height="550" />

 

三、    启动进入创建的测试库

$ db2 connect to test

<img alt="技术分享" onload="if(this.width>650) this.width=650;" title="clip_image003" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; margin: 0px; padding-right: 0px" border="0" alt="clip_image003" src="http://s3.51cto.com/wyfs02/M00/92/AF/wKioL1kB0XmRfIHSAAAPxb6GR1Q545.png" "424" height="138" />

 

 

l  列出激活的库

$ db2 list active databases

<img alt="技术分享" onload="if(this.width>650) this.width=650;" title="clip_image005" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; margin: 0px; padding-right: 0px" border="0" alt="clip_image005" src="http://s3.51cto.com/wyfs02/M01/92/B1/wKiom1kB0XuRhVAEAAA-8DeiQAM522.jpg" "554" height="143" />

 

l  查看表空间

$ db2 list tablespaces

 

<img alt="技术分享" onload="if(this.width>650) this.width=650;" title="clip_image007" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; margin: 0px; padding-right: 0px" border="0" alt="clip_image007" src="http://s3.51cto.com/wyfs02/M01/92/B1/wKiom1kB0X2jQPhoAABxfa4wkww982.jpg" "554" height="353" />

 

l  列出库中的表

$ db2 list tables

 

Table/View                      Schema          Type  Creation time            

------------------------------- --------------- ----- --------------------------

 

  0 record(s) selected.

 

l  创建表

$ db2 "create table student(id int,fname varchar(30),age int)"

 

l  赋予用户权限

db2 "grant use of tablespace  TABLESPACE_TEST to user XXX"

<img alt="技术分享" onload="if(this.width>650) this.width=650;" title="clip_image009" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; margin: 0px; padding-right: 0px" border="0" alt="clip_image009" src="http://s3.51cto.com/wyfs02/M01/92/AF/wKioL1kB0X_xE8mCAAA54knV_m0661.jpg" "554" height="114" />

 

l  向表中插入数据

$ db2 "insert into student values (1,‘Tom‘,22)"

 

l  查询表内容

<img alt="技术分享" onload="if(this.width>650) this.width=650;" title="clip_image010" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; margin: 0px; padding-right: 0px" border="0" alt="clip_image010" src="http://s3.51cto.com/wyfs02/M02/92/AF/wKioL1kB0YDTSkKkAAAKh0HlDOQ928.png" "492" height="131" />

 

 

四、    在线备份

备份指定库

$ db2 backup db TEST online to /opt/dbbak  include logs

 

如果报:

SQL2413N  Online backup is not allowed because the database is not recoverable

or a backup pending condition is in effect.

 

原因:数据库在线备份需要启用归档日志功能,刚装的数据库是没有启用的

 

备份到null

db2 backup db idsldap online to /dev/null

 

Backup successful. The timestamp for this backup image is : 20161228092925

 

增量备份:

/usr/bin/su - db2inst1  -c "db2 backup db FDBKDB61 online incremental use tsm >>/IBM/tsm/db2inst1/FDBKDB61_incbk.log 2>&1"

 

l  查看归档设置

$ db2 get db cfg for test|grep -i LOGARCHMETH

<img alt="技术分享" onload="if(this.width>650) this.width=650;" title="clip_image012" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; margin: 0px; padding-right: 0px" border="0" alt="clip_image012" src="http://s3.51cto.com/wyfs02/M00/92/B1/wKiom1kB0YLwIOO0AACD4grPDTY801.jpg" "554" height="121" />

 

l  设置归档路径

$ db2 update db cfg using logarchmeth1 disk:/opt/dbbak/arch

 

如果报

SQL1024N  A database connection does not exist.  SQLSTATE=08003

 

原因:需要备份数据库一次,然后在修改主日志文件和此日志文件的归档路径

 

解决方法:要连接数据库时要备份一下。

<img alt="技术分享" onload="if(this.width>650) this.width=650;" title="clip_image014" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; margin: 0px; padding-right: 0px" border="0" alt="clip_image014" src="http://s3.51cto.com/wyfs02/M00/92/AF/wKioL1kB0YSj9wGvAAByxS42UQk554.jpg" "554" height="177" />

 

 

解决方法:(需要离线备份一次,再连接数据库)

 

l  显示应用状态

$ db2 list applications

 

l  停掉数据库应用

$ db2 force applications all

 

l  再查看应用状态,无输出

$ db2 list applications

SQL1611W  No data was returned by Database System Monitor.

 

l  做离线备份

$ db2 backup db TEST  to /opt/dbbak

 

l  生成备份文件和归档日志

<img alt="技术分享" onload="if(this.width>650) this.width=650;" title="clip_image015" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; margin: 0px; padding-right: 0px" border="0" alt="clip_image015" src="http://s3.51cto.com/wyfs02/M01/92/B0/wKioL1kB0YXi7ogYAAAHygwlERE596.png" "441" height="39" />

 

<img alt="技术分享" onload="if(this.width>650) this.width=650;" title="clip_image016" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; margin: 0px; padding-right: 0px" border="0" alt="clip_image016" src="http://s3.51cto.com/wyfs02/M02/92/B1/wKiom1kB0YWAsC_3AAAKciwu4RU148.png" "289" height="76" />

 

l  查看备份记录

> db2adutl query  //该命令是TSM集成命令,无TSM环境不能执行

<img alt="技术分享" onload="if(this.width>650) this.width=650;" title="clip_image018" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; margin: 0px; padding-right: 0px" border="0" alt="clip_image018" src="http://s3.51cto.com/wyfs02/M01/92/B1/wKiom1kB0YrQT5eiAAFaJ_3coWM233.jpg" "554" height="355" />

 

$ db2 list history backup all for test

<img alt="技术分享" onload="if(this.width>650) this.width=650;" title="clip_image020" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; margin: 0px; padding-right: 0px" border="0" alt="clip_image020" src="http://s3.51cto.com/wyfs02/M02/92/B1/wKiom1kB0Y2i2T_fAADNrXIu7b8206.jpg" "554" height="478" />

 

l  启动进入库

$ db2 connect to test

 

l  再做在线备份,可以进行

$ db2 backup db TEST online to /opt/dbbak include logs

 

五、    删除数据库模拟破坏

$ db2 drop db test

 

$ db2 connect to test

SQL1013N  The database alias name or database name "TEST" could not be found. 

SQLSTATE=42705

 

六、    恢复数据库

$ db2 restore db TEST from /opt/bak taken at 20170123152954

$ db2 restore db test taken at 20160513020408

这时候,连接数据库,提示:

SQL1117N  A connection to or activation of database "TEST" cannot be made

because of ROLL-FORWARD PENDING.  SQLSTATE=57019

 

在前滚已经过大于或等于 这个 的时间点之前,数据库 "XXX"

不能脱离前滚暂挂状态,因为节点 "0" 中包含比指定时间更新的信息

解决办法:前滚日志

$ db2 rollforward database test to end of logs and complete

如果报:

SQL4970N  Rollforward recovery stopped on the database named "TEST" because

the rollforward utility cannot reach the specified stop point (end-of-log or

point-in-time) on the following database partitions: "0".

 

解决办法:恢复日志

$ db2 restore db test logs from /opt/dbbak/ taken at 20160513020408 logtarget /opt/dbbak/

 

复制恢复的归档文件

$ cp /opt/dbbak/S0000000.LOG /home/db2inst1/db2inst1/NODE0000/SQL00002/LOGSTREAM0000/LOGSTREAM0000

 

<img alt="技术分享" onload="if(this.width>650) this.width=650;" title="clip_image022" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; margin: 0px; padding-right: 0px" border="0" alt="clip_image022" src="http://s3.51cto.com/wyfs02/M00/92/B1/wKiom1kB0ZHyf-2mAABNwiG-Xn4255.jpg" "554" height="159" />

 

l  启动进入数据库

<img alt="技术分享" onload="if(this.width>650) this.width=650;" title="clip_image023" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; margin: 0px; padding-right: 0px" border="0" alt="clip_image023" src="http://s3.51cto.com/wyfs02/M00/92/B0/wKioL1kB0ZGheOWIAAARQzPJyzs675.png" "497" height="148" />

 

l  查询表和数据

<im 本文系统来源:http://12865902.blog.51cto.com/12855902/1920218


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!