DB2备份+日志恢复并前滚数据库

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介:

任何数据库都是这样的,我们会做数据库的全量备份,增量备份,并记录日志。如果数据库出现意外宕机,则可以用全备+增量备份+日志来进行数据库恢复。
本文主要展示如何使用备份+日志来恢复DB2的数据库,为了更清晰一点儿,使用两台机器151和152;在152机器上利用备份和日志恢复到151的数据库的状态。

151机器上数据操作步骤

1.创建测试库

#1.创建测试数据库
db2 create database yynewlog
#2.启用归档日志
db2 update db cfg for yynewlog using logarchmeth1 DISK:/data/db2data
#3.启用增量备份
db2 update db cfg for yynewlog using TRACKMOD YES

#3.解除backup pending状态
db2 backup database yynewlog

#4.创建测试表
 db2 connect to yynewlog
db2 "create table student (id int not null,
name varchar(20),
sex smallint,
phone varchar(12),
address varchar(30)
)"

#5.填充数据:id=1,id=2
db2 "insert into student values(1,'charles ',0,'19089076756','BEIJING ')"
db2 "insert into student values(2,'zhouzhou ',0,'1076758886','BEIJING ')"

2.全量备份

  使用在线备份数据库,with include logs保留日志,这个选项好像是默认的,但是为了语句的可读理解,最好还是加上。

[db2inst1@db22 ~]$ db2 backup db yynewlog online to /tmp include logs

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

  /tmp目录下有了名为YYNEWLOG.0.db2inst1.DBPART000.20171030175924.001的全量备份,全量备份里面是id=1,id=2的记录。

3.增量备份

#新插入id=3,id=4的数据。
db2 "insert into student values(3,'xiaohong',0,'67583886','TAIYUAN')"
db2 "insert into student values(4,'lianliang',0,'13667583886','SHIJIAZHAUNG')"

#增量备份
[db2inst1@db22 ~]$ db2 backup db yynewlog ONLINE INCREMENTAL to /tmp include logs

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

/tmp目录下有了名为YYNEWLOG.0.db2inst1.DBPART000.20171030180235.001的备份文件。
全量备份+增量备份里面是id=1,id=2,id=3,id=4的数据。

语法

>--+------------------------+----------------------------------->
   '-INCREMENTAL--+-------+-'   
                  '-DELTA-'     
  • INCREMENTAL
    Specifies a cumulative (incremental) backup image. An incremental backup image is a copy of all database data that has changed since the most recent successful, full backup operation.
    指定一个累积的(增量)备份。一个INCREMENTAL备份镜像是一个 从 距离最近的成功备份的全量备份 之后 所有改变的数据的副本。

    • DELTA

         Specifies a noncumulative (delta) backup image. A delta backup image is a copy of all database data that has changed since the most recent successful backup operation of any type.

      指定一个非 累积的(delta:差值)。一个delta备份镜像是一个 自动最近的一次成功的任何类型的备份操作之后 所有改变的数据库数据的副本。

4.增量delta备份

db2 "insert into student values(5,'shuang',0,'15067583886','SHENZHEN')"
db2 "insert into student values(6,'licy',0,'15683886675','XIAMEN')"
[db2inst1@db22 ~]$ db2 backup db yynewlog ONLINE INCREMENTAL DELTA to /tmp include logs

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

5.活动日志

db2 "insert into student values(7,'shuang',0,'15067583886','SHENZHEN')"
db2 "insert into student values(8,'licy',0,'15683886675','XIAMEN')" 

  此时数据如下:


[db2inst1@db22 ~]$ db2 "select * from student"

ID          NAME                 SEX    PHONE        ADDRESS                       
----------- -------------------- ------ ------------ ------------------------------
          1 charles                   0 19089076756  BEIJING                       
          2 zhouzhou                  0 1076758886   BEIJING                       
          3 xiaohong                  0 67583886     TAIYUAN                       
          4 lianliang                 0 13667583886  SHIJIAZHAUNG                  
          5 shuang                    0 15067583886  SHENZHEN                      
          6 licy                      0 15683886675  XIAMEN                        
          7 shuang                    0 15067583886  SHENZHEN                      
          8 licy                      0 15683886675  XIAMEN                        

  8 record(s) selected.

152机器上的恢复步骤

1.拷贝文件到目标机器

scp /tmp/YYNEWLOG.* db2inst1@192.168.225.152:/tmp

将拷贝YYNEWLOG.0.db2inst1.DBPART000.20171030175924.001 ,
YYNEWLOG.0.db2inst1.DBPART000.20171030180235.001 ,
YYNEWLOG.0.db2inst1.DBPART000.20171030181903.001 这三个文件。

2.备份恢复步骤:

三个时间戳
20171030175924:全备
20171030180235:增量1
20171030181903:差异增量2

#1.指定通过增量备份恢复的形式最后要恢复到 20171030181903这个是时间戳。
[db2inst1@db22 ~]$ db2 restore db YYNEWLOG  INCREMENTAL from /tmp  taken at 20171030181903 
DB20000I  The RESTORE DATABASE command completed successfully.
#2.第一步恢复全备
[db2inst1@db22 ~]$ db2 restore db YYNEWLOG  INCREMENTAL from /tmp  taken at 20171030175924 logtarget /data/db2data/logs
DB20000I  The RESTORE DATABASE command completed successfully.
#3.第二步恢复备份1:
[db2inst1@db22 ~]$ db2 restore db YYNEWLOG  INCREMENTAL from /tmp  taken at 20171030180235 logtarget /data/db2data/logs
SQL2580W  Warning! Restoring logs to a path which contains existing log files. 
Attempting to overwrite an existing log file during restore will cause the 
restore operation to fail.
Do you want to continue ? (y/n) y
DB20000I  The RESTORE DATABASE command completed successfully.
#4.第三步恢复差异增量备份2:
[db2inst1@db22 ~]$ db2 restore db YYNEWLOG  INCREMENTAL from /tmp  taken at 20171030181903 logtarget /data/db2data/logs
SQL2580W  Warning! Restoring logs to a path which contains existing log files. 
Attempting to overwrite an existing log file during restore will cause the 
restore operation to fail.
Do you want to continue ? (y/n) y
DB20000I  The RESTORE DATABASE command completed successfully.

此时,logtarget下有前滚所需的日志:

[root@db22 logs]# ls NODE0000/LOGSTREAM0000/
S0000007.LOG  S0000010.LOG  S0000013.LOG

3.确定所需的活动日志:

3.1 151机器上的活动日志:

[db2inst1@db22 ~]$ db2 get db cfg for yynewlog | grep -i 'First active log'
 First active log file                                   = S0000015.LOG

3.1 152机器上的活动日志:

[db2inst1@db22 ~]$ db2 get db cfg for yynewlog | grep -i 'First active log'
 First active log file                                   = S0000013.LOG

3.3 确定所需的日志

所以恢复的话是需要S0000014.LOG和S0000015.LOG的,151上的第一个活动日志是S0000015.LOG,故S0000014.LOG已经归档了,要把该日志拷贝到归档日志的目录中(恢复时的overflow log path
中,在此我们指定overflow log path为/data/db2data/logs下,刚刚restore时的logtarget):

3.3.1 拷贝归档日志

#先找一下归档日志路径
[root@db22 tmp]# find / -name S0000014.LOG
/db/log/db2inst1/TESTMOVE/NODE0000/LOGSTREAM0000/C0000000/S0000014.LOG
/data/bak/db2inst1/SAMPLE/NODE0000/LOGSTREAM0000/C0000001/S0000014.LOG
/data/db2data/db2inst1/SCHOOL/NODE0000/LOGSTREAM0000/C0000000/S0000014.LOG
/data/db2data/db2inst1/YYNEWLOG/NODE0000/LOGSTREAM0000/C0000000/S0000014.LOG
/home/db2inst1/db2inst1/NODE0000/SQL00003/LOGSTREAM0000/S0000014.LOG
/home/db2inst1/db2inst1/NODE0000/SQL00002/LOGSTREAM0000/S0000014.LOG

#拷贝归档日志:
scp /data/db2data/db2inst1/YYNEWLOG/NODE0000/LOGSTREAM0000/C0000000/S0000014.LOG  db2inst1@192.168.225.152:/data/db2data/logs/NODE0000/LOGSTREAM0000
#overflow log path 下有该日志了
[root@db22 logs]# ls NODE0000/LOGSTREAM0000/
S0000007.LOG  S0000010.LOG  S0000013.LOG  S0000014.LOG

3.3.2 拷贝归档日志

#152机器上的活动日志目录
[db2inst1@db22 ~]$ db2 get db cfg for yynewlog | grep -i 'log'

 Changed path to log files                  (NEWLOGPATH) = 
 Path to log files                                       = /home/db2inst1/db2inst1/NODE0000/SQL00003/LOGSTREAM0000/
 Overflow log path                     (OVERFLOWLOGPATH) = 
 Mirror log path                         (MIRRORLOGPATH) = 

所以152机器上的活动日志目录是:/home/db2inst1/db2inst1/NODE0000/SQL00003/LOGSTREAM0000/

拷贝活动日志

[root@db22 tmp]# scp -r /home/db2inst1/db2inst1/NODE0000/SQL00005/LOGSTREAM0000 db2inst1@192.168.225.152:/home/db2inst1/db2inst1/NODE0000/SQL00003db2inst1@192.168.225.152's password: 
S0000024.LOG            100% 4104KB   4.0MB/s   00:00    
S0000020.LOG            100% 4104KB   4.0MB/s   00:00    
S0000022.LOG            100% 4104KB   4.0MB/s   00:01    
S0000015.LOG            100% 4104KB   4.0MB/s   00:00    
S0000017.LOG            100% 4104KB   4.0MB/s   00:00    
S0000026.LOG            100% 4104KB   4.0MB/s   00:01    
S0000023.LOG            100% 4104KB   4.0MB/s   00:00    
S0000027.LOG            100% 4104KB   4.0MB/s   00:01    
S0000021.LOG            100% 4104KB   4.0MB/s   00:00    
S0000019.LOG            100% 4104KB   4.0MB/s   00:00    
S0000016.LOG            100% 4104KB   4.0MB/s   00:01    
S0000025.LOG            100% 4104KB   4.0MB/s   00:01    
S0000018.LOG            100% 4104KB   4.0MB/s   00:01    

4.前滚恢复

[db2inst1@db22 ~]$ db2 "rollforward db yynewlog to end of logs overflow log path (/data/db2data/logs)"

                                 Rollforward Status

 Input database alias                   = yynewlog
 Number of members have returned status = 1

 Member ID                              = 0
 Rollforward status                     = DB  working
 Next log file to be read               = S0000016.LOG
 Log files processed                    = S0000013.LOG - S0000014.LOG
 Last committed transaction             = 2017-10-31-08.43.15.000000 Local

DB20000I  The ROLLFORWARD command completed successfully.
[db2inst1@db22 ~]$ db2 "rollforward db yynewlog complete"

                                 Rollforward Status

 Input database alias                   = yynewlog
 Number of members have returned status = 1

 Member ID                              = 0
 Rollforward status                     = not pending
 Next log file to be read               =
 Log files processed                    = S0000013.LOG - S0000014.LOG
 Last committed transaction             = 2017-10-31-08.43.15.000000 Local

DB20000I  The ROLLFORWARD command completed successfully.

5.校验

[db2inst1@db22 ~]$ db2 "connect to yynewlog"

   Database Connection Information

 Database server        = DB2/LINUXX8664 11.1.1.1
 SQL authorization ID   = DB2INST1
 Local database alias   = YYNEWLOG

[db2inst1@db22 ~]$ db2 "select * from student"

ID          NAME                 SEX    PHONE        ADDRESS                       
----------- -------------------- ------ ------------ ------------------------------
          1 charles                   0 19089076756  BEIJING                       
          2 zhouzhou                  0 1076758886   BEIJING                       
          3 xiaohong                  0 67583886     TAIYUAN                       
          4 lianliang                 0 13667583886  SHIJIAZHAUNG                  
          5 shuang                    0 15067583886  SHENZHEN                      
          6 licy                      0 15683886675  XIAMEN                        
          7 shuang                    0 15067583886  SHENZHEN                      
          8 licy                      0 15683886675  XIAMEN                        

6.成功

7.疑问解析

7.1 Log files processed = S0000013.LOG - S0000014.LOG,没有15.LOG;是否并不需要活动日志?

前滚恢复返回:

 Next log file to be read               = S0000016.LOG
 Log files processed                    = S0000013.LOG - S0000014.LOG

不拷贝活动日志的情况下来试试恢复会如何

db2 restore db YYNEWLOG  INCREMENTAL from /tmp  taken at 20171030181903 
db2 restore db YYNEWLOG  INCREMENTAL from /tmp  taken at 20171030175924 
db2 restore db YYNEWLOG  INCREMENTAL from /tmp  taken at 20171030180235 
db2 restore db YYNEWLOG  INCREMENTAL from /tmp  taken at 20171030181903 logtarget /data/db2data/logs
#拷贝14.LOG日
[root@db22 logs]# ls NODE0000/LOGSTREAM0000/
S0000013.LOG  S0000014.LOG
[root@db22 logs]# ls /home/db2inst1/db2inst1/NODE0000/SQL00003/LOGSTREAM0000/
[root@db22 logs]# 

#前滚

[db2inst1@db22 ~]$ db2 "rollforward db yynewlog to end of logs overflow log path (/data/db2data/logs)"

                                 Rollforward Status

 Input database alias                   = yynewlog
 Number of members have returned status = 1

 Member ID                              = 0
 Rollforward status                     = DB  working
 Next log file to be read               = S0000015.LOG
 Log files processed                    = S0000013.LOG - S0000013.LOG
 Last committed transaction             = 2017-10-30-10.19.11.000000 UTC

DB20000I  The ROLLFORWARD command completed successfully.
[db2inst1@db22 ~]$ db2 "rollforward db yynewlog complete"
SQL1273N  An operation reading the logs on database "YYNEWLOG" cannot continue 
because of a missing log file "S0000014.LOG" on database partition "0" and log 
stream "0".

由上面的实验可以看到我们溢出目录中是有14.LOG文件的,但是却报找不到日志文件的错误

将活动日志拷贝过来之后:

[db2inst1@db22 ~]$ db2 "rollforward db yynewlog complete"
SQL1273N  An operation reading the logs on database "YYNEWLOG" cannot continue 
because of a missing log file "S0000014.LOG" on database partition "0" and log 
stream "0".
[db2inst1@db22 ~]$ db2 "rollforward db yynewlog to end of logs overflow log path (/data/db2data/logs)"

                                 Rollforward Status

 Input database alias                   = yynewlog
 Number of members have returned status = 1

 Member ID                              = 0
 Rollforward status                     = DB  working
 Next log file to be read               = S0000016.LOG
 Log files processed                    = S0000013.LOG - S0000014.LOG
 Last committed transaction             = 2017-10-31-02.03.51.000000 UTC

DB20000I  The ROLLFORWARD command completed successfully.
[db2inst1@db22 ~]$ db2 "rollforward db yynewlog complete"

                                 Rollforward Status

 Input database alias                   = yynewlog
 Number of members have returned status = 1

 Member ID                              = 0
 Rollforward status                     = not pending
 Next log file to be read               =
 Log files processed                    = S0000013.LOG - S0000014.LOG
 Last committed transaction             = 2017-10-31-02.03.51.000000 UTC

DB20000I  The ROLLFORWARD command completed successfully.

  将活动日志拷贝过来之后再rollforward就可以成功了。

7.2 我只想恢复id =7 的数据,不想要id=8的数据怎么办?重点怎么确定时间戳?

参考:

db2 rollforward 命令时读取日志的顺序

数据库备份恢复的一点总结

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
3月前
|
存储 关系型数据库 MySQL
mysql数据库备份与恢复
MySQL数据库的备份与恢复是确保数据安全性和业务连续性的关键操作。
130 4
|
4天前
|
安全 关系型数据库 MySQL
MySQL崩溃保险箱:探秘Redo/Undo日志确保数据库安全无忧!
《MySQL崩溃保险箱:探秘Redo/Undo日志确保数据库安全无忧!》介绍了MySQL中的三种关键日志:二进制日志(Binary Log)、重做日志(Redo Log)和撤销日志(Undo Log)。这些日志确保了数据库的ACID特性,即原子性、一致性、隔离性和持久性。Redo Log记录数据页的物理修改,保证事务持久性;Undo Log记录事务的逆操作,支持回滚和多版本并发控制(MVCC)。文章还详细对比了InnoDB和MyISAM存储引擎在事务支持、锁定机制、并发性等方面的差异,强调了InnoDB在高并发和事务处理中的优势。通过这些机制,MySQL能够在事务执行、崩溃和恢复过程中保持
20 3
|
1月前
|
关系型数据库 MySQL Linux
Linux环境下MySQL数据库自动定时备份实践
数据库备份是确保数据安全的重要措施。在Linux环境下,实现MySQL数据库的自动定时备份可以通过多种方式完成。本文将介绍如何使用`cron`定时任务和`mysqldump`工具来实现MySQL数据库的每日自动备份。
109 3
|
1月前
|
监控 关系型数据库 MySQL
Linux环境下MySQL数据库自动定时备份策略
在Linux环境下,MySQL数据库的自动定时备份是确保数据安全和可靠性的重要措施。通过设置定时任务,我们可以每天自动执行数据库备份,从而减少人为错误和提高数据恢复的效率。本文将详细介绍如何在Linux下实现MySQL数据库的自动定时备份。
50 3
|
2月前
|
存储 定位技术 数据库
介绍一下数据库的备份和恢复策略
【10月更文挑战第21】介绍一下数据库的备份和恢复策略
|
1月前
|
数据库
【赵渝强老师】数据库的备份方式
备份数据库是指将数据库中的数据及相关信息保存起来,以便在系统故障时恢复。备份对象不仅限于数据本身,还包括数据库对象、用户权限等。根据备份策略、类型和模式的不同,可分为整体/部分备份、完全/增量备份、一致/非一致备份。文中还附有相关视频讲解。
|
2月前
|
存储 Oracle 关系型数据库
【数据库-DB2】深入了解DB2 reorg
本文介绍了DB2数据库中reorg操作的重要性,旨在通过重组表数据来消除数据碎片、压缩信息并提高数据访问速度。reorg操作能够根据索引关键字重新排序数据,减少查询I/O次数,提升查询性能。文章详细讲解了reorg的操作步骤、适用场景及注意事项,强调了在执行reorg前后更新统计信息的必要性。
67 2
|
2月前
|
关系型数据库 MySQL 数据库
DZ社区 mysql日志清理 Discuz! X3.5数据库可以做定期常规清理的表
很多站长在网站日常维护中忽略了比较重要的一个环节,就是对于数据库的清理工作,造成数据库使用量增加必须多的原因一般有2个:后台站点功能开启了家园,此功能现在很少有论坛会用到,但是灌水机会灌入大量垃圾信息致使站长长时间未能发觉;再有就是程序默认的一些通知类表单会存放大量的、对于网站日常运行并无意义的通知信息。
108 2
|
2月前
|
存储 关系型数据库 数据库
数据库启用慢速查询日志如何增强 Postgres 可观测性
数据库启用慢速查询日志如何增强 Postgres 可观测性
43 1
|
4天前
|
存储 Oracle 关系型数据库
数据库传奇:MySQL创世之父的两千金My、Maria
《数据库传奇:MySQL创世之父的两千金My、Maria》介绍了MySQL的发展历程及其分支MariaDB。MySQL由Michael Widenius等人于1994年创建,现归Oracle所有,广泛应用于阿里巴巴、腾讯等企业。2009年,Widenius因担心Oracle收购影响MySQL的开源性,创建了MariaDB,提供额外功能和改进。维基百科、Google等已逐步替换为MariaDB,以确保更好的性能和社区支持。掌握MariaDB作为备用方案,对未来发展至关重要。
13 3

热门文章

最新文章