一、前期准备
设置RMAN参数
--保留策略
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;
--配置备份优化
CONFIGURE BACKUP OPTIMIZATION ON;
--自动备份控制文件
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/backup/rman/controlfile/%F';
--开启块跟踪功能
alter database enable block change tracking using file '+DATA/<ORACLE_SID>/change_tracking.dbf';
select * from v$block_change_tracking;
二、备份策略
使用差异增量备份
日 一 二 三 四 五 六 日
0 2 2 1 2 2 2 0
全 一 一 三 一 一 一 全
三、RMAN脚本
0级备份脚本
#!/bin/bash
export TMP=/tmp
export TMPDIR=$TMP
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.2.0.4/db_1
export ORACLE_SID=xxx
export ORACLE_TERM=xterm
export PATH=/usr/sbin:/$PATH
export PATH=$ORACLE_HOME/bin:$PATH:/u01/app/11.2.0.4/grid/bin:/u01/app/oracle/product/11.2.0.4/db_1/OPatch:/usr/local/openssh/bin
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib
export CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib
rman target / <<EOF
run{
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
allocate channel c4 type disk;
crosscheck archivelog all;
backup as compressed backupset incremental level 0 format '/backup/ehr_bak/%d_%T_%U_0.bak' database plus archivelog;
sql'alter system archive log current';
backup spfile;
delete noprompt obsolete;
release channel c1;
release channel c2;
release channel c3;
release channel c4;
}
quit;
EOF
1级备份脚本
#!/bin/bash
export TMP=/tmp
export TMPDIR=$TMP
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.2.0.4/db_1
export ORACLE_SID=ehr1
export ORACLE_TERM=xterm
export PATH=/usr/sbin:/$PATH
export PATH=$ORACLE_HOME/bin:$PATH:/u01/app/11.2.0.4/grid/bin:/u01/app/oracle/product/11.2.0.4/db_1/OPatch:/usr/local/openssh/bin
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib
export CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib
rman target / <<EOF
run{
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
allocate channel c4 type disk;
crosscheck archivelog all;
backup as compressed backupset incremental level 1 format '/backup/ehr_bak/%d_%T_%U_1.bak' database plus archivelog;
sql'alter system archive log current';
backup spfile;
delete noprompt obsolete;
release channel c1;
release channel c2;
release channel c3;
release channel c4;
}
quit;
EOF
2级备份脚本
#!/bin/bash
export TMP=/tmp
export TMPDIR=$TMP
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.2.0.4/db_1
export ORACLE_SID=ehr1
export ORACLE_TERM=xterm
export PATH=/usr/sbin:/$PATH
export PATH=$ORACLE_HOME/bin:$PATH:/u01/app/11.2.0.4/grid/bin:/u01/app/oracle/product/11.2.0.4/db_1/OPatch:/usr/local/openssh/bin
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib
export CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib
rman target / <<EOF
run{
sql'alter system archive log current';
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
allocate channel c4 type disk;
crosscheck archivelog all;
backup as compressed backupset incremental level 2 format '/backup/ehr_bak/%d_%T_%U_2.bak' database plus archivelog;
sql'alter system archive log current';
backup spfile;
delete noprompt obsolete;
release channel c1;
release channel c2;
release channel c3;
release channel c4;
}
quit;
EOF
四、设置定时作业
1、设置脚本权限
chmod -R 777 ehrrman0.sh
chmod -R 777 ehrrman1.sh
chmod -R 777 ehrrman2.sh
2、设置用户权限
chage -M 99999 oracle
3、设置crontab任务
crontab –e
0 2 * * 0 sh /backup/scripts/ehrrman0.sh >> /backup/scripts/log/backup0.log 2>&1
0 2 * * 3 sh /backup/scripts/ehrrman1.sh >> /backup/scripts/log/backup1.log 2>&1
0 2 * * 1,2,4,5,6 sh /backup/scripts/ehrrman2.sh >> /backup/scripts/log/backup2.log 2>&1
五、一个小插曲
设置好脚本后,测试时总是提示
:command not found
message file RMAN<lang>.msb not found
Verify that ORACLE_HOME is set proerly
检查ORACLE_HOME设置没有问题
最后
我一直是sh ehrrman0.sh执行,然后就提示这些错误,突然用./ehrrman0.sh执行试一下,结果提示source /home/oracle/.bash_profile^M ,我一看右边有个^M,那就是后边是不是有空格什么的,然后一行一行删,再执行还是提示相同的错误。在网上查询后发现是脚本格式问题,用vim编辑脚本文件,使用 :set ff?命令发现是dos格式的,手动设置为set ff=unix,执行就好了,然后删除此脚本,重新新建脚本,再试也没有问题了。