立即登录

oracle 数据库备份脚本 oracle自动备份脚本

11/28 10:19:09

oracle 数据库备份脚本 oracle自动备份脚本

#!/bin/bash

#获取当前日期

date_now=$(date +%Y%m%d);

#数据库用户

db_user="ynsps";

#数据库密码

db_password="ynsps_2021";

#ip地址

ip="10.180.39.83"

#oracle虚拟目录名

oracle_directory="DATA_PUMP_DIR";

#数据库服务器oracle 数据库备份脚本,需要在本地客户端tnsnames中定义好

db_server="orcl";

#文件名

file_name="Aj"

#本地客户端执行程序目录

oracle_bin_dir="/home/oracle/xx/bin";

#本地备份目录,存放dump文件与临时压缩文件

local_backup_dir="/opt/oracle/app/admin/orcl/dpdump";

#远程备份目录辅助卡盟,对应备份服务器存放位置oracle 数据库备份脚本,需要提前挂载

remote_backup_dir="/xx/xx/oracle_backup";

#备份当天的数据库

echo "-- $(date +'%Y-%m-%d %H:%M:%S') 自动备份数据库并压缩开始----------------------------------------------";

echo "-- $(date +'%Y-%m-%d %H:%M:%S') 数据库备份开始--------------------------------------------------------";

echo "su oracle -lc "${oracle_bin_dir}/expdp ${db_user}/${db_password}@${db_server} directory=${oracle_directory} full=y dumpfile=${db_server}_${date_now}.dmp logfile=${db_server}_${date_now}.log"";

su - oracle -lc "${oracle_bin_dir}/expdp ${db_user}/${db_password}@${ip}/${db_server} directory=${oracle_directory} full=y dumpfile=${file_name}_${db_server}_${date_now}.dmp logfile=${file_name}_${db_server}_${date_now}.log compression=all";

echo "-- $(date +'%Y-%m-%d %H:%M:%S') 数据库压缩开始--------------------------------------------------------";

echo "cd ${local_backup_dir} && tar -zcf ${db_server}_${date_now}.tar.gz ${db_server}_${date_now}.dmp ${db_server}_${date_now}.log";

cd ${local_backup_dir} && tar -zcf ${file_name}_${db_server}_${date_now}.tar.gz ${file_name}_${db_server}_${date_now}.dmp ${file_name}_${db_server}_${date_now}.log;

echo "-- $(date +'%Y-%m-%d %H:%M:%S') 数据库压缩结束--------------------------------------------------------";

echo "-- $(date +'%Y-%m-%d %H:%M:%S') 传输备份文件开始--------------------------------------------------------";

# echo "scp ${local_backup_dir}/${db_server}_${date_now}.tar.gz ${backup_user}@${backup_server}:/${remote_backup_dir}";

# scp ${local_backup_dir}/${file_name}_${db_server}_${date_now}.tar.gz ${backup_user}@${backup_server}:/${remote_backup_dir};

/home/oracle/auto_scp.sh >> /home/oracle/auto_scp.log

echo "-- $(date +'%Y-%m-%d %H:%M:%S') 传输备份文件结束--------------------------------------------------------";

echo "-- $(date +'%Y-%m-%d %H:%M:%S') 删除当前备份原始文件开始----------------------------------------------";

echo "rm -rf ${local_backup_dir}/${db_server}_${date_now}.dmp ${local_backup_dir}/${db_server}_${date_now}.log"

rm -rf ${local_backup_dir}/${file_name}_${db_server}_${date_now}.dmp ${local_backup_dir}/${file_name}_${db_server}_${date_now}.log;

echo "-- $(date +'%Y-%m-%d %H:%M:%S') 删除当前备份原始文件结束 ----------------------------------------------";

echo "-- $(date +'%Y-%m-%d %H:%M:%S') 删除一个月前备份压缩文件开始----------------------------------------------";

# echo "rm -rf ${local_backup_dir}/${db_server}_${date_now}.tar.gz"

# rm -rf ${local_backup_dir}/${db_server}_${date_now}.tar.gz;

find ${local_backup_dir} -mtime +30 -type f -name "*.*" -exec rm -rvf {} ;

echo "-- $(date +'%Y-%m-%d %H:%M:%S') 删除一个月前备份压缩文件结束----------------------------------------------";

echo "-- $(date +'%Y-%m-%d %H:%M:%S') 自动备份数据库并压缩结束----------------------------------------------";

chmod +x /shell/backup_oracle.sh

crontab -e

0 1 * * * nohup /usr/bin/sh /shell/backup_oracle.sh >> /shell/backup_oracle.log &

vim auto_scp.sh

#! /usr/bin/expect

set timeout 5

set date_now $(date +%Y%m%d);

# set ip 10.180.39.8[lindex $argv 0]

set password "app@2022"

spawn scp /opt/oracle/app/admin/orcl/dpdump/Aj_orcl_$date_now.tar.gz app@10.180.39.85:/sdb5/dmpbackup83/

#expect "yes/no"

#send "yesn"

expect "password:"

send "$passwordn"

interact

来源:【九爱网址导航www.fuzhukm.com】 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

最新资讯