对于 Oracle 数据库的备份与恢复,使用最多的莫过于使用RMAN进行备份与恢复。而制定RMAN备份策略则是基于数据库丢失的容忍程度,即恢复策略来制定。
在下面的备份策略中,给出的是一个通用的备份策略。在该备份策略中,使用了catalog方式来保持备份脚本以及备份信息。在实际环境中应根据具体的情况对此进行相应的调整。
(一) 步骤
1 archive log list
检查归档日志
1 Shutdown immediate 2 Startup mount 3 Alter database archievelog 4 Alter database open
打开归档
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=/oradata/ts_backup/bk/scripts/inc1.rcv log=/u03/bk/scripts/inc0.log append &
6.使用crontab制定备份计划
(二) 具体实现
系统: LINUX release 5.4 + Oracle 11g
目标数据库: orcl ecology1
恢复数据库: catalog_orcl rman
备份目录: /oradata/ts_backup/bk
连接脚本所在路径:/oradata/ts_backup/bk/scripts
注:监听需要配置好且已启动
1. 创建catalog数据库
使用dbca创建数据库catalog_orcl.创建完成后在该数据库创建用户rman。
1 创建rman表空间: 2 create tablespace MYECOLOGY1 datafile ‘D:\app\Administrator\oradata\orcl\ts_backup.dbf‘ size 100M AUTOEXTEND ON NEXT 10M maxsize unlimited segment space management auto; 3 创建用户:create user rman identified by rman default tablespace MYECOLOGY1 4 授权:grant connect,resource,recovery_catalog_owner to rman; 5 6 rman catalog rman/rman@catalog_orcl
创建恢复目录:
1 Create catalog tablespace MYECOLOGY1
注册恢复目录:
Register database
2. 创建脚本
1.连接脚本
1 connect.rcv文件内容 2 connect catalog rman/rman; 3 connect target ecology1/oracle; 4 catalog的通用脚本 5 rman cmdfile=/oradata/ts_backup/bk/scripts/confnect.rcv --(在rman中使用外部脚本) 6 rman catalog rman/rman target ecology1/oracle --使用该脚本连接后创建通用脚本
2.建立通用脚本
--删除不必要的备份
1 RMAN> create global script global_del_obso comment ‘A script for obsolete backup and delete it‘ { 2 2> crosscheck archivelog all; 3 3> delete noprompt expired archivelog all; 4 4> allocate channel ch1 device type disk; 5 5> delete noprompt obsolete recovery window of 7 days; 6 6> release channel ch1; 7 7> }
--备份控制文件脚本
1 RMAN> create global script global_bkctl comment ‘A script for backup control file‘{ 2 2> allocate channel ch1 device type disk; 3 3> backup as compressed backupset 4 4> current controlfile reuse 5 5> format=‘/oradata/ts_backup/bk/backupctl.ctl‘ 6 6> tag=‘bkctl‘; 7 7> release channel ch1; 8 8> }
--备份归档日志脚本
1 RMAN> create global script global_arch comment "backup archivelog and then delete it" { 2 2> allocate channel ch1 device type disk; 3 3> allocate channel ch2 device type disk; 4 4> sql "alter system archive log current"; --归档当前的联机日志 5 5> set limit channel ch1 readrate=10240; --(读取速率10M) 6 6> set limit channel ch1 kbytes=4096000; --(备份片的大小) 7 7> backup as compressed backupset 8 8> format=‘/oradata/ts_backup/bk/arch_%d_%U‘ 9 9> tag=‘bkarch‘ 10 10> archivelog all delete input; 11 11> release channel ch1; 12 12>0020release channel ch2; 13 13> }
--使用list查看所有的已建立的全局脚本
1 list global script names; --(列出所有的脚本) 2 delete globals script script_name; --(删除脚本) 3 RMAN> list global script names; 4 List of Stored Scripts in Recovery Catalog 5 Global Scripts 6 Script Name 7 Description 8 ----------------------------------------------------------------------- 9 global_arch 10 backup archivelog and then delete it 11 global_bkctl 12 A script for backup control file 13 global_del_obso 14 A script for obsolete backup and delete it
3.创建0,1,2级增量备份脚本(注:每个脚本备份前会执行删除过旧的备份,脚本尾部会调用另外两个脚本来备份归档日志及控制文件)
--创建0级增量备份
1 RMAN> create global script global_inc0 comment "backup database as incrmental level 0"{ 2 2> execute global script global_del_obso; 3 3> allocate channel ch1 device type disk; 4 4> allocate channel ch2 device type disk; 5 5> set limit channel ch1 readrate=10240; 6 6> set limit channel ch1 kbytes=4096000; 7 7> set limit channel ch2 readrate=10240; 8 8> set limit channel ch2 kbytes=4096000; 9 9> backup as compressed backupset 10 10> incremental level 0 database 11 11> format=‘ /u01/bk/inc0_%d_%U‘ 12 12> tag=‘inc0‘; 13 13> release channel ch1; 14 14> release channel ch2; 15 15> execute global script global_arch; 16 16> execute global script global_bkctl; 17 17> }
--创建1级增量备份
1 RMAN> create global script global_inc1 comment "backup database as incrmental level 1"{ 2 2> execute global script global_del_obso; 3 3> allocate channel ch1 device type disk; 4 4> allocate channel ch2 device type disk; 5 5> set limit channel ch1 readrate=10240; 6 6> set limit channel ch1 kbytes=4096000; 7 7> set limit channel ch2 readrate=10240; 8 8> set limit channel ch2 kbytes=4096000; 9 9> backup as compressed backupset 10 10> incremental level 1 database 11 11> format=‘/oradata/ts_backup/bk/inc1_%d_%U‘ 12 12> tag=‘inc1‘; 13 13> release channel ch1; 14 14> release channel ch2; 15 15> execute global script global_arch; 16 16> execute global script global_bkctl; 17 17> }
--创建2级增量备份
1 RMAN> create global script global_inc2 comment "backup database as incrmental level 2"{ 2 2> execute global script global_del_obso; 3 3> allocate channel ch1 device type disk; 4 4> allocate channel ch2 device type disk; 5 5> set limit channel ch1 readrate=10240; 6 6> set limit channel ch1 kbytes=4096000; 7 7> set limit channel ch2 readrate=10240; 8 8> set limit channel ch2 kbytes=4096000; 9 9> backup as compressed backupset 10 10> incremental level 2 database 11 11> format=‘/oradata/ts_backup/bk/inc2_%d_%U‘ 12 12> tag=‘inc2‘; 13 13> release channel ch1; 14 14> release channel ch2; 15 15> execute global script global_arch; 16 16> execute global script global_bkctl; 17 17> }
--在rman中检验在rman中写的脚本global_inc0、global_inc1、global_inc2,因为RMAN不会自动检查,下面的语句用来执行脚本(检验)
1 RMAN> run{ 2 2> execute global script global_inc0; 3 3> execute global script global_inc1; 4 4> execute global script global_inc2; 5 5> }
--查看备份完成情况
1 list backupset summary; 2 4.建立shell脚本,让linux自动执行脚本 3 a. vi inc0.rcv,inc1.rcv ,inc2.rcv --注意不同的文件执行不同的备份脚本 4 @@/oradata/ts_backup/bk/scripts/connect.rcv --(rman下的脚本去调用其他脚本用@@符号)(调用脚本不需要分号) 5 run{ 6 execute global script global_inc0; 7 } 8 exit; 9 b. 编辑shell文件 10 vi inc0.sh 11 nohup $ORACLE_HOME/bin/rman cmdfile=/oradata/ts_backup/bk/scripts/inc0.rcv log=/oradata/ts_backup/bk/scripts/inc0.log append & 12 vi inc1.sh 13 nohup $ORACLE_HOME/bin/rman cmdfile=/u03/bk/scripts/inc1.rcv log=/u03/bk/scripts/inc0.log append & 14 vi inc2.sh 15 nohup $ORACLE_HOME/bin/rman cmdfile=/u03/bk/scripts/inc2.rcv log=/u03/bk/scripts/inc0.log append &
--注意:nohup与&表示将脚本放入后台执行
c.使用crontab建立一个备份计划
1 crontab -e 2 #min hour date mon day(星期) command 3 30 1 * * 0 /u03/bk/scripts/inc0.sh 4 30 1 * * 1 /u03/bk/scripts/inc2.sh 5 30 1 * * 2 /u03/bk/scripts/inc2.sh 6 30 1 * * 3 /u03/bk/scripts/inc2.sh 7 30 1 * * 4 /u03/bk/scripts/inc1.sh 8 30 1 * * 5 /u03/bk/scripts/inc2.sh 9 30 1 * * 6 /u03/bk/scripts/inc2.sh
i 编辑
Esc : wq 保存
Esc : q! 退出 不保存
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
(三) 总结
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
/oadata/ts_backup/bk 备份文件
/oadata/ts_backup/bk/scripts 脚本