1、创建备份目录:
- [root@Centos ~]# mkdir -p /home/oracle/backup
2、设置目录权限:
- [root@Centos ~]# chown -R oracle:oinstall /home/oracle/backup
3、登录 Oracle:
- [root@Centos ~]# su - oracle # 切换到 Oracle 用户下
- 上一次登录:三 12月 :: CST 2019pts/ 上
- [oracle@Centos ~]$ sqlplus / as sysdba
- SQL*Plus: Release 11.2.0.1. Production on 星期四 12月 ::
- Copyright (c) , , Oracle. All rights reserved.
- 连接到:
- Oracle Database 11g Enterprise Edition Release 11.2.0.1. - 64bit Production
- With the Partitioning, OLAP, Data Mining and Real Application Testing options
- SQL>
4、指定 expdp 输出目录:
- create directory expdata as '/home/oracle/backup';
5、授予 system 权限:
- grant create any directory to system;
6、退出 sqlplus:
- exit;
或者按 CTRL + d。
7、编写备份脚本:
- [oracle@Centos ~]$ vim /home/oracle/oracle_back.sh
脚本内容如下:
- #!/bin/bash
- # oracle 全库备份脚本,只保留最近7天的备份
- # export ORACLE_BASE=/u01/app/oracle
- # export ORACLE_SID=centos
- # export ORACLE_HOME=/u01/app/oracle/product/11.2./db_1
- # export PATH=$PATH:$ORACLE_HOME/bin
- # 以上环境变量如果在 .bash_profile 已配置过,则无需添加
- DATA_DIR="/home/oracle/backup"
- BAKUP_TIME=`date +%Y-%m-%d`
- DAYS=7
- echo "Starting backup..."
- echo "Bakup file path $DATA_DIR/$BAKUP_TIME.dmp"
- expdp system/'oracle' directory=expdata dumpfile=$BAKUP_TIME.dmp full=y logfile=$BAKUP_TIME.log
- echo "Successfully."
- # 删除 7 天之前的备份脚本
- find $DATA_DIR -type f -mtime +$DAYS -exec rm -f {} \;
#!/bin/bash
#export ORACLE_BASE=/home/oracledb/app/oracle
#export ORACLE_HOME=$ORACLE_BASE/product/19.5.0
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
export ORACLE_SID=jzcs
#PATH=/usr/bin:/usr/sbin:/sbin:/bin:$ORACLE_HOME/bin:$PATH;
#export PATH
/usr/bin/find /home/oracledb/backup_jzcs -name "*" -type f -mtime +7 -exec ls -ltr {} \;
/usr/bin/find /home/oracledb/backup_jzcs -name "*" -type f -mtime +7 -exec rm -f {} \;
rq=$(date '+%Y%m%d')
echo $rq >> /home/oracledb/jzcs.log
/home/oracledb/app/oracle/product/19.5.0/bin/expdp dboshop001/jzcs202308 directory=dump_jzcs dumpfile=jzcs-$rq.dump full=y logfile=jzcs-$rq.log >> /home/oracledb/jzcs.log
8、设置自动执行 使用到 crontab 命令
- [oracle@Centos ~]$ crontab -e
- 0 1 * * * /home/oracle/oracle_back.sh # 每天凌晨一点执行
最后保存退出。可通过 crontab -l 查看任务是否设置成功。
(1)查看当前的定时任务: crontab -l

(2)利用crontab定时执行任务:crontab -e ,进入定时任务编辑界面
0 22 * * * /opt/oracle_dmp//oracle_bak.sh #每天晚上10点执行
0 22 * * 6 /opt/oracle_dmp//oracle_bak.sh #每周六晚上10点执行
crontab 操作命令介绍
查看crontab:crontab -l 编辑crontab:crontab -e 删除crontab:crontab -r
启动:systemctl start crond.service
停止:systemctl stop crond.service
重启:systemctl restart crond.service
设置开机自启
把crontab设置成开机自启
# vim /etc/rc.d/rc.local
加入以下内容:
systemctl start crond.service

参考链接:https://blog.csdn.net/Byppfeng/article/details/90376780
浙公网安备 33010602011771号