Linux中怎样定时备份Oracle表数据?

2025-10-09 02:57:36

1、    在Linux备份机中配置执行定时任务时的用户环境变量。这也是再执行定时任务时遇到的第一个坑。在没有配置环境变量的前提下,即使脚本能够手动执行并且没有问题。但添加到系统定时任务后还是会执行失败。

定时任务执行时需要载入当前执行用户的环境变量,否则执行时会找不到sqlplus命令。

1、查看已经配置好的环境变量,一般会在文件最底部,图例仅供参考。

      less /ect/profile

Linux中怎样定时备份Oracle表数据?

2、复制环境变量,粘贴至定时任务执行用户的环境变量中并保存。

     vim /home/username/.bash_profile 

3、编写备份脚本

#!/bin/bash

if [ -f ~/.bash_profile ]

then

 . ~/.bash_profile

fi

rq=`date +%Y%m%d%H%M%S`

lastname='.zip'

mkdir /home/username/temp/$rq

sqlplus -S /nolog > /home/username/logs/$rq.log <<EOF

conn user/password@orcl

set echo off

set head off

set headsep off

set newp none

set linesize 500

set pagesize 0

set trimspool ON

set trimout on

set termout off

set feedback off

set timing off

spool /home/username/temp/$rq/TABLENAME.csv

select ID||','||AGENTNO||','||NAME||','||ADDRESS2||','||OCODE||','||ENTITY_ID from TABLENAME;

spool off

exit

EOF

#加密压缩

zip -rP 123456 /home/username/zip/$rq$lastname /home/username/temp/$rq

# 保留最近14天的备份

ls -t /home/username/zip/ | awk 'NR>14{print "rm -rf " $0}' | xargs rm -rf

4、在shell执行用户中添加定时任务,之后用root重启cron服务即可

crontab -e

0 0 * * * /home/username/shellname.sh

cron 重启命令(不同系统可能命令不同):

service crond restart

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢