思路:
- expdp导出dmp到本地dump目录中
- 使用FTP上传到远端
- 删除本地导出的dmp文件
注意事项:
- expdp比exp效率高很多,但需要在Oracle本机运行
- 最好同时保存导出的log
- 备份时dmp和log分开保存
- 需要执行bash_profile,以防止在crontab中expdp执行无效
- 使用ftp -ivn以便于在shell中输入账号密码
- FTP传输必须使用Binary模式传输,(如果ftp服务器系统不一致的情况下)否则dmp文件可能不可用
- 删除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