--**********************************
-- 基于Linux下 Oracle 备份策略(RMAN)
--**********************************
对于 Oracle 数据库的备份与恢复,尽管存在热备,冷备以及逻辑备份之外,使用最多的莫过于使用RMAN进行备份与恢复。而制定RMAN备份策
略则是基于数据库丢失的容忍程度,即恢复策略来制定。在下面的备份策略中,给出的是一个通用的备份策略。在该备份策略中,使用了catalog方
式来保持备份脚本以及备份信息。在实际环境中应根据具体的情况对此进行相应的调整。
一、步骤
1.确认备份可用空间以及备份路径,根据需要创建相应文件夹
1.对于账户的连接创建一个connect.rcv,该文件包含连接到target 和catalog信息
2.创建通用的脚本用于删除过旧的备份和备份控制文件以及备份归档日志
global_del_obso --删除过旧的备份
global_bkctl --备份控制文件
global_arch --备份归档日志
3.创建0,1,2级增量备份
这三个脚本中均包含第2步的3个脚本,先调用global_del,然后做增量备份,最后备份归档日志global_arch和控制文件global_bkctl
4.创建inc0.rcv,inc1.rcv,inc2.rcv
文件内容包含调用 @@/<dir>/connect.rcv以及run{execute global script scriptname;}exit;
5.编辑第4步的三个文件分别为inc0.sh,inc1.sh,inc2.sh
nohup $ORACLE_HOME/bin/rman cmdfile=/u03/bk/scripts/inc1.rcv log=/u03/bk/scripts/inc0.log append &
6.使用crontab制定备份计划
二、具体实现
演示环境:
系统: CentOS release 4.6 (Final) + Oracle 10g
目标数据库: austin
恢复目录数据库: david
备份目录: /u03/bk
连接脚本所在路径:/u03/bk/scripts
注:监听需要配置好且已启动
1.连接脚本
connect.rcv文件内容
connect catalog rman/rman@david;
connect target sys/oracle@austin;
catalog的通用脚本
rman cmdfile=/u03/bk/scripts/connect.rcv --(在rman中使用外部脚本)
rman catalog rman/rman@david target sys/oracle@austin --使用该脚本连接后创建通用脚本
2.建立通用脚本
--删除不必要的备份
RMAN> create global script global_del_obso comment 'A script for obsolete backup and delete it' {
2> crosscheck archivelog all;
3> delete noprompt expired archivelog all;
4> allocate channel ch1 device type disk;
5> delete noprompt obsolete recovery window of 7 days;
6> release channel ch1;
7> }
--备份控制文件脚本
RMAN> create global script global_bkctl comment 'A script for backup control file'{
2> allocate channel ch1 device type disk;
3> backup as compressed backupset
4> current controlfile reuse
5> format='/u03/bk/backupctl.ctl'
6> tag='bkctl';
7> release channel ch1;
8> }
--备份归档日志脚本
RMAN> create global script global_arch comment "backup archivelog and then delete it" {
2> allocate channel ch1 device type disk;
3> allocate channel ch2 device type disk;
4> sql "alter system archive log current"; --归档当前的联机日志
5> set limit channel ch1 readrate=10240; --(读取速率10M)
6> set limit channel ch1 kbytes=4096000; --(备份片的大小)
7> backup as compressed backupset
8> format='/u03/bk/arch_%d_%U'
9> tag='bkarch'
10> archivelog all delete input;
11> release channel ch1;
12> release channel ch2;
13> }
--使用list查看所有的已建立的全局脚本
list global script names; --(列出所有的脚本)
delete globals script script_name; --(删除脚本)
RMAN> list global script names;
List of Stored Scripts in Recovery Catalog
Global Scripts
Script Name
Description
-----------------------------------------------------------------------
global_arch
backup archivelog and then delete it
global_bkctl
A script for backup control file
global_del_obso
A script for obsolete backup and delete it
3.创建0,1,2级增量备份脚本(注:每个脚本备份前会执行删除过旧的备份,脚本尾部会调用另外两个脚本来备份归档日志及控制文件)
--创建0级增量备份
RMAN> create global script global_inc0 comment "backup database as incrmental level 0"{
2> execute global script global_del_obso;
3> allocate channel ch1 device type disk;
4> allocate channel ch2 device type disk;
5> set limit channel ch1 readrate=10240;
6> set limit channel ch1 kbytes=4096000;
7> set limit channel ch2 readrate=10240;
8> set limit channel ch2 kbytes=4096000;
9> backup as compressed backupset
10> incremental level 0 database
11> format='/u03/bk/inc0_%d_%U'
12> tag='inc0';
13> release channel ch1;
14> release channel ch2;
15> execute global script global_arch;
16> execute global script global_bkctl;
17> }
--创建1级增量备份
RMAN> create global script global_inc1 comment "backup database as incrmental level 1"{
2> execute global script global_del_obso;
3> allocate channel ch1 device type disk;
4> allocate channel ch2 device type disk;
5> set limit channel ch1 readrate=10240;
6> set limit channel ch1 kbytes=4096000;
7> set limit channel ch2 readrate=10240;
8> set limit channel ch2 kbytes=4096000;
9> backup as compressed backupset
10> incremental level 1 database
11> format='/u03/bk/inc1_%d_%U'
12> tag='inc1';
13> release channel ch1;
14> release channel ch2;
15> execute global script global_arch;
16> execute global script global_bkctl;
17> }
--创建2级增量备份
RMAN> create global script global_inc2 comment "backup database as incrmental level 2"{
2> execute global script global_del_obso;
3> allocate channel ch1 device type disk;
4> allocate channel ch2 device type disk;
5> set limit channel ch1 readrate=10240;
6> set limit channel ch1 kbytes=4096000;
7> set limit channel ch2 readrate=10240;
8> set limit channel ch2 kbytes=4096000;
9> backup as compressed backupset
10> incremental level 2 database
11> format='/u03/bk/inc2_%d_%U'
12> tag='inc2';
13> release channel ch1;
14> release channel ch2;
15> execute global script global_arch;
16> execute global script global_bkctl;
17> }
--在rman中检验在rman中写的脚本global_inc0、global_inc1、global_inc2,因为RMAN不会自动检查,下面的语句用来执行脚本(检验)
RMAN> run{
2> execute global script global_inc0;
3> execute global script global_inc1;
4> execute global script global_inc2;
5> }
--查看备份完成情况
list backupset summary;
4.建立shell脚本,让linux自动执行脚本
a. vi inc0.rcv,inc1.rcv ,inc2.rcv --注意不同的文件执行不同的备份脚本
@@/u03/bk/scripts/connect.rcv --(rman下的脚本去调用其他脚本用@@符号)(调用脚本不需要分号)
run{
execute global script gloal_inc0;
}
exit;
b. 编辑shell文件
vi inc0.sh
nohup $ORACLE_HOME/bin/rman cmdfile=/u03/bk/scripts/inc0.rcv log=/u03/bk/scripts/inc0.log append &
vi inc1.sh
nohup $ORACLE_HOME/bin/rman cmdfile=/u03/bk/scripts/inc1.rcv log=/u03/bk/scripts/inc0.log append &
vi inc2.sh
nohup $ORACLE_HOME/bin/rman cmdfile=/u03/bk/scripts/inc2.rcv log=/u03/bk/scripts/inc0.log append &
--注意:nohup与&表示将脚本放入后台执行
c.使用crontab建立一个备份计划
crontab -e
#min hour date mon day(星期) command
30 1 * * 0 /u03/bk/scripts/inc0.sh
30 1 * * 1 /u03/bk/scripts/inc2.sh
30 1 * * 2 /u03/bk/scripts/inc2.sh
30 1 * * 3 /u03/bk/scripts/inc2.sh
30 1 * * 4 /u03/bk/scripts/inc1.sh
30 1 * * 5 /u03/bk/scripts/inc2.sh
30 1 * * 6 /u03/bk/scripts/inc2.sh
d.重启crontab服务(如果没有启动)
# /sbin/service crond status --用于检查crontab 服务状态
# /sbin/service crond stop //关闭服务
# /sbin/service crond restart //重启服务
# /sbin/service crond reload //重新载入配置
使crontab服务在系统启动的时候自动启动:
在/etc/rc.d/rc.local这个脚本的末尾加上:
/sbin/service crond start
e.从上面的备份策略来看,即
周日执行0级增量备份,相当于全备
周一,周二,周三执行2级增量备份
周四执行1级增量备份
周五,周六执行2级增量备份
f.编辑好的shell 脚本测试
chmod 755 *.sh --给shell脚本加权限
测试脚本 例如./inc0.sh