DM7使用DMRAMN对多次故障恢复后使用不同数据库的归档进行恢复

使用DMRAMN对多次故障恢复后使用不同数据库的归档进行恢复
在实际应用中可能会遇到以下还原场景:
1) 创建一个数据库D1。
2) 操作数据库并执行数据库备份B1。
3) 继续操作数据库的过程中数据库故障,此时生成的归档为A1。
4) 利用备份B1和归档A1将数据库D1恢复到目标库D2,此时数据库D2为D1故障前的状态。
5) 启动数据库D2,操作数据库过程中数据库第二次故障,此时生成的归档为A2。
归档A1、A2属于不同的数据库,使用备份B1和归档A1、A2是否可以恢复数据库到第二次故障发生前的状态呢?下面将举例详细说明如何使用不同数据库的归档恢复数据库到最新状态,具体步骤如下:

1) 创建源库D1,即待备份的数据库,然后启动数据库并配置归档,归档目录为1) 创建源库D1,即待备份的数据库,然后启动数据库并配置归档,归档目录为/dm_home/dmdba/dmdbms/data/d1_arch。

[dmdba@shard1 data]$ dminit path=/dm_home/dmdba/dmdbms/data db_name=d1 instance_name=d1 port_num=5237 unicode_flag=0
initdb V7.1.6.46-Build(2018.02.08-89107)ENT
db version: 0x7000a
file dm.key not found, use default license!
License will expire on 2020-07-03

 log file path: /dm_home/dmdba/dmdbms/data/d1/d101.log


 log file path: /dm_home/dmdba/dmdbms/data/d1/d102.log

write to dir [/dm_home/dmdba/dmdbms/data/d1].
create dm database success. 2020-06-19 16:47:51


[root@shard1 root]# ./dm_service_installer.sh -i /dm_home/dmdba/dmdbms/data/d1/dm.ini -p d1 -t dmserver
ln -s '/usr/lib/systemd/system/DmServiced1.service' '/etc/systemd/system/multi-user.target.wants/DmServiced1.service'
Finished to create the service (DmServiced1)
[root@shard1 root]# service DmServiced1 start
Redirecting to /bin/systemctl start  DmServiced1.service

–连接数据库配置归档

[dmdba@shard1 ~]$ disql SYSDBA/SYSDBA@localhost:5237

Server[localhost:5237]:mode is normal, state is open
login used time: 9.972(ms)
disql V7.1.6.46-Build(2018.02.08-89107)ENT
Connected to: DM 7.1.6.46
SQL> alter database mount;
executed successfully
used time: 00:00:01.845. Execute id is 0.
SQL> alter database add archivelog 'dest=/dm_home/dmdba/dmdbms/data/d1_arch,type=local,file_size=256,space_limit=10240';
executed successfully
used time: 22.257(ms). Execute id is 0.
SQL> alter database archivelog;
executed successfully
used time: 31.716(ms). Execute id is 0.
SQL> alter database open;
executed successfully
used time: 823.762(ms). Execute id is 0.
SQL> select permanent_magic;

LINEID     PERMANENT_MAGIC
---------- ---------------
1          -836921674

used time: 2.193(ms). Execute id is 807.
SQL> select db_magic from v$rlog;

LINEID     DB_MAGIC
---------- -----------
1          -811309012

used time: 1.365(ms). Execute id is 808.

2) 操作数据库的同时备份数据库,备份集为B1。此处以向表中循环插入数据为例操作数据库。

SQL> create table tab_for_recover_01(c1 int);
executed successfully
used time: 22.960(ms). Execute id is 809.
SQL> begin
2   for i in 1 .. 100000 loop
3    insert into tab_for_recover_01 values(i);
4   end loop;
5   commit;
6   end;
7   /
DMSQL executed successfully
used time: 00:00:01.612. Execute id is 810.

插入数据的同时,另外一个会话备份数据库

SQL> backup database full to b1 backupset '/dm_home/dmdba/dmdbms/backup/b1' device type disk backupinfo 'dameng database full backup online' maxpiecesize 2048;
executed successfully
used time: 00:00:01.920. Execute id is 813.

3) 继续向数据库插入数据一段时间后杀掉服务器,模拟服务器故障。从备份后到服务器故障这段时间的生成的归档称为A1。

SQL> begin
2   for i in 100001 .. 199999 loop
3    insert into tab_for_recover_01 values(i);
4   end loop;
5   commit;
6   end;
7   /
DMSQL executed successfully
used time: 00:00:01.633. Execute id is 814.

[root@shard1 root]# ps -ef | grep dmserver
dmdba     9749     1  1 16:49 ?        00:00:09 /dm_home/dmdba/dmdbms/bin/dmserver /dm_home/dmdba/dmdbms/data/d1/dm.ini -noconsole
root     13497  9392  0 17:03 pts/11   00:00:00 grep --color=auto dmserver
[root@shard1 root]# kill -9 9749
[root@shard1 root]# ps -ef | grep dmserver
root     13598  9392  0 17:04 pts/11   00:00:00 grep --color=auto dmserver

4) 创建目标库D2,即待还原的库,并配置归档(参考3.1.2小节),归档目录与数据库D1相同。

[dmdba@shard1 data]$ dminit path=/dm_home/dmdba/dmdbms/data db_name=d2 instance_name=d2 port_num=5238 unicode_flag=0
initdb V7.1.6.46-Build(2018.02.08-89107)ENT
db version: 0x7000a
file dm.key not found, use default license!
License will expire on 2020-07-03

 log file path: /dm_home/dmdba/dmdbms/data/d2/d201.log


 log file path: /dm_home/dmdba/dmdbms/data/d2/d202.log

write to dir [/dm_home/dmdba/dmdbms/data/d2].
create dm database success. 2020-06-19 17:11:54

[root@shard1 root]# ./dm_service_installer.sh -i /dm_home/dmdba/dmdbms/data/d2/dm.ini -p d2 -t dmserver
ln -s '/usr/lib/systemd/system/DmServiced2.service' '/etc/systemd/system/multi-user.target.wants/DmServiced2.service'
Finished to create the service (DmServiced2)

[root@shard1 root]# service DmServiced2 start
Redirecting to /bin/systemctl start  DmServiced2.service


[dmdba@shard1 ~]$ disql SYSDBA/SYSDBA@localhost:5238

Server[localhost:5238]:mode is normal, state is open
login used time: 9.862(ms)
disql V7.1.6.46-Build(2018.02.08-89107)ENT
Connected to: DM 7.1.6.46
SQL> alter database mount;
executed successfully
used time: 00:00:01.844. Execute id is 0.
SQL> alter database add archivelog 'dest=/dm_home/dmdba/dmdbms/data/d2_arch,type=local,file_size=256,space_limit=10240';
executed successfully
used time: 40.105(ms). Execute id is 0.
SQL> alter database archivelog;
executed successfully
used time: 31.716(ms). Execute id is 0.
SQL> alter database open;
executed successfully
used time: 675.370(ms). Execute id is 0.

5) 启动DMRMAN工具,利用备份集B1和归档A1恢复数据库D2到D1故障前的状态。

[root@shard1 root]# service DmServiced2 stop
Redirecting to /bin/systemctl stop  DmServiced2.service

RMAN> restore database '/dm_home/dmdba/dmdbms/data/d2/dm.ini' from backupset '/dm_home/dmdba/dmdbms/backup/b1';
restore database '/dm_home/dmdba/dmdbms/data/d2/dm.ini' from backupset '/dm_home/dmdba/dmdbms/backup/b1';
file dm.key not found, use default license!
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[4].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[3].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[2].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[1].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[0].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running, write dmrman info.
RESTORE DATABASE  CHECK......
RESTORE DATABASE , dbf collect......
RESTORE DATABASE , dbf refresh ......
RESTORE BACKUPSET [/dm_home/dmdba/dmdbms/backup/b1] START......
total 3 packages processed...
RESTORE DATABASE , UPDATE ctl file......
RESTORE DATABASE , REBUILD key file......
RESTORE DATABASE , CHECK db info......
RESTORE DATABASE , UPDATE db info......
total 4 packages processed!
CMD END.CODE:[0]
restore successfully.
time used: 15382.524(ms)
RMAN> recover database '/dm_home/dmdba/dmdbms/data/d2/dm.ini' with archivedir '/dm_home/dmdba/dmdbms/data/d1_arch';
recover database '/dm_home/dmdba/dmdbms/data/d2/dm.ini' with archivedir '/dm_home/dmdba/dmdbms/data/d1_arch';
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[4].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[3].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[2].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[1].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[0].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running, write dmrman info.
EP[0] max_lsn: 134425

total redo pages:32768

EP[0] Recover LSN from 134426 to 235404.
Recover from archive log finished, time used:0.910s.
recover successfully!
time used: 8038.102(ms)

6)启动目标库D2,向数据库中继续插入数据。

[root@shard1 root]# service DmServiced2 start
Redirecting to /bin/systemctl start  DmServiced2.service


[dmdba@shard1 ~]$ disql SYSDBA/SYSDBA@localhost:5238

Server[localhost:5238]:mode is normal, state is open
login used time: 9.853(ms)
disql V7.1.6.46-Build(2018.02.08-89107)ENT
Connected to: DM 7.1.6.46
SConnected to: DM 7.1.6.46
SQL> select permanent_magic;

LINEID     PERMANENT_MAGIC
---------- ---------------
1          -836921674

used time: 2.479(ms). Execute id is 3.
SQL> select db_magic from v$rlog;

LINEID     DB_MAGIC
---------- -----------
1          1621992964

used time: 1.784(ms). Execute id is 4.

恢复后目标库的db_magic与源库的不一样,因为每次恢复后都会发生改变。

SQL> select count(*) from tab_for_recover_01;

LINEID     COUNT(*)
---------- --------------------
1          199999

used time: 1.256(ms). Execute id is 5.


SQL> begin
2   for i in 200000 .. 300000 loop
3    insert into tab_for_recover_01 values(i);
4   end loop;
5   commit;
6   end;
7   /
DMSQL executed successfully
used time: 00:00:01.274. Execute id is 7.

7) 插入数据一段时间后,杀掉服务器,模拟服务器第二次故障。数据库D2启动到第二次故障之间产生的归档为A2。

SQL> select count(*) from tab_for_recover_01;

LINEID     COUNT(*)
---------- --------------------
1          300000

used time: 0.387(ms). Execute id is 8.

[root@shard1 root]# ps -ef | grep dmserver
dmdba    23093     1  1 17:49 ?        00:00:01 /dm_home/dmdba/dmdbms/bin/dmserver /dm_home/dmdba/dmdbms/data/d2/dm.ini -noconsole
root     23629  9392  0 17:51 pts/11   00:00:00 grep --color=auto dmserver
[root@shard1 root]# kill -9 23093
[root@shard1 root]# ps -ef | grep dmserver
root     23701  9392  0 17:51 pts/11   00:00:00 grep --color=auto dmserver

8) 恢复目标库D2到第一次故障前的状态。恢复一次,目标库的DB_MAGIC都会改变,因此归档A1、A2的DB_MAGIC不同,属于不同的数据库。第一次恢复只能先重做归档A1,即恢复到第一次故障前的状态。

RMAN> restore database '/dm_home/dmdba/dmdbms/data/d2/dm.ini' from backupset '/dm_home/dmdba/dmdbms/backup/b1';
restore database '/dm_home/dmdba/dmdbms/data/d2/dm.ini' from backupset '/dm_home/dmdba/dmdbms/backup/b1';
file dm.key not found, use default license!
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[4].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[3].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[2].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[1].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[0].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running, write dmrman info.
RESTORE DATABASE  CHECK......
RESTORE DATABASE , dbf collect......
RESTORE DATABASE , dbf refresh ......
RESTORE BACKUPSET [/dm_home/dmdba/dmdbms/backup/b1] START......
total 3 packages processed...
RESTORE DATABASE , UPDATE ctl file......
RESTORE DATABASE , REBUILD key file......
RESTORE DATABASE , CHECK db info......
RESTORE DATABASE , UPDATE db info......
total 4 packages processed!
CMD END.CODE:[0]
restore successfully.
time used: 13519.050(ms)
RMAN> recover database '/dm_home/dmdba/dmdbms/data/d2/dm.ini' with archivedir '/dm_home/dmdba/dmdbms/data/d1_arch';
recover database '/dm_home/dmdba/dmdbms/data/d2/dm.ini' with archivedir '/dm_home/dmdba/dmdbms/data/d1_arch';
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[4].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[3].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[2].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[1].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[0].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running, write dmrman info.
EP[0] max_lsn: 134425

total redo pages:32768

total redo pages:50909


total redo pages:25326


EP[0] Recover LSN from 134426 to 339536.
Recover from archive log finished, time used:1.557s.
recover successfully!
time used: 8040.986(ms)

9) 查看归档A2的DB_MAGIC。

[dmdba@shard1 d2_arch]$ ls -lrt
总用量 12800
-rw-r--r-- 1 dmdba dinstall  13093376 6月  19 17:51 ARCHIVE_LOCAL1_20200619174912579_0.log
-rw-r--r-- 1 dmdba dinstall 268435456 6月  19 17:51 ARCHIVE_LOCAL1_20200619175139125_0.log
[dmdba@shard1 d2_arch]$ dmrachk arch_fil=/dm_home/dmdba/dmdbms/data/d2_arch/ARCHIVE_LOCAL1_20200619174912579_0.log
rachk V7.1.6.46-Build(2018.02.08-89107)ENT
/*******************************************************************/
archive file /dm_home/dmdba/dmdbms/data/d2_arch/ARCHIVE_LOCAL1_20200619174912579_0.log itemize.
version        : 0x7004
status         : INACTIVE
n_rpags        : 25565
db_magic       : 1621992964
pemnt_magic    : -836921674
arch_lsn       : 339537
arch_seq       : 0
clsn           : 442123
next_seq       : 25564
file len       : 13093376
file free      : 13093376
create time    : 2020-06-19 17:49:12
close time     : 2020-06-19 17:51:39
crc_check      : TRUE
/*******************************************************************/

The SUMMARY(seqno[0]):
total files: 1
okey files: 1
fail file: 0
repeat file: 0

the rachk tool running cost 0.691 ms
[dmdba@shard1 d2_arch]$ dmrachk arch_fil=/dm_home/dmdba/dmdbms/data/d2_arch/ARCHIVE_LOCAL1_20200619175139125_0.log
rachk V7.1.6.46-Build(2018.02.08-89107)ENT
/*******************************************************************/
archive file /dm_home/dmdba/dmdbms/data/d2_arch/ARCHIVE_LOCAL1_20200619175139125_0.log itemize.
version        : 0x7004
status         : ACTIVE
n_rpags        : 0
db_magic       : 1621992964
pemnt_magic    : -836921674
arch_lsn       : 0
arch_seq       : 0
clsn           : 0
next_seq       : 0
file len       : 268435456
file free      : 4096
create time    : 2020-06-19 17:51:39
close time     : 2020-06-19 17:51:39
crc_check      : TRUE
/*******************************************************************/
check archive file /dm_home/dmdba/dmdbms/data/d2_arch/ARCHIVE_LOCAL1_20200619175139125_0.log, encounter 1 error, as follow:

the archive file /dm_home/dmdba/dmdbms/data/d2_arch/ARCHIVE_LOCAL1_20200619175139125_0.log encounter no data.


The SUMMARY(seqno[0]):
total files: 1
okey files: 0
fail file: 1
repeat file: 0

the rachk tool running cost 0.267 ms

10) 修改目标库D2的DB_MAGIC与归档A2一致。若要利用归档恢复,必须保证数据库的DB_MAGIC与归档的DB_MAGIC一致。由步骤8的查询可知,归档A2的DB_MAGIC为1449535319。修改数据库的DB_MAGIC需要修改SYSTEM.DBF和DAMENG_FOR_RES01.log两个文件,步骤如下:
–修改文件SYSTEM.DBF

[dmdba@shard1 d2_arch]$ dmmdf type=1 file=/dm_home/dmdba/dmdbms/data/d2/SYSTEM.DBF
dmmdf V7.1.6.46-Build(2018.02.08-89107)ENT
**********************************************************
1 db_magic=-779140962
2 next_trxid=1127
3 pemnt_magic=-836921674
**********************************************************
Please input which parameter you want to change(1-3), q to quit: 1
Please input new value: 1621992964
**********************************************************
1 db_magic=1621992964
2 next_trxid=1127
3 pemnt_magic=-836921674
**********************************************************
Do you want to save the change to file (y/n): y
Save to file success!

–修改文件d201.log

[dmdba@shard1 d2_arch]$ dmmdf type=2 file=/dm_home/dmdba/dmdbms/data/d2/d201.log
dmmdf V7.1.6.46-Build(2018.02.08-89107)ENT
**********************************************************
1 sig = DMRLOG
2 ver = 7004
3 chksum = 0
4 dbversion = 0x7000a
5 sta = 1
6 n_magic = 14
7 db_magic = -779140962
8 clsn_fil = 0
10 next_seq = 0
11 arch_seq = 0
12 len = 268435456
13 free = 4096
14 clsn = 339536
15 clsn_off = 4096
16 arch_lsn = 0
17 pemnt_magic = -836921674
18 fil_id = 0
19 crc_check = 1
**********************************************************
You can only reset sta(5) or db_magic (7) or clsn (14) or pemnt_magic(17) or fil_id(18).
Please input the num which one you want to change, q to quit: 7
Input the new value: 1621992964
**********************************************************
1 sig = DMRLOG
2 ver = 7004
3 chksum = 0
4 dbversion = 0x7000a
5 sta = 1
6 n_magic = 14
7 db_magic = 1621992964
8 clsn_fil = 0
10 next_seq = 0
11 arch_seq = 0
12 len = 268435456
13 free = 4096
14 clsn = 339536
15 clsn_off = 4096
16 arch_lsn = 0
17 pemnt_magic = -836921674
18 fil_id = 0
19 crc_check = 1
**********************************************************
Do you want to quit and save the change to file (y/n): y
Save to file success!

–修改文件d202.log

[dmdba@shard1 d2_arch]$ dmmdf type=2 file=/dm_home/dmdba/dmdbms/data/d2/d202.log
dmmdf V7.1.6.46-Build(2018.02.08-89107)ENT
**********************************************************
1 sig = DMRLOG
2 ver = 7004
3 chksum = 0
4 dbversion = 0x7000a
5 sta = 0
6 n_magic = 14
7 db_magic = -779140962
8 clsn_fil = 0
10 next_seq = 0
11 arch_seq = 0
12 len = 268435456
13 free = 4096
14 clsn = 0
15 clsn_off = 0
16 arch_lsn = 0
17 pemnt_magic = -836921674
18 fil_id = 1
19 crc_check = 1
**********************************************************
You can only reset sta(5) or db_magic (7) or clsn (14) or pemnt_magic(17) or fil_id(18).
Please input the num which one you want to change, q to quit: 7
Input the new value: 1621992964
**********************************************************
1 sig = DMRLOG
2 ver = 7004
3 chksum = 0
4 dbversion = 0x7000a
5 sta = 0
6 n_magic = 14
7 db_magic = 1621992964
8 clsn_fil = 0
10 next_seq = 0
11 arch_seq = 0
12 len = 268435456
13 free = 4096
14 clsn = 0
15 clsn_off = 0
16 arch_lsn = 0
17 pemnt_magic = -836921674
18 fil_id = 1
19 crc_check = 1
**********************************************************
Do you want to quit and save the change to file (y/n): y
Save to file success!

11) 利用归档A2恢复数据库至第二次故障前的状态。

RMAN> recover database '/dm_home/dmdba/dmdbms/data/d2/dm.ini' with archivedir '/dm_home/dmdba/dmdbms/data/d2_arch';
recover database '/dm_home/dmdba/dmdbms/data/d2/dm.ini' with archivedir '/dm_home/dmdba/dmdbms/data/d2_arch';
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[4].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[3].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[2].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[1].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running...[0].
checking if the database under system path [/dm_home/dmdba/dmdbms/data/d2] is running, write dmrman info.
EP[0] max_lsn: 339536

EP[0] Recover LSN from 339537 to 442123.
Recover from archive log finished, time used:1.077s.
recover successfully!
time used: 8048.396(ms)

12)验证数据

[root@shard1 root]# service DmServiced2 start
Redirecting to /bin/systemctl start  DmServiced2.service

SQL> select count(*) from tab_for_recover_01;

LINEID     COUNT(*)
---------- --------------------
1          300000

used time: 3.022(ms). Execute id is 3.

发表评论

电子邮件地址不会被公开。