Home | Mirror | Search

3. RMAN

3.1. 資料庫模式

資料庫必須是歸檔模式,使用下面SQL查詢當前資料庫模式

		
SQL> archive log list;
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence     66
Next log sequence to archive   68
Current log sequence           68
		
		

如果已經是歸檔模式可跳過此步,下面是切換伺服器到歸檔模式的方法:

		
[oracle@oracle ~]$ sqlplus /nolog (啟動sqlplus)

SQL> conn / as sysdba (以DBA身份連接資料庫)

SQL> shutdown immediate; (立即關閉資料庫)

SQL> startup mount (啟動實例並加載資料庫,但不打開)

SQL> alter database archivelog; (更改資料庫為歸檔模式)

SQL> alter database open; (打開資料庫)

SQL> alter system archive log start; (啟用自動歸檔)

SQL> exit (退出)
		
		

rman 採用塊備份,查看塊信息使用下面SQL語句

select * from dba_extents
		

3.2. 完全備份

run {
allocate channel d1 type disk;
backup format='/opt/oracle/backup/%d_%N_%s.bk' tablespace users;
release channel d1;
}
		

下面讓我來演示給你看

		
[oracle@oracle ~]$ mkdir /opt/oracle/backup
[oracle@oracle ~]$ rman target sys/passw0rd nocatalog

Recovery Manager: Release 11.2.0.1.0 - Production on Mon Jun 13 12:03:20 2011

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

connected to target database: WCSDB (DBID=2970836713)
using target database control file instead of recovery catalog

RMAN> run {
allocate channel d1 type disk;
backup format='/opt/oracle/backup/%d_%N_%s.bk' tablespace users;
release channel d1;
} 2> 3> 4> 5>

allocated channel: d1
channel d1: SID=36 device type=DISK

Starting backup at 13-JUN-11
channel d1: starting full datafile backup set
channel d1: specifying datafile(s) in backup set
input datafile file number=00004 name=/opt/oracle/oradata/wcsdb/users01.dbf
channel d1: starting piece 1 at 13-JUN-11
channel d1: finished piece 1 at 13-JUN-11
piece handle=/opt/oracle/backup/WCSDB_USERS_1.bk tag=TAG20110613T120325 comment=NONE
channel d1: backup set complete, elapsed time: 00:00:01
Finished backup at 13-JUN-11

released channel: d1

RMAN>
		
		

查看備份結果

		
RMAN> list backup of tablespace users;


List of Backup Sets
===================


BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
1       Full    1.30M      DISK        00:00:01     13-JUN-11
        BP Key: 1   Status: AVAILABLE  Compressed: NO  Tag: TAG20110613T120325
        Piece Name: /opt/oracle/backup/WCSDB_USERS_1.bk
  List of Datafiles in backup set 1
  File LV Type Ckp SCN    Ckp Time  Name
  ---- -- ---- ---------- --------- ----
  4       Full 1561686    13-JUN-11 /opt/oracle/oradata/wcsdb/users01.dbf
		
		

備份目錄下面是剛剛生成的備份檔案

		
$ ls /opt/oracle/backup
WCSDB_USERS_1.bk
		
		

例 21.1. full backup

			
# crontab -u oracle -l
0 1 * * * /opt/oracle/rman/backup.sh
			
			
			
# cat .bash_profile
export ORACLE_BASE=/opt/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1
export ORACLE_SID=orcl
export PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib

# cat /opt/oracle/rman/backup.sh
rman target sys/passw0rd@orcl msglog=/opt/oracle/rman/log/`date +"%Y%m%d%H%M"`.log cmdfile=/opt/oracle/rman/orcl.rman
			
			
			
# cat /opt/oracle/rman/orcl.rman
run {
   allocate channel c1 type disk;
   allocate channel c2 type disk;
   configure retention policy to recovery window of 6 days;
   configure controlfile autobackup on;
   configure backup optimization  on;
   configure device type disk parallelism 4 backup type to compressed backupset;
   configure controlfile autobackup format for device type disk to '/opt/oracle/backup/rman/%F.ctl';
   sql 'alter system switch logfile';
   backup full database format '/opt/oracle/backup/rman/df_%t_%s_%p.bak' tag='full' include current controlfile;
   sql 'alter system archive log current';
   backup  archivelog all  format '/opt/oracle/backup/rman/arc_%U_%s.bak' delete all input;
   release channel c1;
   release channel c2;
}
crosscheck backup;
delete noprompt expired backup;
delete noprompt obsolete;
exit
			
			

3.3. 增量備份

RMAN> BACKUP INCREMENTAL LEVEL 0 DATABASE;
RMAN> BACKUP INCREMENTAL LEVEL 1 TABLESPACE SYSTEM DATAFILE 'ora_home/oradata/ tools01.dbf';
RMAN> BACKUP INCREMENTAL LEVEL = 1 CUMULATIVE TABLESPACE users;
		

網上發現的腳步,署名不詳

run{
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
backup full tag 'dbfull' format '/u01/oradata/backup/full%u_%s_%p' database include current controlfile;
sql 'alter system archive log current';
backup filesperset 3 format '/u01/oradata/backup/arch%u_%s_%p' archivelog all delete input;
release channel c1;
release channel c2;
release channel c3;
}

零級備份腳本
run{
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
backup incremental level 0 tag ‘db0’ format ‘/u01/oradata/backup/db0%u_%s_%p’ database skip readonly;
sql ‘alter system archive log current’;
backup filesperset 3 format ‘/u01/oradata/backup/arch%u_%s_%p’ archivelog all delete input;
release channel c1;
release channel c2;
release channel c3;
}

一級備份腳本
run{
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
backup incremental level 1 tag ‘db1’ format ‘/u01/oradata/backup/db1%u_%s_%p’ database skip readonly;
sql ‘alter system archive log current’;
backup filesperset 3 format ‘/u01/oradata/backup/arch%u_%s_%p’ archivelog all delete input;
release channel c1;
release channel c2;
release channel c3;
}
		

網上發現的腳步,沒有署名,我也沒有測試過,僅供參考

		
do_rman.sh

#!/bin/bash
#set env
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
export PATH=$ORACLE_HOME/bin:$PATH

TARGET_SID=$TARGET_SID
RMAN_SID=$RMAN_SID

export PATH=$ORACLE_HOME/bin:$PATH
DATE=`date +%w`
DATE_2=`date +%Y%m%d`
BACKUP_PATH=$ORACLE_BASE/admin/$ORACLE_SID/rman/backup
LEVEL=$@
BIN=$ORACLE_HOME/bin
# Delete the data backuped last time

rm -rf $BACKUP_PATH/data/$DATE/*

if [ $# != 1 ]; then
	echo "usage: do_rman.sh n
	where n is the rman backup level(0,1,2 is permitted)."
	exit 1
fi
if [ $@ -ne 0 -a $@ -ne 1 -a $@ -ne 2 ]; then
	echo "usage: do_rman.sh n
	where n is the rman backup level(Only 0,1,2 is permitted)."
	exit 2
fi
echo "[do_rman] rman is starting."
if [ $LEVEL = 0 ]; then
	$BIN/rman log $BACKUP_PATH/log/level.$TARGET_SID.$LEVEL.$DATE_2.log < connect target /;
	connect catalog rman/rman@$RMAN_SID;
	resync catalog;
	run{
		allocate channel c1 type disk ;
		crosscheck backupset of archivelog all ;
		backup filesperset 3 format '$BACKUP_PATH/data/$DATE/arch.%d.live.$LEVEL.%t'(archivelog from time 'sysdate-7' all delete input) ;
		delete noprompt expired backupset of archivelog all ;
		release channel c1 ;
	}

	run{
		allocate channel c2 type disk ;
		crosscheck backupset of database ;
		backup incremental level $LEVEL filesperset 3 format '$BACKUP_PATH/data/$DATE/data.%d.live.$LEVEL.%t'(database include current controlfile) ;
		delete noprompt expired backupset of database ;
		delete noprompt obsolete ;
		release channel c2 ;
	}
	exit;
EOF

else
	$BIN/rman log $BACKUP_PATH/log/level.$TARGET_SID.$LEVEL.$DATE_2.log < connect target sys/sys202;
	connect catalog rman/rman@$RMAN_SID;
	resync catalog;
	run{
		allocate channel c1 type disk ;
		crosscheck backupset of archivelog all ;
		backup filesperset 3 format '$BACKUP_PATH/data/$DATE/arch.%d.live.$LEVEL.%t' (archivelog from time 'sysdate-1' all) ;
		delete noprompt expired backupset of archivelog all ;
		release channel c1 ;
	}

	run{
		allocate channel c2 type disk ;
		crosscheck backupset of database ;
		backup incremental level $LEVEL filesperset 3 format '$BACKUP_PATH/data/$DATE/data.%d.live.$LEVEL.%t' (database include current controlfile) ;
		delete noprompt expired backupset of database ;
		delete noprompt obsolete ;
		release channel c2 ;
	}

	exit;
EOF
fi

echo "[do_rman] rman is success."
		
		

3.4. 恢復資料庫

		
%rman target=rman/rman@mydb

RMAN> startup nomount

RMAN> restore database;

RMAN> recover database;

RMAN> alter database open;
		
		

網上找到的文檔,署名不詳,我沒有測試過是否可以運行

		
設定參數:
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/arch/rman/controlfile%F.ctnl';
CONFIGURE CHANNEL 1 DEVICE TYPE DISK FORMAT   '/arch/rman/full%t.bak';

資料庫rman 全備
rman>backup database plus archivelog delete input;
備份產生的三個檔案
-rw-r----- 1 oracle oinstall   7143424 Jan 28 18:05 controlfilec-2719028776-20100128-01.ctnl
-rw-r----- 1 oracle oinstall  41074688 Jan 28 18:03 full709495428.bak
-rw-r----- 1 oracle oinstall 763379712 Jan 28 18:05 full709495432.bak
-rw-r----- 1 oracle oinstall     17920 Jan 28 18:05 full709495518.bak


rman恢復
------------------
1、啟動資料庫到 nomount 狀態
$sqlplus / as sysdba
SQL> startup nomount

2、spfile 恢復
$rman nocatalog
rman> connect target /
run {
allocate channel c1 DEVICE TYPE DISK format '/arch/rman/controlfile%F.ctnl';
restore spfile to pfile '/arch/pfile.ora' from '/arch/rman/controlfilec-2719028776-20100128-01.ctnl';
release channel c1;
}

3、控制檔案恢復
run {
allocate channel c1 DEVICE TYPE DISK format '/arch/rman/controlfile%F.ctnl';
restore controlfile from '/arch/rman/controlfilec-2719028776-20100128-01.ctnl';
release channel c1;
}

4、全庫恢復
在恢復控制檔案的情況下,可以修改數據到 mount狀態,進行全庫的恢復
rman> alter database mount;
run {
allocate channel c1 device type disk format '/arch/rman/full%t.bak';
restore database;
release channel c1;
}

5、恢復archivelog
run {
allocate channel c1 device type disk format '/arch/rman/full%t.bak';
restore archivelog all;
}
run {
allocate channel c1 device type disk format '/arch/rman/full%t.bak';
restore archivelog from logseq=72 until logseq=73;
}

6、redolog 恢復
SQL>recover database using backup controlfile until cancel;
SQL>alter database open resetlogs;  //現在有redolog 產生了,還有temp表空間檔案也生成了或者分開兩步執行

SQL>select * from dual;
全庫成功恢復
		
		

3.5. 是用tar打包rman檔案

# find /opt/oracle/rman/ -type f -mtime 1 -printf "%CY-%Cm-%Cd %Cr %s %f\n"

tar --newer="2011-07-04" -zcvf backup.tar.gz /opt/oracle/rman/

find /opt/oracle/rman/ -type f -mtime 1 | xargs tar zcvf oracle_2011-07-04.tgz

rsync -azP `find /opt/oracle/rman/ -type f -mtime 1` test@172.16.0.5:/home/test
		
comments powered by Disqus