使用Contab调用Shell脚本执行expdp自动备份Oracle

本文档详细介绍了如何使用expdp工具在Oracle数据库中导出dmp文件,然后通过FTP上传到远程服务器,并在完成后删除本地备份。整个过程包括设置bash_profile、执行expdp命令、使用FTP传输及删除旧备份。最后展示了脚本执行的日志输出,强调了FTP传输必须使用Binary模式,以及定期清理7天前的备份。
摘要由CSDN通过智能技术生成

思路:

  1. expdp导出dmp到本地dump目录中
  2. 使用FTP上传到远端
  3. 删除本地导出的dmp文件

注意事项:

  1. expdp比exp效率高很多,但需要在Oracle本机运行
  2. 最好同时保存导出的log
  3. 备份时dmp和log分开保存
  4. 需要执行bash_profile,以防止在crontab中expdp执行无效
  5. 使用ftp -ivn以便于在shell中输入账号密码
  6. FTP传输必须使用Binary模式传输,(如果ftp服务器系统不一致的情况下)否则dmp文件可能不可用
  7. 删除7天前无效的dmp

代码如下:

if [ -f ~/.bash_profile ];
then
  . ~/.bash_profile
fi
echo "------ start" $(date "+%Y-%m-%d %H:%M:%S") "----------------------"
expdp hr/hr schemas=hr dumpfile=hr_$(date "+%Y-%m-%d").dmp logfile=hr_$(date "+%Y-%m-%d").log
ftp -ivn<<EOF
open 10.255.255.11
user ftpuser ftpuser_password
bin
lcd /u01/app/oracle/admin/orcl/dpdump
cd /DB_Backup/db
put hr_$(date "+%Y-%m-%d").dmp
delete hr_$(date -d "7 day ago" "+%Y-%m-%d").dmp
cd /DB_Backup/log
put hr_$(date "+%Y-%m-%d").log
quit
EOF
rm -f /u01/app/oracle/admin/orcl/dpdump/hr_$(date "+%Y-%m-%d").dmp
rm -f /u01/app/oracle/admin/orcl/dpdump/hr_$(date "+%Y-%m-%d").log

配置任务计划

使用crontab -e编辑任务计划

10 00 * * * sh /home/oracle/auto_backup_db.sh >> /home/oracle/auto_backup_db.log

执行结果:

[oracle@mesdb01 ~]$ ./auto_backup_mesdb.sh

Export: Release 11.2.0.4.0 - Production on Mon Apr 26 15:14:11 2021

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Release 11.2.0.4.0 - 64bit Production
With the Real Application Clusters and Automatic Storage Management options
Starting "MES"."SYS_EXPORT_SCHEMA_02":  MES/******** schemas=MES dumpfile=MES_2021-04-26.dmp logfile=MES_2021-04-26.log
Estimate in progress using BLOCKS method...
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 49.30 GB
Processing object type SCHEMA_EXPORT/USER
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/SYNONYM/SYNONYM
Processing object type SCHEMA_EXPORT/TYPE/TYPE_SPEC
Processing object type SCHEMA_EXPORT/SEQUENCE/SEQUENCE
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/TABLE/COMMENT
Processing object type SCHEMA_EXPORT/FUNCTION/FUNCTION
Processing object type SCHEMA_EXPORT/FUNCTION/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/PROCEDURE/PROCEDURE
Processing object type SCHEMA_EXPORT/FUNCTION/ALTER_FUNCTION
Processing object type SCHEMA_EXPORT/PROCEDURE/ALTER_PROCEDURE
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/VIEW/VIEW
Processing object type SCHEMA_EXPORT/VIEW/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/TABLE/TRIGGER
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Processing object type SCHEMA_EXPORT/JOB
. . exported "MES"."TBLWIPXXXX"                    11.43 GB   15027 rows
. . exported "MES"."TBLWIPXXXXENTATTRIB"           4.019 GB 73487138 rows
. . exported "MES"."TBLWIPRXXXEUPDAXECONTENT"      2.498 GB 17186687 rows
. . exported "MES"."TBAREPWRGEEARHEAXTE"           930.2 MB 2658014 rows
. . exported "MES"."BAREHWIPCERHERAHTSTATE"        1.720 GB 31698192 rows
. . . . . .
. . . . . .
. . . . . .
. . exported "MES"."TBL_KJBRGERHBSERTHTR"              0 KB       0 rows
. . exported "MES"."TBL_NBRSJNRTJNRTTSUMTNRY"          0 KB       0 rows
. . exported "MES"."TBL_NBWNRTNPETNRTTAIL"             0 KB       0 rows
. . exported "MES"."TBL_NBWNTTNNTTRNSTN                0 KB       0 rows
. . exported "MES"."TBL_UNTNTEPNONR_AGNSNBLY"          0 KB       0 rows
Master table "MES"."SYS_EXPORT_SCHEMA_02" successfully loaded/unloaded
******************************************************************************
Dump file set for MES.SYS_EXPORT_SCHEMA_02 is:
  /u01/app/oracle/admin/orcl/dpdump/MES_2021-04-26.dmp
Job "MES"."SYS_EXPORT_SCHEMA_02" successfully completed at Mon Apr 26 15:33:53 2021 elapsed 0 00:19:37

Connected to 10.255.255.11 (10.255.255.11).
220 Serv-U FTP Server v15.1 ready...
331 User name okay, need password.
230 User logged in, proceed.
Local directory now /u01/app/oracle/admin/orcl/dpdump
250 Directory changed to /DB_Backup/db
200 Type set to I.
local: MES_2021-04-26.dmp remote: MES_2021-04-26.dmp
227 Entering Passive Mode (10,255,255,11,215,185)
150 Opening BINARY mode data connection for MES_2021-04-26.dmp.
226 Transfer complete. 42,817,797,281 bytes transferred. 60,793.06 KB/sec.
42817797281 bytes sent in 688 secs (62252.10 Kbytes/sec)
250 DELE command successful.
250 Directory changed to /DB_Backup/log
local: MES_2021-04-26.log remote: MES_2021-04-26.log
227 Entering Passive Mode (10,255,12,53,216,85)
150 Opening BINARY mode data connection for MES_2021-04-26.log.
226 Transfer complete. 83,006 bytes transferred. 81.06 KB/sec.
83006 bytes sent in 0.00336 secs (24718.88 Kbytes/sec)
250 DELE command successful.
221 Goodbye, closing session.
[oracle@mesdb01 ~]$

备注说明:

如果FTP传输时模式选择不对的话,会出现如下几种情况,务必注意:

file size is not a multiple of logical block size

bad dump file specification

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值