将Oracle数据库数据每天备份恢复一次数据到另一台服务器上两份数据
1、创建用户,授权,创建测试数据
创建用户 CREATE USER test identified by 123; 授权 grant dba to test; 创建测试数据 create table a(id int); insert into a select 1 from dual connect by level<=50;
2、创建备份目录
create directory UserName_dir as '/home/oracle/oradb'; grant read,write on directory UserName_dir to test;
3、导出数据并获取当日时间并推送到另一台服务器上
排除需要导出的表:exclude=table:"in('SZSY','JBXX_H','MENT')",表名必须大写
[oracle@server80 script]$ cat ora_exp_backup.sh #!/bin/bash datadir=/home/oracle/oradb mydmp=/home/oracle/oradb/exp_test_$(date +%Y%m%d).dmp if [ -f "$mydmp" ];then rm -rf $mydmp fi cat << EOF > /home/oracle/script/exp_test.par dumpfile=exp_test_$(date +%Y%m%d).dmp logfile=exp_test_$(date +%Y%m%d).log directory=UserName_dir SCHEMAS=TEST EOF expdp \' / as sysdba \' parfile=/home/oracle/script/exp_test.par
#获取备份日志关键字“sucessfully” ora_backup_info=$(cat /home/oracle/oradb/exp_test_$(date +%Y%m%d).log|tail -n 1|awk '{print $3}') if [ $ora_backup_info = 'successfully' ];then cd $datadir && tar -zcf exp_test_$(date +%Y%m%d).tar.gz exp_test_$(date +%Y%m%d).dmp rsync -avz $datadir/exp_test_$(date +%Y%m%d).tar.gz root@localhost:/home/oracle/ if [ $? = '0' ];then echo "1" > /tmp/ora_backup_status.log rsync -avz /tmp/ora_backup_status.log root@localhost:/home/oracle/oradb if [ $? = '0' ];then cd $datadir && rm -rf exp_test_$(date +%Y%m%d)* fi else echo "数据备份成功,发送失败" > /tmp/ora_backup_status.log fi else echo "数据备份失败" > /tmp/ora_backup_status.log fi
4、校验数据并导入到新服务器数据库里上
[oracle@server80 script]$ cat ora_imp_backup.sh #!/bin/bash cat << EOF > /home/oracle/script/imp_text.par dumpfile=exp_test_$(date +%Y%m%d).dmp logfile=imp_test_$(date +%Y%m%d).log directory=UserName_dir remap_schema=test:test2 EOF date=$(date +%Y-%m-%d)
#获取该文件的最新修改时间 ora_backup_date=$(stat /home/oracle/oradb/ora_backup_status.log|tail -n 1|awk '{print $2}')
#获取备份成功返回的状态码 ora_rsync_status=$(cat /home/oracle/oradb/ora_backup_status.log)
mytgz=/home/oracle/exp_test_$(date +%Y%m%d).tar.gz if [ $ora_backup_date = $date ];then if [ $ora_rsync_status = '1' ];then sh /home/oracle/script/dropuser2.sh impdp \' / as sysdba \' parfile=/home/oracle/script/imp_text.par if [ $? = '0' ];then #数据导入成功 echo "1" >/tmp/ora_imp2_status.log
cd /home/oracle/oradb/ && rm -rf exp_test_$(date +%Y%m%d)* else echo "是最新数据,数据导入失败" >/tmp/ora_imp2_status.log fi else echo "不是最新数据,失败" >/tmp/ora_imp_status2.log fi else sleep 30 if [ $ora_rsync_status = '1' ] && [ $ora_backup_date = $date ];then sh /home/oracle/script/dropuser2.sh impdp \' / as sysdba \' parfile=/home/oracle/script/imp_text.par if [ $? = '0' ];then #数据导入成功 echo "1" >/tmp/ora_imp_status2.log cd /home/oracle/oradb/ && rm -rf exp_test_$(date +%Y%m%d)* else echo "是最新数据,数据导入失败" >/tmp/ora_imp2_status.log fi else echo "两次检查,数据不是最新的,结束数据导入" >/tmp/ora_imp2_status.log fi fi
5、创建删除用户脚本
目的:在导入数据的时候由于数据已经存在,且覆盖出错,清理用户数据重新导入即可,删除session,否则删除报错。
vim dropuser.sh
#!/bin/bash ${ORACLE_HOME}/bin/sqlplus /nolog <<EOF connect / as sysdba SELECT 'alter system kill session '''||sid||','||serial#||''' immediate;' FROM V\$SESSION WHERE USERNAME='TEST1'; drop user test1 cascade; exit EOF
6、修改一份适合第二份数据存储的数据
修改ora_imp.backup.sh,dropuser.sh 修改创建两份文件即可。第一份脚本执行完不删备份的数据,第二份脚本执行完后删除备份的数据。
7、总执行脚本如下
[oracle@server81 script]$ cat ora_imp.sh #!/bin/bash #判断压缩包是否存在 mytgz=/home/oracle/exp_test_$(date +%Y%m%d).tar.gz if [ ! -f "$mytgz" ];then exit else cd /home/oracle/ && tar xf exp_test_$(date +%Y%m%d).tar.gz -C /home/oracle/oradb/ && rm -rf exp_test_$(date +%Y%m%d).tar.gz fi ora_imp1=/home/oracle/script/ora_imp_backup.sh ora_imp2=/home/oracle/script/ora_imp2_backup.sh #循环执行导入两份数据 for i in $ora_imp1 $ora_imp2; do sh $i done
温馨提示:本文由自己记录,仅作参考,出了问题概不负责。。。。。