一、准备工作
查看是否处在归档模式
SQL> archive log list;
Database log mode No Archive Mode
Automatic archival Disabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 28
Current log sequence 30
如果是"No Archive Mode"
修改为归档模式
首先要关闭数据库,启动到mount状态。
SQL> shutdown immediate;
SQL> startup mount;
修改为归档模式
SQL>alter database archivelog;
验证修改结果
SQL> select log_mode from v$database;
LOG_MODE
------------
ARCHIVELOG
打开数据库
SQL> alter database open;
二、RMAN Catalog 配置
创建 RMAN Calalog表空间
SQL>create tablespace RMAN_TS datafile '/opt/oracle/oradata/orcl/RMAN_TS01.dbf' size 500M;
--创建用户rman/rman 默认表空间味RMAN_TS允许自由使用
SQL> create user rman identified by rman default tablespace RMAN_TS quota unlimited on RMAN_TS;
用户授权
SQL>grant connect, resource,recovery_catalog_owner to rman;
创建恢复目录
在命令终端
[oracle@localhost ~]$rman catalog rman/rman
RMAN> CREATE CATALOG;
连接,注册目标数据库,同步catalog和控制文件
[oracle@localhost ~]$rman target sys/wxbwer catalog rman/rman
连接成功出现下面的信息
connected to target database: ORCL (DBID=1325399111)
connected to recovery catalog database
RMAN> REGISTER DATABASE;
RMAN> RESYNC CATALOG;
下面是否有注册信息即可
RMAN>LIST INCARNATION;
List of Database Incarnations
DB Key Inc Key DB Name DB ID STATUS Reset SCN Reset Time
------- ------- -------- ---------------- --- ---------- ----------
2 36 ORCL 1325399111 PARENT 1 13-AUG-09
2 4 ORCL 1325399111 CURRENT 754488 25-OCT-12
三、创建RMAN备份脚本
来自:http://blog.csdn.net/robinson_0612/article/details/8029245
##===========================================================
## db_bak_rman.sh
## created by Robinson
## 2011/11/07
## usage: db_bak_rman.sh <$ORACLE_SID> <$BACKUP_LEVEL>
## BACKUP_LEVEL:
## F: full backup
## 0: level 0
## 1: level 1
##============================================================
#!/bin/bash
# User specific environment and startup programs
if [ -f ~/.bash_profile ];
then
. ~/.bash_profile
fi
ORACLE_SID=${1}; export ORACLE_SID
RMAN_LEVEL=${2}; export RMAN_LEVEL
TIMESTAMP=`date +%Y%m%d%H%M`; export TIMESTAMP
DATE=`date +%Y%m%d`; export DATE
#RMAN_DIR=/u02/database/${ORACLE_SID}/backup/rman; export RMAN_DIR
#RMAN_DATA=${RMAN_DIR}/${DATE}; export RMAN_DATA
#RMAN_LOG=/u02/database/${ORACLE_SID}/backup/rman/log export RMAN_LOG
RMAN_DIR=/opt/oracle/oradata/backup/rman; export RMAN_DIR
RMAN_DATA=${RMAN_DIR}/${DATE}; export RMAN_DATA
RMAN_LOG=${RMAN_DIR}/log export RMAN_LOG
# Check rman level
#======================================================================
if [ "$RMAN_LEVEL" == "F" ];
then unset INCR_LVL
BACKUP_TYPE=full
else
INCR_LVL="INCREMENTAL LEVEL ${RMAN_LEVEL}"
BACKUP_TYPE=lev${RMAN_LEVEL}
fi
RMAN_FILE=${RMAN_DATA}/${ORACLE_SID}_${BACKUP_TYPE}_${TIMESTAMP}; export RMAN_FILE
SSH_LOG=${RMAN_LOG}/${ORACLE_SID}_${BACKUP_TYPE}_${TIMESTAMP}.log; export SSH_LOG
MAXPIECESIZE=4G; export MAXPIECESIZE
#Check RMAN Backup Path
#=========================================================================
if ! test -d ${RMAN_DATA}
then
mkdir -p ${RMAN_DATA}
fi
echo "---------------------------------" >>${SSH_LOG}
echo " " >>${SSH_LOG}
echo "Rman Begin to Working ........." >>${SSH_LOG}
echo "Begin time at:" `date` --`date +%Y%m%d%H%M` >>${SSH_LOG}
#Startup rman to backup
#=============================================================================
$ORACLE_HOME/bin/rman log=${RMAN_FILE}.log <<EOF
connect target /
connect catalog rman/rman
run {
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 3 DAYS;
CONFIGURE BACKUP OPTIMIZATION ON;
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '${RMAN_FILE}_%F';
ALLOCATE CHANNEL 'ch1' TYPE DISK maxpiecesize=${MAXPIECESIZE};
ALLOCATE CHANNEL 'ch2' TYPE DISK maxpiecesize=${MAXPIECESIZE};
set limit channel ch1 readrate=10240;
set limit channel ch1 kbytes=4096000;
set limit channel ch2 readrate=10240;
set limit channel ch2 kbytes=4096000;
CROSSCHECK ARCHIVELOG ALL;
DELETE NOPROMPT EXPIRED ARCHIVELOG ALL;
BACKUP
#AS COMPRESSED BACKUPSET
${INCR_LVL}
DATABASE FORMAT '${RMAN_FILE}_%U' TAG '${ORACLE_SID}_${BACKUP_TYPE}_${TIMESTAMP}';
SQL 'ALTER SYSTEM ARCHIVE LOG CURRENT';
BACKUP ARCHIVELOG ALL FORMAT '${RMAN_FILE}_arc_%U' TAG '${ORACLE_SID}_arc_${TIMESTAMP}'
DELETE INPUT;
DELETE NOPROMPT OBSOLETE;
RELEASE CHANNEL ch1;
RELEASE CHANNEL ch2;
}
sql "alter database backup controlfile to ''${RMAN_DATA}/cntl_${BACKUP_TYPE}.bak''";
exit;
EOF
RC=$?
cat ${RMAN_FILE}.log >>${SSH_LOG}
echo "Rman Stop working @ time:"`date` `date +%Y%m%d%H%M` >>${SSH_LOG}
echo >>${SSH_LOG}
echo "------------------------" >>${SSH_LOG}
echo "------ Disk Space ------" >>${SSH_LOG}
df -h >>${SSH_LOG}
echo >>${SSH_LOG}
if [ $RC -ne "0" ]; then
echo "------ error ------" >>${SSH_LOG}
else
echo "------ no error found during RMAN backup peroid------" >>${SSH_LOG}
rm -rf ${RMAN_FILE}.log
fi
#Remove old backup than 3 days
#============================================================================
RMDIR=${RMAN_DIR}/`/bin/date +%Y%m%d -d "3 days ago"`; export RMDIR
echo >>${SSH_LOG}
echo -e "------Remove old backup than 3 days ------\n" >>${SSH_LOG}
if test -d ${RMDIR}
then
rm -rf ${RMDIR}
RC=$?
fi
echo >>${SSH_LOG}
if [ $RC -ne "0" ]; then
echo -e "------ Remove old backup exception------ \n" >>${SSH_LOG}
else
echo -e "------ no error found during remove old backup set peroid------ \n" >>${SSH_LOG}
fi
exit
[oracle@localhost backup]$ pwd
/opt/oracle/oradata/backup
[oracle@localhost backup]$vi db_bak_rman.sh
将上面脚本复制进去,并保存,且设置权限
[oracle@localhost backup]$ chmod 755 db_bak_rman.sh
测试脚本
orcl 为 SID
0: 代表0级备份
[oracle@localhost backup]$ ./db_bak_rman.sh orcl 0
四、crontab 定时任务 以 oralce用户登录
[oracle@localhost backup]$crontab -e
45 23 * * 0 /opt/oracle/oradata/backup/db_bak_rman.sh orcl 0
45 23 * * 1-3 /opt/oracle/oradata/backup/db_bak_rman.sh orcl 2
45 23 * * 4 /opt/oracle/oradata/backup/db_bak_rman.sh orcl 1
45 23 * * 5-6 /opt/oracle/oradata/backup/db_bak_rman.sh orcl 2
以root用户登录
[root@localhost backup]$/etc/init.d/crond restart
Stopping crond: [ OK ]
Starting crond: [ OK ]
脚本的增量备份策略: 周日0级备份,周四1级备份,其他2级备份
差异备份有3个级别:
0级:相当于全备,不同的是0级可用于增量备份,全备不行。
1级:备份自上次0级备份以来的数据
2级:备份自上次备份依赖的数据