Wednesday, March 27, 2013

ASM Metadata Backup and Restore

ASMCMD md_backup and md_restore use to create backup/restore of asm disk group metadata information. This backup restores all the ASM directory structures, otherwise you have to create complete directory structure before restoring databases.

Syntax: md_backup -b <<backupfilename>> -G <<diskgroup>>
 
When you restore RMAN backup to a lost diskgroup or to a different server you will get errors something like

ORA-01119: error in creating database file ...
ORA-17502: ksfdcre:4 Failed to create file ...
ORA-15001: diskgroup "DATA" does not exist or is not mounted

You have two options to restore :

1. Use SET newname for datafile <<fileno#>> to <<new diskgroup>> or db_file_name_convert option to restore these files to new disk group.

2. Recreate ASM diskgroup manually and other user defined directory structures inside that diskgroup.
Let try this with this example.

Example: For this example I will create different directories paths and one tablespace ts1 with 2 datafiles on DATA2 disk group. We will take a tablespace backup, DATA2 diskgroup metadata backup. We will restore DATA2 and it's directory tree with md_restore and tablespace datafiles from the RMAN backup.


ASMCMD> cd DATA2
ASMCMD>mkdir mydir1
ASMCMD>mkdir mydir2
ASMCMD>ls -l
Type Redund Striped Time Sys Name
                                            N mydir2/
                                            N mydir1/

ASMCMD> cd mydir1
ASMCMD> cd mydir1
ASMCMD> ls -l
ASMCMD>mkdir ts1_dir
ASMCMD>mkdir ts2_dir
ASMCMD>ls -l
Type Redund Striped Time Sys Name
                                            N ts1_dir/
                                            N ts2_dir/

Create a tablespace and create one table inside it.
SQL> create tablespace ts1 datafile '+DATA2/test1.dbf' size 1m;
Tablespace created.

SQL> alter tablespace ts1 add datafile '+DATA2/ts2.dbf' size 2m;
Tablespace altered

SQL> connect scott/tiger

SQL> create table test tablespace ts1
as select * from user_objects;
Table created

SQL> select count(1) from test;
COUNT(1)
----------
7

Take the ASM DATA2 diskgroup metadata backup

ASMCMD> md_backup data2asm_backup -G DATA2
Disk group metadata to be backed up: DATA2
Current alias directory path: mydir1/ts2_dir
Current alias directory path: mydir1
Current alias directory path: mydir2
Current alias directory path: mydir1/ts1_dir
Current alias directory path: TEST
Current alias directory path: TEST/DATAFILEST/DATAFILE

ASMCMD> exit

$ ls -lt
-rw-r--r-- 1 oracle oinstall 13418 Nov 20 13:03 data2aasm_backup

Take RMAN tablespace ts1 backup with following commands.

RMAN> run {
2> allocate channel c1 type disk;
3> backup tablespace ts1 format "/backup/test/ts1_%s_%t";
4> }
using target database control file instead of recovery catalog
allocated channel: c1
channel c1: sid=51 instance=TEST1 devtype=DISK
Starting backup at 20-NOV-10
channel c1: starting full datafile backupset
channel c1: specifying datafile(s) in backupset
input datafile fno=00007 name=+DATA2/ts2.dbf
input datafile fno=00006 name=+DATA2/ts1.dbf
channel c1: starting piece 1 at 20-NOV-10
channel c1: finished piece 1 at 20-NOV-10
piece handle=/backup/test/ts1_11_735580273 tag=TAG20101120T155112 comment=NONE
channel c1: backup set complete, elapsed time: 00:00:01
Finished backup at 20-NOV-10
released channel: c1
RMAN>
RMAN>
RMAN> **end-of-file**
 
 
SQL> alter tablespace ts1 offline;
Tablespace altered.
 
Now drop the DATA2 disk group with force option.

$asmcmd
ASMCMD> dropdg data2
ORA-15039: diskgroup not dropped
ORA-15053: diskgroup "DATA2" contains existing files (DBD ERROR: OCIStmtExecute)
ASMCMD>dropdg -r data2
ASMCMD>
 
SQL>connect scott/tiger
 
SQL> select * from test;
select * from test
*
ERROR at line 1:
ORA-00376: file 6 cannot be read at this time
ORA-01110: data file 6: '+DATA2/ts1.dbf'

It's time to restore ts1 tablespace files from RMAN backup.

RMAN> run {
2> allocate channel c1 type disk format '/backup/test/ts1_%s_%t' ;
3> restore tablespace ts1 ;
4> }
using target database control file instead of recovery catalog
allocated channel: c1
channel c1: sid=169 instance=TEST1 devtype=DISK
Starting restore at 20-NOV-10
channel c1: starting datafile backupset restore
channel c1: specifying datafile(s) to restore from backup set
restoring datafile 00006 to +DATA2/ts1.dbf
restoring datafile 00007 to +DATA2/ts2.dbf
channel c1: reading from backup piece /backup/test/ts1_11_735580273
ORA-19870: error reading backup piece /backup/test/ts1_11_735580273
ORA-19504: failed to create file "+DATA2/ts2.dbf"
ORA-17502: ksfdcre:3 Failed to create file +DATA2/ts2.dbf
ORA-15001: diskgroup "DATA2" does not exist or is not mounted <---- No diskgroup exists
ORA-15001: diskgroup "DATA2" does not exist or is not mounted <---- No such diskgroup exists
failover to previous backup
creating datafile fno=7 name=+DATA2/ts2.dbf
released channel: c1
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of restore command at 11/20/2010 15:57:13
ORA-01119: error in creating database file '+DATA2/ts2.dbf'
ORA-17502: ksfdcre:4 Failed to create file +DATA2/ts2.dbf
ORA-15001: diskgroup "DATA2" does not exist or is not mounted
ORA-15001: diskgroup "DATA2" does not exist or is not mounted
 
Lets use ASM md_restore command to create DATA2 diskgroup from backup. This will restore all the metadata information and create directory structure.
 
$ asmcmd
ASMCMD> md_restore disk2asm_backup
Current Diskgroup metadata being restored: DATA2
Diskgroup DATA2 created!
System template ONLINELOG modified!
System template AUTOBACKUP modified!
System template ASMPARAMETERFILE modified!
System template OCRFILE modified!
System template ASM_STALE modified!
System template OCRBACKUP modified!
System template PARAMETERFILE modified!
System template ASMPARAMETERBAKFILE modified!
System template FLASHFILE modified!
System template XTRANSPORT modified!
System template DATAGUARDCONFIG modified!
System template TEMPFILE modified!
System template ARCHIVELOG modified!
System template CONTROLFILE modified!
System template DUMPSET modified!
System template BACKUPSET modified!
System template FLASHBACK modified!
System template DATAFILE modified!
System template CHANGETRACKING modified!
Directory +DATA2/mydir1 re-created!
Directory +DATA2/TEST re-created!
Directory +DATA2/mydir2 re-created!
Directory +DATA2/mydir1/ts2_dir re-created!
Directory +DATA2/mydir1/ts1_dir re-created!
Directory +DATA2/TEST/DATAFILE re-created!

ASMCMD>
Restore tablespace ts1 datafiles from RMAN backups

RMAN> run {
2> allocate channel c1 type disk format '/backup/test/ts1_%s_%t' ;
3> restore tablespace ts1 ;
4> }
using target database control file instead of recovery catalog
allocated channel: c1
channel c1: sid=167 instance=TEST1 devtype=DISK
Starting restore at 20-NOV-10
channel c1: starting datafile backupset restore
channel c1: specifying datafile(s) to restore from backup set
restoring datafile 00006 to +DATA2/ts1.dbf
restoring datafile 00007 to +DATA2/ts2.dbf
channel c1: reading from backup piece /backup/test/ts1_11_735580273
channel c1: restored backup piece 1
piece handle=/backup/test/ts1_11_735580273 tag=TAG20101120T155112
channel c1: restore complete, elapsed time: 00:00:01
Finished restore at 20-NOV-10
released channel: c1
RMAN>
RMAN>

SQL> alter tablespace ts1 online;
alter tablespace ts1 online
*
ERROR at line 1:
ORA-01113: file 6 needs media recovery
ORA-01110: data file 6: '+DATA2/ts1.dbf'
 
SQL> recover tablespace ts1;
Media recovery complete.
SQL> alter tablespace ts1 online;
Tablespace altered.
SQL> alter tablespace ts1 online;
Tablespace altered.
SQL> connect scott/tiger
Connected.
SQL> select count(1) from test;
COUNT(1)
----------
7

No comments:

Post a Comment