備份資料庫A
備份腳本:
rman target / log=/u01/rec_backup.log<<EOF
run
{
allocate channel ch1 device type disk; ----開啟通道
allocate channel ch2 device type disk;
sql 'alter system archive log current'; ----切歸檔日志
backup database format '/u01/RMAN/backup/db_%d_%T_%U'; ----備份的資料檔案的路徑
sql 'alter system archive log current'; ----切歸檔日志
backup archivelog all format '/u01/RMAN/backup/arch_%t_%s' delete all input; -----備份歸檔日志并删除歸檔歸檔(當然也可以不删)
backup format '/u01/RMAN/backup/con_%s_%p' current controlfile; ----備份控制檔案
crosscheck backup; ----檢查備份
crosscheck archivelog all; -----檢查歸檔(為什麼要檢查,因為檢查了一下的指令才可以使用)
delete noprompt expired backup; ----删除過期備份
delete noprompt obsolete; -----删除過期檔案
delete noprompt backup of database completed before 'sysdate -15'; -----删除15天前的備份
delete noprompt archivelog all; ----删除過期歸檔
delete noprompt backup of archivelog all completed before 'sysdate -15'; -----删除15天前的歸檔
release channel ch1; ----釋放通道
release channel ch2; -----釋放通道
}
EOF
移動備份檔案
移動備份腳本裡的資料檔案、控制檔案、歸檔日志檔案通過scp指令移動到資料庫B指定目錄下
建立pfile
在資料庫A,mount下,通過spfile建立pfile(create spfile from pfile),到cd $ORACLE_HOME/dbs下找到剛建立的pfile檔案,
SCP移動到資料庫B上的$ORACLE_HOME/dbs下,資料庫恢複的時候啟庫要用
移動環境變量
把資料庫A上的ORACLE的環境變量複制到資料庫B上,source .bash_profile 讓環境變量生效
檢查控制檔案
檢查控制檔案中路徑是否都建立,要拉起到nomount了
通過spfile啟庫
資料庫B上通過從資料庫A拉過來的spfile啟庫到nomount,startup nomount pfile =’xx/xx/xxinit.ora’; 需要指定pfile路徑
恢複控制檔案
恢複從資料庫A上SCP過來的控制檔案
先進入rman
Rman target /
恢複:restore controlfile from ‘xx/wwe/控制檔案名’;
并資料庫啟動到mount; alter database mount;
恢複資料檔案
注冊備份集
在跑RMAN恢複資料檔案之前需要先注冊一下,注冊備份集 ,從資料庫A拷貝過來的資料檔案,歸檔日志檔案,需要注冊到資料庫B中,讓資料庫B知道,這些是恢複檔案
catalog start with '/u01/backup/';
會讀到你拷貝的資料檔案
寫rman恢複腳本
rman target / log=/u01/recover/rec_recover.log<<EOF
run
{
allocate channel ch1 device type disk;
allocate channel ch2 device type disk;
set until time "to_date('2018-09-12 18:50:49', 'YYYY-MM-DD HH24:MI:SS')";
set newname for datafile 1 to "/u01/app/oracle/oradata/arorcl/datafile/system01.dbf";
set newname for datafile 2 to "/u01/app/oracle/oradata/arorcl/datafile/sysaux01.dbf";
set newname for datafile 3 to "/u01/app/oracle/oradata/arorcl/datafile/undotbs01.dbf";
set newname for datafile 4 to "/u01/app/oracle/oradata/arorcl/datafile/users01.dbf";
set newname for tempfile 1 to "/u01/app/oracle/oradata/arorcl/datafile/temp01.dbf";
restore database;
switch datafile all;
switch tempfile all;
recover database;
release channel c1;
release channel c2;
SQL "ALTER DATABASE RENAME FILE ''/u01/app/oracle/oradata/arorcl/redo03.log'' to ''/u01/app/oracle/oradata/arorcl/redo/redo03.log'' ";
SQL "ALTER DATABASE RENAME FILE ''/u01/app/oracle/oradata/arorcl/redo02.log'' to ''/u01/app/oracle/oradata/arorcl/redo/redo02.log'' ";
SQL "ALTER DATABASE RENAME FILE ''/u01/app/oracle/oradata/arorcl/redo01.log'' to ''/u01/app/oracle/oradata/arorcl/redo/redo01.log'' ";
sql 'alter database clear logfile group 1';
sql 'alter database clear logfile group 2';
sql 'alter database clear logfile group 3';
}
quit;
EOF
我是指定時間點恢複,時間點是通過select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual; 這條指令查詢出來,後期可能恢複會有問題,歸檔追不到情況,會解決,看下文
運作rman腳本,nohup sh rman.sh &
Tail –f rec_rman.sh :檢視進行恢複的日志
在檢視恢複日志過程中結束後看到日志裡報錯
追不到SCN OF 1155225的日志
檢視控制檔案裡歸檔日志記錄資訊
最下面一個是未追到的,那可以試着網上跳一個,選擇1155217恢複,指定SCN的恢複方法
恢複腳本:
rman target / log=/u01/recover/rec_recover1.log<<EOF
run
{
allocate channel ch1 device type disk;
allocate channel ch2 device type disk;
set until scn 1155217;
recover database;
release channel c1;
release channel c2;
SQL "ALTER DATABASE RENAME FILE ''/u01/app/oracle/oradata/arorcl/redo03.log'' to ''/u01/app/oracle/oradata/arorcl/redo/redo03.log'' ";
SQL "ALTER DATABASE RENAME FILE ''/u01/app/oracle/oradata/arorcl/redo02.log'' to ''/u01/app/oracle/oradata/arorcl/redo/redo02.log'' ";
SQL "ALTER DATABASE RENAME FILE ''/u01/app/oracle/oradata/arorcl/redo01.log'' to ''/u01/app/oracle/oradata/arorcl/redo/redo01.log'' ";
sql 'alter database clear logfile group 1';
sql 'alter database clear logfile group 2';
sql 'alter database clear logfile group 3';
}
quit;
EOF
繼續報錯
從英文字面上翻譯是,datafile 1必須恢複比SCN 1155217舊的資料檔案,翻過來說,其實已經恢複到1155217了
那接下去可以查下資料表頭和資料檔案中資料檔案的資訊
select a.name,a.checkpoint_change# “start_SCN”,
> b.checkpoint_change# “last_SCN”
> from v$datafile_header a, v$datafile b
> where a.file#=b.file#;
這裡解釋下關于oracle動态視圖v$datafile和v$datafile_header
v$datafile是從oracle的控制檔案中獲得的資料檔案的資訊
v$datafile_header是從資料檔案的頭部
在正常運作下,兩者的檢查點SCN值是一緻的,但當datafile出現損壞時可以用v$datafile_header确定應用重做的起始SCN,而v$datafile可确定應用重做的結束SCN值
查詢結果資訊資料一緻,可以開庫,但我redo路徑改了,還是需要先在sql裡改下路徑
ALTER DATABASE RENAME FILE '/u01/app/oracle/oradata/arorcl/redo01.log' TO '/u01/app/oracle/oradata/arorcl/redo/redo01.log';
ALTER DATABASE RENAME FILE '/u01/app/oracle/oradata/arorcl/redo02.log' TO '/u01/app/oracle/oradata/arorcl/redo/redo02.log';
ALTER DATABASE RENAME FILE '/u01/app/oracle/oradata/arorcl/redo03.log' TO '/u01/app/oracle/oradata/arorcl/redo/redo03.log';
alter database clear logfile group 1;
alter database clear logfile group 2;
alter database clear logfile group 3;
開庫
Alter database open resetlogs;
單個恢複
run{
set newname for datafile 1 to "+DATAGR/arorcl/DATAFILE/system01.dbf";
restore datafile 1;
}
run{
set newname for datafile 2 to "+DATAGR/arorcl/DATAFILE/sysaux01.dbf";
restore datafile 2;
}
run{
set newname for datafile 3 to "+DATAGR/arorcl/DATAFILE/undotbs01.dbf";
restore datafile 3;
}
run{
set newname for datafile 4 to "+DATAGR/arorcl/DATAFILE/users01.dbf";
restore datafile 4;
}
run{
set newname for tempfile 1 to "+DATAGR/arorcl/DATAFILE/temp01.dbf";
restore tempfile 1;
}
單個Switch all
run{
set newname for datafile 1 to "+DATAGR/arorcl/DATAFILE/system01.dbf";
switch datafile all;
}