Linux系统中Oracle自动备份方案

作为DBA,每次全库备份几乎都要等别人不用数据库的时候才可以进行(我知道还有其他方式,本文暂时只考虑export),通过下面2部分的就可以轻松实现半夜无值守备份啦。

 

第一部分:编写Oracle备份shell脚本(分常规方式和数据泵方式,根据需要选择一种,创建shell脚本文件:autobackup.sh)
/********************************exp常规方式的shell脚本部分*********************************/
#! /bin/sh
#set environment variable
export  LANG="en_US.UTF-8"
export NLS_LANG="Simplified Chinese_china".ZHS16GBK
export ORACLE_BASE=/home/app/oracle
export ORACLE_HOME=/home/app/oracle/product/11.2.3/db_1
export ORACLE_Sid="YAGDB"
export PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin
#set backupfile format
backuptime=`date +"%Y%m%d"`
#remove overdue backup--删除历史备份,此处根据需要保留或去除
deletetime=`date -d "1 week ago" +"%Y%m%d"`
rm -rf /data/AUTOBACKUP/YAGDBBAK$deletetime
#backup start
cd /data/AUTOBACKUP
mkdir /data/AUTOBACKUP/YAGDBBAK$backuptime
touch /data/AUTOBACKUP/YAGDBBAK$backuptime/FMIS9999_EXP.dmp
touch /data/AUTOBACKUP/YAGDBBAK$backuptime/FMIS9999_EXP.log
touch /data/AUTOBACKUP/YAGDBBAK$backuptime/FMISTMP_EXP.dmp
touch /data/AUTOBACKUP/YAGDBBAK$backuptime/FMISTMP_EXP.log
touch /data/AUTOBACKUP/YAGDBBAK$backuptime/ODS_EXP.dmp
touch /data/AUTOBACKUP/YAGDBBAK$backuptime/ODS_EXP.log
chmod 777 /data/AUTOBACKUP/*.*
chmod 777 /data/AUTOBACKUP/YAGDBBAK$backuptime
chmod 777 /data/AUTOBACKUP/YAGDBBAK$backuptime/*.*
exp 'FMIS9999/[email protected]/YAGDB' file=/data/AUTOBACKUP/YAGDBBAK$backuptime/FMIS9999_EXP.dmp log=/data/AUTOBACKUP/YAGDBBAK$backuptime/FMIS9999_EXP.log owner=FMIS9999 statistics=NONE
exp 'FMIS9999/[email protected]/YAGDB' file=/data/AUTOBACKUP/YAGDBBAK$backuptime/FMISTMP_EXP.dmp log=/data/AUTOBACKUP/YAGDBBAK$backuptime/FMISTMP_EXP.log owner=FMISTMP statistics=NONE
exp 'FMIS9999/[email protected]/YAGDB' file=/data/AUTOBACKUP/YAGDBBAK$backuptime/ODS_EXP.dmp log=/data/AUTOBACKUP/YAGDBBAK$backuptime/ODS_EXP.log owner=ODS sTatistics=NONE


/******************************expdp数据泵方式的shell脚本部分******************************/
#! /bin/sh
#set environment variable
export  LANG="en_US.UTF-8"
export NLS_LANG="Simplified Chinese_china".ZHS16GBK
export ORACLE_BASE=/home/app/oracle
export ORACLE_HOME=/home/app/oracle/product/11.2.3/db_1
export ORACLE_Sid="YAGDB"
export PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin
#set backupfile format
backuptime=`date +"%Y%m%d"`
# remove overdue backup--删除历史备份,此处根据需要保留或去除
deletetime=`date -d "1 week ago" +"%Y%m%d"`
rm -rf /data/AUTOBACKUP/YAGDBBAK$deletetime
#backup start
cd /data/AUTOBACKUP
# create pump forder
mkdir /data/AUTOBACKUP/YAGDBBAK$backuptime
# enduing policy with pump forder
chmod 777 /data/AUTOBACKUP/*.*
chmod 777 /data/AUTOBACKUP/YAGDBBAK$backuptime
# expdp command
expdp 'FMIS9999/[email protected]/YAGDB' directory=dump_dir dumpfile=FMIS9999_EXP.dmp logfile=FMIS9999exp.log schemas=FMIS9999 exclude=statistics
expdp 'FMIS9999/[email protected]/YAGDB' directory=dump_dir dumpfile=FMISTMP_EXP.dmp logfile=FMISTMPexp.log schemas=FMISTMP exclude=statistics
expdp 'FMIS9999/[email protected]/YAGDB' directory=dump_dir dumpfile=ODS_EXP.dmp logfile=ODSexp.log schemas=ODS exclude=statistics

#注:数据泵方式需创建directory ,可在自己本地创建

/******************************************************************************************/

第二部分:设置按需定时执行备份脚本
set autorun
# vi /etc/crontab
#在最后一行添加
0 3 * * 6 root /data/AUTOBACKUP/autobackup.sh      #每周六的3:00执行/data/AUTOBACKUP/autobackup.sh

相关推荐