使用DMRAMN执行备份集恢复
使用RECOVER命令完成数据库恢复工作,可以是基于备份集的恢复工作,也可以是使用本地归档日志的恢复工作。如果还原后,数据已经处于一致性状态了,则可以使用更新DB_MAGIC方式恢复,前提是不需要重做日志。语法如下:
RECOVER DATABASE '' [ ] [USE DB_MAGIC ] [UNTIL TIME '< 时间串>'] [UNTIL LSN ]; | RECOVER DATABASE ' ' FROM BACKUPSET '< 备份集目录>'[DEVICE TYPE DISK|TAPE[PARMS '< 介质参数>']] [IDENTIFIED BY < 密码> [ENCRYPT WITH < 加密算法>]] ; | RECOVER DATABASE ' ' UPDATE DB_MAGIC; ::= WITH ARCHIVEDIR '< 归档日志目录>'{,'< 归档日志目录>'}
DATABASE:指定还原库目标的dm.ini文件路径。
USE DB_MAGIC:指定本地归档日志对应数据库的DB_MAGIC,若不指定,则默认使用目标恢复数据库的DB_MAGIC。
UNTIL TIME:恢复数据库到指定的时间点。
UNTIL LSN:恢复数据库到指定的LSN。
BACKUPSET:指定用于还原目标数据库的备份集目录。
DEVICE TYPE:指存储备份集的介质类型,支持DISK和TAPE,默认为DISK。DISK表示存储备份集到磁盘,TAPE表示存储到磁带。
PARMS:介质参数,供第三方存储介质(TAPE类型)管理使用。
IDENTIFIED BY:指定备份时使用的加密密码,供还原过程解密使用。
ENCRYPT WITH:指定备份时使用的加密算法,供还原过程解密使用,若未指定,则使用默认算法。
WITH ARCHIVEDIR:本地归档日志搜索目录,若未指定,则仅使用目标库配置本地归档目录,RAC环境还会取REMOTE归档目录。
数据库恢复有三种方式:更新DB_MAGIC恢复、从备份集恢复和从归档恢复。
从备份集恢复
如果备份集在备份过程中生成了日志,且这些日志在备份集中有完整备份(如联机数据库备份),在执行数据库还原后,可以重做备份集中备份的日志,将数据库恢复到备份时的状态,即从备份集恢复。完整的示例如下:
1) 启动DIsql联机备份数据库。
SQL> backup database full to db_full_bak_recover_bakset backupset '/dm_home/dmdba/dmdbms/data/jydm/bak/db_full_bak_recover_bakset_backupset'; executed successfully used time: 00:00:07.673. Execute id is 247. SQL> select permanent_magic; LINEID PERMANENT_MAGIC ---------- --------------- 1 1250320462 used time: 48.106(ms). Execute id is 282. SQL> select db_magic from v$rlog; LINEID DB_MAGIC ---------- ----------- 1 708657636 used time: 37.277(ms). Execute id is 315.
2) 准备目标库,可以使用备份库,也可以重新生成库。这里是在别一台服务器上创建新库,重新生成库操作如下:
[dmdba@dmks ~]$ dminit path=/dm_home/dmdbms/data db_name=dameng_for_recover auto_overwrite=1 port_num=5336 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 in 14 day(s) on 2020-06-30 log file path: /dm_home/dmdbms/data/dameng_for_recover/dameng_for_recover01.log log file path: /dm_home/dmdbms/data/dameng_for_recover/dameng_for_recover02.log write to dir [/dm_home/dmdbms/data/dameng_for_recover]. create dm database success. 2020-06-16 16:03:18 [root@dmks root]# ./dm_service_installer.sh -i /dm_home/dmdbms/data/dameng_for_recover/dm.ini -p dmrc -t dmserver Move the service script file(/dm_home/dmdbms/bin/DmServicedmrc to /etc/rc.d/init.d/DmServicedmrc) Finished to create the service (DmServicedmrc) [root@dmks root]# service DmServicedmrc start Starting DmServicedmrc: [ OK ] [dmdba@dmks ~]$ disql SYSDBA/SYSDBA@localhost:5336 Server[localhost:5336]:mode is normal, state is open login used time: 10.727(ms) disql V7.1.6.46-Build(2018.02.08-89107)ENT Connected to: DM 7.1.6.46 SQL> select permanent_magic; LINEID PERMANENT_MAGIC ---------- --------------- 1 450923536 used time: 1.042(ms). Execute id is 5. SQL> select db_magic from v$rlog; LINEID DB_MAGIC ---------- ----------- 1 1009602608 used time: 1.332(ms). Execute id is 6.
3)将备份集复制到目标库所在主机上
[dmdba@shard1 bak]$ scp -r db_full_bak_recover_bakset_backupset/ dmdba@10.138.130.187:/dm_home/dmdbms/backup/ dmdba@10.138.130.187's password: db_full_bak_recover_bakset_backupset.bak 100% 211MB 105.3MB/s 00:02 db_full_bak_recover_bakset_backupset_1.bak 100% 225KB 225.0KB/s 00:00 db_full_bak_recover_bakset_backupset.meta 100% 93KB 92.5KB/s 00:00 [dmdba@shard1 bak]$
4)启动RMAN,校验备份。
RMAN> check backupset '/dm_home/dmdbms/backup/db_full_bak_recover_bakset_backupset'; check backupset '/dm_home/dmdbms/backup/db_full_bak_recover_bakset_backupset'; CMD END.CODE:[0] check backupset successfully. time used: 6.293(ms)
5)还原数据库。
RMAN> restore database '/dm_home/dmdbms/data/dameng_for_recover/dm.ini' from backupset '/dm_home/dmdbms/backup/db_full_bak_recover_bakset_backupset'; restore database '/dm_home/dmdbms/data/dameng_for_recover/dm.ini' from backupset '/dm_home/dmdbms/backup/db_full_bak_recover_bakset_backupset'; checking if the database under system path [/dm_home/dmdbms/data/dameng_for_recover] is running...[4]. checking if the database under system path [/dm_home/dmdbms/data/dameng_for_recover] is running...[3]. checking if the database under system path [/dm_home/dmdbms/data/dameng_for_recover] is running...[2]. checking if the database under system path [/dm_home/dmdbms/data/dameng_for_recover] is running...[1]. checking if the database under system path [/dm_home/dmdbms/data/dameng_for_recover] is running...[0]. checking if the database under system path [/dm_home/dmdbms/data/dameng_for_recover] is running, write dmrman info. RESTORE DATABASE CHECK...... RESTORE DATABASE , dbf collect...... RESTORE DATABASE , dbf refresh ...... RESTORE BACKUPSET [/dm_home/dmdbms/backup/db_full_bak_recover_bakset_backupset] START...... total 22 packages processed... RESTORE DATABASE , UPDATE ctl file...... RESTORE DATABASE , REBUILD key file...... RESTORE DATABASE , CHECK db info...... RESTORE DATABASE , UPDATE db info...... total 24 packages processed! CMD END.CODE:[0] restore successfully. time used: 29564.812(ms)
6)恢复数据库。
RMAN> recover database '/dm_home/dmdbms/data/dameng_for_recover/dm.ini' from backupset '/dm_home/dmdbms/backup/db_full_bak_recover_bakset_backupset'; recover database '/dm_home/dmdbms/data/dameng_for_recover/dm.ini' from backupset '/dm_home/dmdbms/backup/db_full_bak_recover_bakset_backupset'; checking if the database under system path [/dm_home/dmdbms/data/dameng_for_recover] is running...[4]. checking if the database under system path [/dm_home/dmdbms/data/dameng_for_recover] is running...[3]. checking if the database under system path [/dm_home/dmdbms/data/dameng_for_recover] is running...[2]. checking if the database under system path [/dm_home/dmdbms/data/dameng_for_recover] is running...[1]. checking if the database under system path [/dm_home/dmdbms/data/dameng_for_recover] is running...[0]. checking if the database under system path [/dm_home/dmdbms/data/dameng_for_recover] is running, write dmrman info. EP[0] max_lsn: 19945240 RESTORE RLOG CHECK...... RESTORE RLOG ,gen tmp file...... RESTORE RLOG FROM BACKUPSET [/dm_home/dmdbms/backup/db_full_bak_recover_bakset_backupset] START...... total 0 packages processed... total 2 packages processed! CMD END.CODE:[0] EP[0] Recover LSN from 19945241 to 19947077. Recover from archive log finished, time used:0.322s. recover successfully! time used: 7027.238(ms)
7)检查db_magic,db_maigc从708657636变成了1572156104
[root@dmks init.d]# service DmServicedmrc start Starting DmServicedmrc: [ OK ] [dmdba@dmks ~]$ disql SYSDBA/xxzx7817600@localhost:5336 Server[localhost:5336]:mode is normal, state is open login used time: 12.530(ms) disql V7.1.6.46-Build(2018.02.08-89107)ENT Connected to: DM 7.1.6.46 SQL> select permanent_magic; LINEID PERMANENT_MAGIC ---------- --------------- 1 1250320462 used time: 1.288(ms). Execute id is 4. SQL> select db_magic from v$rlog; LINEID DB_MAGIC ---------- ----------- 1 1572156104 used time: 1.845(ms). Execute id is 5.
8)检查数据
SQL> select * from t1; LINEID ID NAME ---------- ----------- ---- 1 1 jy 2 2 hy used time: 1.286(ms). Execute id is 5.