1、创建备份目录:

  1. [root@Centos ~]# mkdir -p /home/oracle/backup

2、设置目录权限:

  1. [root@Centos ~]# chown -R oracle:oinstall /home/oracle/backup

3、登录 Oracle:

  1. [root@Centos ~]# su - oracle # 切换到 Oracle 用户下
  2. 上一次登录:三 12 :: CST 2019pts/
  3. [oracle@Centos ~]$ sqlplus / as sysdba
  4.  
  5. SQL*Plus: Release 11.2.0.1. Production on 星期四 12 ::
  6.  
  7. Copyright (c) , , Oracle. All rights reserved.
  8.  
  9. 连接到:
  10. Oracle Database 11g Enterprise Edition Release 11.2.0.1. - 64bit Production
  11. With the Partitioning, OLAP, Data Mining and Real Application Testing options
  12.  
  13. SQL>

4、指定 expdp 输出目录:

  1. create directory expdata as '/home/oracle/backup';

5、授予 system 权限:

  1. grant create any directory to system;

6、退出 sqlplus:

  1. exit;

或者按 CTRL + d。

7、编写备份脚本:

  1. [oracle@Centos ~]$ vim /home/oracle/oracle_back.sh

脚本内容如下:

  1. #!/bin/bash
  2. # oracle 全库备份脚本,只保留最近7天的备份
  3.  
  4. # export ORACLE_BASE=/u01/app/oracle
  5. # export ORACLE_SID=centos
  6. # export ORACLE_HOME=/u01/app/oracle/product/11.2./db_1
  7. # export PATH=$PATH:$ORACLE_HOME/bin
  8. # 以上环境变量如果在 .bash_profile 已配置过,则无需添加
  9.  
  10. DATA_DIR="/home/oracle/backup"
  11. BAKUP_TIME=`date +%Y-%m-%d`
  12. DAYS=7
  13. echo "Starting backup..."
  14. echo "Bakup file path $DATA_DIR/$BAKUP_TIME.dmp"
  15. expdp system/'oracle' directory=expdata dumpfile=$BAKUP_TIME.dmp full=y logfile=$BAKUP_TIME.log
  16. echo "Successfully."
  17. # 删除 7 天之前的备份脚本
  18. 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 命令

  1. [oracle@Centos ~]$ crontab -e
  2.  
  3. 0 1 * * * /home/oracle/oracle_back.sh # 每天凌晨一点执行
  1.  

最后保存退出。可通过 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

posted on 2023-01-18 15:18  Faker006  阅读(231)  评论(0)    收藏  举报