在rmanscripts下面建 rmanlevel0.sh rmanlevel1.sh rmanlevel2.sh 文件:
mkdir oradataback
--home/oracle/app/oradataback
chmod 775 oradataback
chown -R oracle:oinstall /home/oracle/app/oradataback
touch rmanlevel0.sh rmanlevel1.sh rmanlevel2.sh
赋权同上
2. vi 编辑3个文件:
0级备份文件:
--rmanlevel0.sh--
#!/bin/sh
source ~/.bash_profile
# set porfile_env
export ORACLE_SID=eytdbs # rman level0 backup
rman log='/home/oracle/app/oradataback/db_rman0.log' append <
connect target /;
run
{
allocate channel c1 type disk;
#allocate channel c2 type disk;
backup as compressed backupset incremental level=0 database format '/home/oracle/app/oradataback/db_level0_%T_%U' tag='level0';
sql 'alter system archive log current';
backup archivelog all format '/home/oracle/app/oradataback/archback/db_arch_%T_%U' delete input;
release channel c1;
#release channel c2;
}
EOF
1级备份文件:
--rmanlevel1.sh--
#!/bin/sh
source ~/.bash_profile
# set porfile_env
export ORACLE_SID=eytdbs # rman level1 backup
rman log='/home/oracle/app/oradataback/db_rman1.log' append <connect target /;
run
{
allocate channel c1 type disk;
#allocate channel c2 type disk;
backup incremental level= 1 database format '/home/oracle/app/oradataback/db_level1_%T_%U' tag='level1';
sql 'alter system archive log current';
backup archivelog all format '/home/oracle/app/oradataback/archback/db_arch_%T_%U' delete input;
release channel c1;
#release channel c2;
delete noprompt obsolete; # 删除过期的备份
}
EOF
2级备份文件:
--rmanlevel2.sh--
#!/bin/sh
source ~/.bash_profile
# set porfile_env
export ORACLE_SID=eytdbs # rman level0 backup
rman log='/home/oracle/app/oradataback/db_rman2.log' append <connect target /;
run
{
allocate channel c1 type disk;
#allocate channel c2 type disk;
backup incremental level=2 database format '/home/oracle/app/oradataback/db_level2_%T_%U' tag='level2';
sql 'alter system archive log current';
backup archivelog all format '/home/oracle/app/oradataback/archback/db_arch_%T_%U' delete input;
release channel c1;
#release channel c2;
}
EOF
#说明:在oracle用户下手动运行脚本的命令:sh rmanlevel2.sh
3. 加自动运行任务:
在root下设置,用vi编辑: #crontab -e
0 2 1 * * su – oracle -c /home/oracle/app/oradataback/rmanscripts/rmanlevel0.sh //每月1日2:00运行 0级备份
0 2 * * 0 su – oracle -c /home/oracle/app/oradataback/rmanscripts/rmanlevel1.sh //每周日2:00运行 1级备份
0 2 * * 1-6 su – oracle -c /home/oracle/app/oradataback/rmanscripts/rmanlevel2.sh //每周一周六2:00运行 2级增量备份
--查看添加的任务: crontab -l
--crontab的格式:
minutes|hours|day of month|month|day of week|
--启动crond:
/sbin/service crond start //启动服务
/sbin/service crond stop //关闭服务
/sbin/service crond restart //重启服务
/sbin/service crond reload //重新载入配置
4. 测试结果:
可以自动运行,能成功的备份数据。
--附加一个ftp.sh文件:
#!/bin/sh
cd /home/oracle/app/oradata #本地目录
tar -czf oradata`date +%Y%m%d`_217.tar.gz /home/oracle/app/oradata/
cd /home/oracle/app/fast_recovery_area/eytdbs #本地目录
tar -czf oralog`date +%Y%m%d`_217.tar.gz /home/oracle/app/fast_recovery_area/eytdbs/
#登陆远端机器
sftp oracle/@10.1.4.200 << EOF
cd /home/oracle #远端目录
put /home/oracle/app/oradata/oradata`date +%Y%m%d`_217.tar.gz
put /home/oracle/app/fast_recovery_area/eytdbs/oralog`date +%Y%m%d`_217.tar.gz
quit
EOF
#!/bin/sh ------全備
source ~/.bash_profile
# set porfile_env
export ORACLE_SID=rmandb # rman full backup
rman log='/u01/bklog/db_rman0.log' append <
connect target /;
run {
crosscheck archivelog all;
crosscheck backup;
delete noprompt obsolete;
delete noprompt expired backup;
allocate channel d1 type disk MAXPIECESIZE 1024M rate 10M;
allocate channel d2 type disk MAXPIECESIZE 1024M rate 10M;
backup format '/u01/bkdata/%T_%d_%s_%p_%u.dat' tag 'full_data' database ;
backup format '/u01/bkarch/%T_%d_%s_%p_%u.arc' tag 'full_arc' archivelog all ;
backup format '/u01/bkdata/%T_%d_%s_%p_%u.ctl' tag 'full_ctl' current controlfile;
release channel d1;
release channel d2;
}
EOF
#!/bin/sh ---累計
source ~/.bash_profile
# set porfile_env
export ORACLE_SID=rmandb # rman level1 backup
rman log='/u01/bklog/db_rman1.log' append <connect target /;
run
{
allocate channel c1 type disk;
allocate channel c2 type disk;
backup incremental level= 1 cumulative database format '/u01/bkdata/%T_%d_%s_%p_%u.dat' tag='in1';
sql 'alter system archive log current';
backup archivelog all format '/u01/bkarch/%T_%d_%s_%p_%u.arc' delete input;
release channel c1;
release channel c2;
delete noprompt obsolete;
}
EOF
#!/bin/sh --差異
source ~/.bash_profile
# set porfile_env
export ORACLE_SID=rmandb # rman level0 backup
rman log='/u01/bklog/db_rman2.log' append <connect target /;
run
{
allocate channel c1 type disk;
allocate channel c2 type disk;
backup incremental level= 1 database format '/u01/bkdata/%T_%d_%s_%p_%u.dat' tag='incre1';
sql 'alter system archive log current';
backup archivelog all format '/u01/bkarch/%T_%d_%s_%p_%u.arc' delete input;
release channel c1;
release channel c2;
}
EOF
startrman.sh 說明每週三日全備其他執行歸檔備份
先調用執行oracle用戶的環境變量,已獲得執行環境
#!/bin/sh
. /home/oracle/.bash_profile
SH_HOME=/home/oracle/bakSpt
export SH_HOME
DATE=`date +"%Y%m%d"`
WEEK=`date +"%w"`
#------------------------------
#Confirm whether the oracle is runing
#-----------------------------
ps -ef| grep dbw0_$ORACLE_SID | grep -v grep >> /dev/null
#-----------------------------
#Begin to backup
#-----------------------------
if if [$? -eq 0]:then
if[ $week="0" ] || [ $week="3" ];then
rman target/ @/u01/backup/fullbackup.sql log=/home/backup/fullbackup`date +%Y%m%d`.log
else
rman target/ @/u01/backup/archivelogbackup.sql log=/home/backup/archivelogbackup`date +%Y%m%d`.log
fi
fi
给脚本授予权限
[oracle@mzl backup]$ chmod +x archivelogbackup.sql
给脚本授予权限
[oracle@mzl backup]$ chmod +x startrman.sh
9 编写定时器crontab,定制每天1:30调用/u01/backup/startrman.sh脚本
[oracle@mzl ~]$ crontab -e
内容:
30 1 * * 0-6 /u01/backup/startrman.sh
查看crontab:
[oracle@mzl ~]$ crontab -l
30 1 * * 0-6 /u01/backup/startrman.sh
最后:测试日期可以用
[oracle@mzl ~]$ echo date=`date +%Y%m%d`
date=20080610
测试星期:
[oracle@mzl ~]$ echo week=`date +%w`
week=2
我的腳本
run {
crosscheck archivelog all;
crosscheck backup;
delete noprompt obsolete;
delete noprompt expired backup;
allocate channel d1 type disk MAXPIECESIZE 1024M rate 10M;
allocate channel d2 type disk MAXPIECESIZE 1024M rate 10M;
backup format '/u01/bkdata/%T_%d_%s_%p_%u.dat' tag 'full_data' database ;
backup format '/u01/bkarch/%T_%d_%s_%p_%u.arc' tag 'full_arc' archivelog all ;
plus archivelog format '/u01/bkarch/%T_%d_%s_%p_%u.arc' tag 'full_arc' delete all input; (yongzhege)
backup format '/u01/bkdata/%T_%d_%s_%p_%u.ctl' tag 'full_ctl' current controlfile;
release channel d1;
release channel d2;
}
歸檔備份日誌腳本
run
{
backup archivelog all format '/u01/bkarch/%T_%d_%s_%p_%u.arc' tag 'full_arc' delete all input;
}
------累計
run {
allocate channel dev1 device type disk;
backup
incremental level 1
cumulative database format 'F:\backup\%d_%s_%p_%c_%t.dat'
tag='in1';
release channel dev1;
}
run
{
allocate channel c1 type disk;
allocate channel c2 type disk;
backup incremental level= 1 cumulative database format '/u01/bkdata/%T_%d_%s_%p_%u.dat' tag='in1';
sql 'alter system archive log current';
backup archivelog all format '/u01/bkarch/%T_%d_%s_%p_%u.arc' delete input;
release channel c1;
release channel c2;
delete noprompt obsolete; # 刪除過去備份
}
-----差異
run {
allocate channel dev1 device type disk;
backup
incremental level 1
database format 'F:\backup\%d_%s_%p_%c_%t'
tag='incre1';
release channel dev1;
}
run
{
allocate channel c1 type disk;
allocate channel c2 type disk;
backup incremental level= 1 database format '/u01/bkdata/%T_%d_%s_%p_%u.dat' tag='incre1';
sql 'alter system archive log current';
backup archivelog all format '/u01/bkarch/%T_%d_%s_%p_%u.arc' delete input;
release channel c1;
release channel c2;
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10201716/viewspace-723037/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/10201716/viewspace-723037/