探索ORACLE之RMAN_07 磁盘损坏数据丢失恢复
作者:吴伟龙 Name:Prodence Woo
QQ:286507175 msn:hapy-wuweilong@hotmail.com
有的时候在企业里面难免会出现由于磁盘损坏而导致数据库的故障乃至数据的丢失,那么这个时候,那么这个时候数据的备份就显得尤为的重要。在这一节我们重点讨论下由于装载数据文件,redo日志文件,controlfile控制文件的磁盘损坏的数据恢复。
6.1 通过强制卸载磁盘模拟数据磁盘损坏:
[root@wwldb ~]# umount -f /DBData/
umount2: 资源或设备忙
umount: /DBData: device is busy
umount2: 资源或设备忙
umount: /DBData: device is busy
[root@wwldb ~]# fuser -m -k /DBData/ 查看设备占用情况
/DBData/: 3508 3510 3512 3514 3516 3518 3529 3531 3535 3541 3610c
[root@wwldb ~]# fuser -m -k -i -k /DBData/ 强制kill /DBData相关进程
[root@wwldb ~]# umount -f /DBData/ 卸载/DBData
[root@wwldb ~]#
6.2 umount 后,通过alert看到实例也随之宕机了。
Fri Jul 616:03:33 2012
Errors in file/DBSoft/admin/WWL/bdump/wwl_pmon_3502.trc:
ORA-00471: DBWR process terminated with error
Fri Jul 616:03:33 2012
PMON: terminating instance due to error 471
Instance terminated by PMON, pid = 3502
[root@wwldb bdump]# ps -ef|grep ora
root 2965 2943 0 14:39 ? 00:00:00 hald-addon-storage: polling/dev/hdc
root 3944 3050 0 16:07 pts/2 00:00:00 su - oracle
oracle 3945 3944 0 16:07 pts/2 00:00:00 -bash
oracle 3977 3945 0 16:07 pts/2 00:00:00 rlwrap sqlplus / as sysdba
oracle 3978 3977 0 16:07 pts/3 00:00:00 sqlplus as sysdba
oracle 3979 3978 0 16:07 ? 00:00:00 oracleWWL(DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))
root 4022 3980 0 16:10 pts/4 00:00:00 grep ora
[root@wwldb bdump]#
6.3 要恢复首先要将数据库启动到mount状态才能恢复
SQL> startup
ORACLE instance started.
Total System Global Area 100663296 bytes
Fixed Size 1217884 bytes
Variable Size 88083108 bytes
Database Buffers 8388608 bytes
Redo Buffers 2973696 bytes
ORA-00205: error in identifying control file, checkalert log for more info
数据库无法启动到mount状态,要执行恢复必须启动到mount状态下才能执行,不过我们在alert日志里面看到是因为确实控制文件2数据库无法启动到mount状态,见如下:
Fri Jul 616:13:24 2012
ORA-00202: control file:'/DBData/oradata/WWL/control02.ctl'
ORA-27037: unable to obtain file status
Linux Error: 2: No such file or directory
Additional information: 3
Fri Jul 616:13:24 2012
ORA-205 signalled during: ALTER DATABASE MOUNT...
这个时候我们可以尝试查找其它控制文件是否都存在,存放在哪里,只要存在任何一个控制文件我们只需要修改参数文件来达到将数据库启动到mount状态。
SQL> show parameter control
NAME TYPE VALUE
----------------------------------------------- ------------------------------
control_file_record_keep_time integer 7
control_files string /DBSoft/oradata/WWL/control01.ctl, /DBData/oradata/WWL/control02.ctl, /DBData/oradata/WWL/control03.ctl
我们通过spfile参数可以看到控制文件是存放在两块磁盘上,损坏的磁盘为/DBData,那么也就以为着control02.ctl和control03.ctl两个控制文件损坏,这个时候我们可以通过/DBSoft磁盘上的control01.ctl来启动数据库,或者将control02.ctl和control03通过control01.ctl转储到其它磁盘上来启动数据库。
我现在通过修改参数文件仅保留control01.ctl来启动数据库。
SQL> alter system set control_files ='/DBSoft/oradata/WWL/control01.ctl' scope=spfile;
System altered.
SQL> shutdown immediate
ORA-01507: database not mounted
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.
Total System Global Area 100663296 bytes
Fixed Size 1217884 bytes
Variable Size 88083108 bytes
Database Buffers 8388608 bytes
Redo Buffers 2973696 bytes
Database mounted.
SQL>
我们可以看到通过修改参数文件,现在数据库已经启动到mount状态。
6.4 添加新的硬盘,并将其格式化创建文件系统,用于数据库数据文件存放的新路径,详细步骤参考:
Fdisk分区方法:http://blog.csdn.net/wuweilong/article/details/7538634
Parte分区方法:http://blog.csdn.net/wuweilong/article/details/7553200
卷管理分方法:http://blog.csdn.net/wuweilong/article/details/7565530
我刚才创建的分区名称是/DBBak2,见如下:
[oracle@wwldb /]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
7.7G 3.0G 4.3G 42% /
/dev/sda1 99M 12M 82M 13% /boot
tmpfs 506M 0 506M 0% /dev/shm
/dev/mapper/DBSoft-dbsoft
20G 1.7G 18G 9% /DBSoft
/dev/mapper/DBBack-DBBack001
20G 720M 18G 4% /DBBak
/dev/mapper/DBBak2-DBBak2
20G 173M 19G 1% /DBBak2
6.5 创建对应的目录
[oracle@wwldb ~]$ mkdir /DBBak2/oradata/WWL
[oracle@wwldb WWL]$ pwd
/DBBak2/oradata/WWL
[oracle@wwldb WWL]$ ls -a
. ..
6.6 将数据文件恢复到/DBBak2/oradata/WWL目录中
查看备份信息:
RMAN> list backup;
using target database control file instead of recoverycatalog
List of Backup Sets
===================
BS Key Type LVSize Device Type Elapsed TimeCompletion Time
------- ---- -- ---------- ----------- ---------------------------
6 Full 540.81M DISK 00:01:13 06-JUL-12
BP Key:6 Status: AVAILABLE Compressed: NO Tag: TAG20120706T154942
PieceName: /DBBak/bak_WWL_07_06_06nfdv8n_1_1
List ofDatafiles in backup set 6
File LV TypeCkp SCN Ckp Time Name
---- -- -------------- --------- ----
1 Full 1263589 06-JUL-12 /DBData/WWL/system01.dbf
2 Full 1263589 06-JUL-12 /DBData/WWL/undotbs01.dbf
3 Full 1263589 06-JUL-12 /DBData/WWL/sysaux01.dbf
4 Full 1263589 06-JUL-12 /DBData/WWL/users01.dbf
5 Full 1263589 06-JUL-12 /DBData/WWL/wwl001.dbf
6 Full 1263589 06-JUL-12 /DBData/WWL/wwl002.dbf
7 Full 1263589 06-JUL-12 /DBData/WWL/wwl003.dbf
BS Key Type LVSize Device Type Elapsed TimeCompletion Time
------- ---- -- ---------- ----------- ---------------------------
7 Full 7.11M DISK 00:00:01 06-JUL-12
BP Key:7 Status: AVAILABLE Compressed: NO Tag: TAG20120706T155059
PieceName: /DBBak/bakctl_c-5520179-20120706-01
Control FileIncluded: Ckp SCN: 1263606 Ckp time:06-JUL-12
SPFILEIncluded: Modification time: 06-JUL-12
通过备份信息执行如下恢复到新的磁盘上:
RMAN> run {
2> set newname fordatafile '/DBData/WWL/system01.dbf' to '/DBBak2/oradata/WWL/system01.dbf';
3> set newname fordatafile '/DBData/WWL/undotbs01.dbf' to '/DBBak2/oradata/WWL/undotbs01.dbf';
4> set newname fordatafile '/DBData/WWL/sysaux01.dbf' to '/DBBak2/oradata/WWL/sysaux01.dbf';
5> set newname fordatafile '/DBData/WWL/users01.dbf' to '/DBBak2/oradata/WWL/users01.dbf';
6> set newname fordatafile '/DBData/WWL/wwl001.dbf' to '/DBBak2/oradata/WWL/wwl01.dbf';
7> set newname fordatafile '/DBData/WWL/wwl002.dbf' to '/DBBak2/oradata/WWL/wwl02.dbf';
8> set newname fordatafile '/DBData/WWL/wwl003.dbf' to '/DBBak2/oradata/WWL/wwl03.dbf';
9> restore database;
10> switch datafile all;
11> recover database;
12> }
executing command: SETNEWNAME
executing command: SETNEWNAME
executing command: SETNEWNAME
executing command: SETNEWNAME
executing command: SETNEWNAME
executing command: SETNEWNAME
executing command: SETNEWNAME
Starting restore at 06-JUL-12
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=46devtype=DISK
channel ORA_DISK_1: startingdatafile backupset restore
channel ORA_DISK_1:specifying datafile(s) to restore from backup set
restoring datafile 00001 to/DBBak2/oradata/WWL/system01.dbf
restoring datafile 00002 to/DBBak2/oradata/WWL/undotbs01.dbf
restoring datafile 00003 to/DBBak2/oradata/WWL/sysaux01.dbf
restoring datafile 00004 to/DBBak2/oradata/WWL/users01.dbf
restoring datafile 00005 to/DBBak2/oradata/WWL/wwl01.dbf
restoring datafile 00006 to/DBBak2/oradata/WWL/wwl02.dbf
restoring datafile 00007 to/DBBak2/oradata/WWL/wwl03.dbf
channel ORA_DISK_1: readingfrom backup piece /DBBak/bak_WWL_07_06_06nfdv8n_1_1
channel ORA_DISK_1: restoredbackup piece 1
piecehandle=/DBBak/bak_WWL_07_06_06nfdv8n_1_1 tag=TAG20120706T154942
channel ORA_DISK_1: restorecomplete, elapsed time: 00:01:46
Finished restore at 06-JUL-12
datafile 1 switched todatafile copy
input datafile copy recid=8stamp=787945637 filename=/DBBak2/oradata/WWL/system01.dbf
datafile 2 switched todatafile copy
input datafile copy recid=9stamp=787945637 filename=/DBBak2/oradata/WWL/undotbs01.dbf
datafile 3 switched todatafile copy
input datafile copy recid=10stamp=787945637 filename=/DBBak2/oradata/WWL/sysaux01.dbf
datafile 4 switched todatafile copy
input datafile copy recid=11stamp=787945637 filename=/DBBak2/oradata/WWL/users01.dbf
datafile 5 switched todatafile copy
input datafile copy recid=12stamp=787945637 filename=/DBBak2/oradata/WWL/wwl01.dbf
datafile 6 switched todatafile copy
input datafile copy recid=13stamp=787945637 filename=/DBBak2/oradata/WWL/wwl02.dbf
datafile 7 switched todatafile copy
input datafile copy recid=14stamp=787945637 filename=/DBBak2/oradata/WWL/wwl03.dbf
Finsh
恢复脚本如下:
run {
set newname for datafile'/DBData/WWL/system01.dbf' to '/DBBak2/oradata/WWL/system01.dbf';
set newname for datafile '/DBData/WWL/undotbs01.dbf'to '/DBBak2/oradata/WWL/undotbs01.dbf';
set newname for datafile'/DBData/WWL/sysaux01.dbf' to '/DBBak2/oradata/WWL/sysaux01.dbf';
set newname for datafile'/DBData/WWL/users01.dbf' to '/DBBak2/oradata/WWL/users01.dbf';
setnewname for datafile '/DBData/WWL/wwl001.dbf' to'/DBBak2/oradata/WWL/wwl01.dbf';
set newname for datafile'/DBData/WWL/wwl002.dbf' to '/DBBak2/oradata/WWL/wwl02.dbf';
set newname for datafile'/DBData/WWL/wwl003.dbf' to '/DBBak2/oradata/WWL/wwl03.dbf';
restore database;
switch datafile all;
}
6.7 生成控制文件trace文件,用来重建控制文件:
SQL> alter database backupcontrolfile to trace as '/tmp/ctl.txt';
SQL> shutdown immediate;
ORA-01109: database not open
Database dismounted.
ORACLE instance shut down.
SQL> STARTUP NOMOUNT
ORACLE instance started.
Total System Global Area 100663296 bytes
Fixed Size 1217884 bytes
Variable Size 88083108 bytes
Database Buffers 8388608 bytes
Redo Buffers 2973696 bytes
SQL> CREATE CONTROLFILEREUSE DATABASE "WWL" RESETLOGS ARCHIVELOG
MAXDATAFILES 100
MAXLOGFILES 16
MAXLOGMEMBERS 3
MAXINSTANCES 8
MAXDATAFILES 100
5 MAXINSTANCES 8
6 MAXLOGHISTORY 292
7 LOGFILE
8 GROUP 1 '/DBBak2/oradata/WWL/redo01.log' SIZE 30M,
GROUP 3 '/DBBak2/oradata/WWL/redo03.log' SIZE 30M,
10 GROUP 4 (
) SIZE 128M,
GROUP 5 (
'/DBBak2/oradata/WWL/redo5a.log',
'/DBBak2/oradata/WWL/redo4a.log',
'/DBBak2/oradata/WWL/redo4b.log'
) SIZE 128M,
GROUP 5 (
'/DBBak2/oradata/WWL/redo5a.log',
'/DBBak2/oradata/WWL/redo5b.log'
) SIZE 128M,
GROUP 6 (
'/DBBak2/oradata/WWL/redo6a.log',
'/DBBak2/oradata/WWL/redo6b.log'
) SIZE 128M,
GROUP 7 (
'/DBBak2/oradata/WWL/redo7a.log',
'/DBBak2/oradata/WWL/redo7b.log'
) SIZE 128M
DATAFILE
'/DBBak2/oradata/WWL/system01.dbf',
'/DBBak2/oradata/WWL/undotbs01.dbf',
'/DBBak2/oradata/WWL/sysaux01.dbf',
'/DBBak2/oradata/WWL/users01.dbf',
'/DBBak2/oradata/WWL/wwl01.dbf',
'/DBBak2/oradata/WWL/wwl02.dbf',
'/DBBak2/oradata/WWL/wwl03.dbf'
CHARACTER SET ZHS16CGB231280
35 ;
Control file created.
6.8 以resetlog模式启动数据库:
SQL> alter database openresetlogs;
6.9 删除原redo日志文件,重建redo新日志文件组到新的磁盘上:
SQL> alterdatabase drop logfile group 1;
Databasealtered.
SQL> alterdatabase drop logfile group 2;
Databasealtered.
SQL> alterdatabase drop logfile group 3;
Database altered.
SQL>
SQL> alter database addlogfile group 4 ('/DBBak2/oradata/WWL/redo4a.log','/DBBak2/oradata/WWL/redo4b.log')size 128M;
Database altered.
SQL> alter database addlogfile group 5('/DBBak2/oradata/WWL/redo5a.log','/DBBak2/oradata/WWL/redo5b.log') size 128M;
Database altered.
SQL> alter database addlogfile group 6('/DBBak2/oradata/WWL/redo6a.log','/DBBak2/oradata/WWL/redo6b.log') size 128M;
Database altered.
SQL> alter database addlogfile group 7 ('/DBBak2/oradata/WWL/redo7a.log','/DBBak2/oradata/WWL/redo7b.log')size 128M;
Database altered.
SQL>
6.10 查看日志组信息及状态:
SQL> select * from v$log;
GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARC STATUS FIRST_CHANGE# FIRST_TIM
---------- -------------------- ---------- ---------- --- ---------------- ------------- ---------
4 1 0 134217728 2 YES UNUSED 0
5 1 0 134217728 2 YES UNUSED 0
6 1 0 134217728 2 YESUNUSED 0
7 1 1 134217728 2 NO CURRENT 1263590 06-JUL-12
SQL> alter system switchlogfile;
System altered.
SQL> alter system switchlogfile;
System altered.
SQL> alter system switchlogfile;
System altered.
SQL> alter system switchlogfile;
System altered.
SQL> select * from v$log;
GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARC STATUS FIRST_CHANGE# FIRST_TIM
---------- -------------------- ---------- ---------- --- ---------------- ------------- ---------
4 1 2 134217728 2 YES INACTIVE 1263859 06-JUL-12
5 1 3 134217728 2 YESINACTIVE 1263861 06-JUL-12
6 1 4 134217728 2 YES INACTIVE 1263863 06-JUL-12
7 1 5 134217728 2 NO CURRENT 1263866 06-JUL-12
SQL>
至此恢复完成。