Oracle 备份脚本
netnr 2021-09-29
# 配置参数(开始)===

ymd=$(date +%Y%m%d)
echo "日期:$ymd"
echo
keepday=5
echo "保留最近天数:$keepday"
echo
dirsync=/package/autosync/sync
echo "同步目录:$dirsync"
echo
dbname="ABC"
echo "数据库:$dbname"
echo
dbconn=system/oracle@orcl
echo "数据库连接字符串:$dbconn"
echo
db_directory_name="DATA_PUMP_DIR"
echo "数据库管理员目录名:$db_directory_name"
echo
db_directory_path="/u01/app/oracle/admin/EE/dpdump/" # 该目录为 docker 容器路径对应的宿主路径
echo "数据库管理员目录路径:$db_directory_path"
# 参数 db_directory_name、db_directory_path 通过执行SQL脚本获得:select * from dba_directories;

# 配置参数(结束)===

echo
echo "清理同步目录 $keepday 天前的备分文件"
find ${dirsync} -mtime +$keepday -name "*.zip" | xargs -I {} rm -rf {}
find ${dirsync} -mtime +$keepday -name "*.gz" | xargs -I {} rm -rf {}
echo
echo "开始备份数据库:$dbname"
# backcmd="expdp ${dbconn} tables=TABLE1,TABLE2 dumpfile=${dbname}_${ymd}.dmp logfile=${dbname}_${ymd}.log directory=${db_directory_name}"
backcmd="expdp ${dbconn} schemas=${dbname} dumpfile=${dbname}_${ymd}.dmp logfile=${dbname}_${ymd}.log directory=${db_directory_name}"
echo $backcmd
docker exec oracle11 /bin/bash -c "${backcmd}"
echo
echo "完成备份"
echo
echo "开始打包"
zip -r -q -o ${db_directory_path}${dbname}_${ymd}.zip  ${db_directory_path}${dbname}_${ymd}.* -P123456789
echo
echo "移动压缩包"
mv ${db_directory_path}${dbname}_${ymd}.zip ${dirsync}
echo
echo "清理备份文件:${db_directory_path}${dbname}_${ymd}.*"
rm -f ${db_directory_path}${dbname}_${ymd}.*
echo
echo "Done"
echo
登录写评论