1.新建备份目录

mkdir -p /home/db/oracle/bakfile

cd /home/db/oracle/bakfile

2.新建shell脚本存放路径

mkdir -p /home/db/oracle/db_back

chown -R oracle:oinstall /home/db/oracle/bakfile

chmod -R 775 /home/db/oracle/bakfile/P_DB_BAK.sh

3.编辑定时任务

编辑定时任务脚本
vim /home/db/oracle/bakfile/P_DB_BAK.sh

备份原来的定时器
cp /etc/crontab /etc/crontab_bak_2022-09-01

编辑定时任务
crontab -e

重启定时任务
systemctl restart crond.service

关闭定时任务
systemctl stop crond.service

每天晚上11点 备份oracle数据库
0 23 * * * /home/db/oracle/bakfile/P_DB_BAK.sh

查看日志
tail -f /var/spool/mail/root

清除定时邮件日志
cat /dev/null > /var/spool/mail/root

4.脚本内容-删除30天前备份数据

#oracle info

 echo "备份脚本开始启动..."

 export ORACLE_SID=orcl
 export ORACLE_BASE=/home/db/oracle
 export ORACLE_HOME=$ORACLE_BASE/product/11.2.0
 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib:/lib:/usr/lib:/usr/local/lib
 export PATH=$ORACLE_HOME/bin:/bin:/usr/bin:/usr/sbin:/usr/local/bin:/oracle/grid/bin

 export LANG=zh_CN.UTF-8
 export NLS_LANG=AMERICAN_AMERICA.AL32UTF8

 dateTime=`date +%Y_%m_%d`
 days=30
 orsid='192.168.78.119:1521/orcl'
 orowner=QHDTE_SOAR
 bakuser=QHDTE_SOAR
 bakpass=QHDTE_SOAR#2014
 bakdir=/home/db/oracle/bakfile
 bakdata=$orowner"_"$dateTime.dmp
 baklog=$orowner"_"$dateTime.log
 ordatabak=$orowner"_"$dateTime.tar.gz
 cd $bakdir

 echo "现在开始进行数据备份..."

 exp $bakuser/$bakpass@$orsid grants=y owner=$orowner file=$bakdir/$bakdata log=$bakdir/$baklog

 echo "数据备份结束..."

 echo "现在开始进行压缩备份数据..."

 tar -zcvf $ordatabak $bakdata $baklog

 echo "压缩备份数据结束..."

 echo "删除备份数据开始..."

 find $bakdir/ -type f -name "*.log" -exec rm {} \;
 find $bakdir/ -type f -name $bakdata -exec rm {} \;
 find $bakdir/ -type f -name "*.tar.gz" -mtime +$days -exec rm -rf {} \;

 echo "删除备份数据结束..."

 echo "备份执行完毕..."
原文链接: https://marshucheng1.github.io/2022/06/23/oracle/