--**********************************
-- 基于Linux下 Oracle 备份策略(RMAN)
--**********************************
对于 Oracle 数据库的备份与恢复,尽管存在热备,冷备以及逻辑备份之外,使用最多的莫过于使用RMAN进行备份与恢复。而制定RMAN备份策略则是基于数据库丢失的容忍程度,即恢复策略来制定。在下面的备份策略中,给出的是一个通用的备份策略。在该备份策略中,使用了nocatalog方式来保持备份脚本以及备份信息。在实际环境中应根据具体的情况对此进行相应的调整。因为我现在目前接手的是一台单实例,一台RAC,所以么有必要做catalog方式。
一、步骤
1.确认备份可用空间以及备份路径,根据需要创建相应文件夹
2.创建通用的脚本用于删除过旧的备份和备份控制文件以及备份归档日志
global_del_obso --删除过旧的备份
global_bkctl --备份控制文件
global_arch --备份归档日志
3.创建0,1级差异增量备份
这三个脚本中均包含第2步的3个脚本,先调用global_del,然后做增量备份,最后备份归档日志global_arch和控制文件global_bkctl
4.创建inc0.rcv,inc1.rcv
文件内容包含调用 @@/<dir>/connect.rcv以及run{execute globalscript scriptname;}exit;
5.编辑第4步的三个文件分别为inc0.sh,inc1.sh
nohup $ORACLE_HOME/bin/rman cmdfile=/u03/bk/scripts/inc1.rcv log=/u03/bk/scripts/inc0.logappend &
6.使用crontab制定备份计划
二、具体实现
演示环境:
系统: RHEL release 5.4 (Final) +Oracle 11.2.3
目标数据库: qbjmes
恢复目录数据库:
备份目录: /u03/bk
连接脚本所在路径:
注:监听需要配置好且已启动
1.连接脚本
connect.rcv文件内容
connect nocatalog rman target /
2.建立通用脚本
--删除不必要的备份
run
{
allocatechannel ch1 device type disk;
crosscheck archivelog all;
delete noprompt expired archivelog all;
delete noprompt obsolete recovery window of 7days;
releasechannel ch1;
}
--备份控制文件脚本
run
{
allocate channel ch1 device type disk;
backup as compressed backupset
current controlfile reuse
format='/u03/bk/backupctl.ctl'
tag='bkctl';
release channel ch1;
}
--备份归档日志脚本
run
{
allocate channel ch1 device type disk;
allocate channelch2 device type disk;
sql"alter system archive log current"; --归档当前的联机日志
set limit channelch1 readrate=10240; --(读取速率10M)
set limit channel ch1 kbytes=4096000; --(备份片的大小)
backup ascompressed backupset
format='/u03/bk/arch_%d_%U'
tag='bkarch'
archivelog all deleteinput;
release channel ch1;
release channel ch2;
}
3.创建0,1级差异增量备份脚本(注:每个脚本备份前会执行删除过旧的备份,脚本尾部会调用另外两个脚本来备份归档日志及控制文件)
--创建0级增量备份
run
{
allocate channelch1 device type disk;
allocate channelch2 device type disk;
set limit channel ch1 readrate=10240;
set limit channelch1 kbytes=4096000;
set limit channelch2 readrate=10240;
set limit channelch2 kbytes=4096000;
backup ascompressed backupset
incremental level 0 database
format='/u03/bk/inc0_%d_%U'
tag='inc0';
release channel ch1;
release channel ch2;
}
--创建1级增量备份
run
{
allocate channel ch1 device type disk;
allocate channel ch2 device type disk;
set limit channel ch1 readrate=10240;
set limit channel ch1 kbytes=4096000;
set limit channel ch2 readrate=10240;
set limit channel ch2 kbytes=4096000;
backup as compressed backupset
incrementallevel 1 database
format='/u03/bk/inc1_%d_%U'
tag='inc1';
releasechannel ch1;
releasechannel ch2;
}
--查看备份完成情况
listbackupset summary;
4.建立shell脚本,让linux自动执行脚本
a. vi inc0.rcv,inc1.rcv --注意不同的文件执行不同的备份脚本
@@/u03/bk/scripts/connect.rcv --(rman下的脚本去调用其他脚本用@@符号)(调用脚本不需要分号)
run{
execute globalscript gloal_inc0;
}
exit;
b. 编辑shell文件
vi inc0.sh
nohup $ORACLE_HOME/bin/rman cmdfile=/u03/bk/scripts/inc0.rcvlog=/u03/bk/scripts/inc0.logappend &
vi inc1.sh
nohup $ORACLE_HOME/bin/rman cmdfile=/u03/bk/scripts/inc1.rcvlog=/u03/bk/scripts/inc0.logappend &
--注意:nohup与&表示将脚本放入后台执行
c.使用crontab建立一个备份计划
crontab -e
#min hour date mon day(星期) command
10 3 * * 0 /u03/bk/scripts/inc0.sh
10 3 * * 1 /u03/bk/scripts/inc1.sh
10 3 * * 2 /u03/bk/scripts/inc1.sh
10 3 * * 3 /u03/bk/scripts/inc1.sh
10 3 * * 4 /u03/bk/scripts/inc1.sh
10 3 * * 5 /u03/bk/scripts/inc1.sh
10 3 * * 6 /u03/bk/scripts/inc1.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级增量备份,相当于全备
周一,周二,周三,周四,周五,周六执行1级增量备份
f.编辑好的shell 脚本测试
chmod 755*.sh --给shell脚本加权限
测试脚本 例如./inc0.sh
三、总结
1.backup controlfile in each scripts tail (在脚本的尾部备份控制文件)
2.Delete obsolete backupset in each scripts threshold(删除旧的备份)
3.Switch logfile before backup database; (在数据库备份以前切换日志,备份联机重做日志)
4.Chmod u+x*.sh
后续:
1、配置RMAN
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FORDEVICE TYPE DISK TO '/u02/backup/%F';
CONFIGURE CHANNEL 1 DEVICE TYPE DISKFORMAT '/u02/rman/dt1/dt1_%U_%T';
CONFIGURE CHANNEL 2 DEVICE TYPE DISKFORMAT '/u02/rman/dt2/dt2_%U_%T';
CONFIGURE COMPRESSION ALGORITHM 'LOW'
2、计划任务:
[oracle@qbjmes ~]$ crontab -l
10 3 * * 0 /u03/bk/scripts/lv0.sh
10 3 * * 1 /u03/bk/scripts/lv1.sh
10 3 * * 2 /u03/bk/scripts/lv1.sh
10 3 * * 3 /u03/bk/scripts/lv1.sh
10 3 * * 4 /u03/bk/scripts/lv1.sh
10 3 * * 5 /u03/bk/scripts/lv1.sh
10 3 * * 6 /u03/bk/scripts/lv1.sh
[oracle@qbjmes ~]$
3、新建目录
mkdir /u03/bk/scripts/
chown -R /u03
4、shell脚本
lv0.sh
#!/bin/bash
source /home/oracle/.bash_profile
rman target / log=/u02/backup/db_lv0_`date+%Y%m%d`.log <<EOF
configure retention policy to recoverywindow of 7 days;
run{
allocate channel disk1 type disk;
allocate channel disk2 type disk;
backup incremental level 0 database includecurrent controlfile format '/u02/backup/db_level0_data_%d_%T_%s'
plus archivelog format'/u02/backup/db_level0_arch_%d_%T_%s'delete all input;
delete noprompt obsolete;
crosscheck backup;
delete noprompt expired backup;
release channel disk1;
release channel disk2;
}
exit;
EOF
lv1:
#!/bin/bash
source /home/oracle/.bash_profile
rman target / log=/u02/backup/db_lv1_`date+%Y%m%d`.log <<EOF
configure retention policy to recoverywindow of 7 days;
run{
allocate channel disk1 type disk;
allocate channel disk2 type disk;
backup incremental level 1 database includecurrent controlfile format '/u02/backup/db_level1_data_%d_%T_%s'
plus archivelog format'/u02/backup/db_level1_arch_%d_%T_%s'delete all input;
delete noprompt obsolete;
crosscheck backup;
delete noprompt expired backup;
release channel disk1;
release channel disk2;
}
exit;
EOF
6、新建一个拷贝scpuser
useradd -m -u 2201 -g oinstall -Gdba,oper,asmdba -d /home/scpuser -s /bin/bash scpuser
7、交换钥匙
ssh-keygen ssh-copy-id
8、拷贝
crontab -e 自动任务
10 4 * * 0 scp /u02/backup/*maohz@172.16.16.11:/data/meslog/0
10 4 * * 1 scp /u02/backup/*maohz@172.16.16.11:/data/meslog/1
10 4 * * 2 scp /u02/backup/*maohz@172.16.16.11:/data/meslog/2
10 4 * * 3 scp /u02/backup/*maohz@172.16.16.11:/data/meslog/3
10 4 * * 4 scp /u02/backup/*maohz@172.16.16.11:/data/meslog/4
10 4 * * 5 scp /u02/backup/*maohz@172.16.16.11:/data/meslog/5
10 4 * * 6 scp /u02/backup/* maohz@172.16.16.11:/data/meslog/6
7、新建一个删除rmuser
useradd -m -u 3301 -g oinstall -Gdba,oper,asmdba -d /home/scpuser -s /bin/bash rmuser
8、交换钥匙
ssh-keygen ssh-copy-id
9、授权
chmod -R 777 /u02/backup
10、删除
crontab -e 自动任务
[rmuser@qbjmes ~]$ crontab -l
10 5 * * 0 rm /u02/backup/*
10 5 * * 1 rm /u02/backup/*
10 5 * * 2 rm /u02/backup/*
10 5 * * 3 rm /u02/backup/*
10 5 * * 4 rm /u02/backup/*
10 5 * * 5 rm /u02/backup/*
10 5 * * 6 rm /u02/backup/*
5、重启cron自动任务
/etc/init.d/cronttabrestart
通过收集网络资源整理获得,结合公司实际的情况改成这样,比较基础,还请赐教。