此步骤自动从生产服务器备份数据,传输到开发机器,并恢复数据库。
从192.168.200.156备份数据传输到192.168.201.11并自动恢复数据
1.准备ssh无密码传输
(1)在200.156上操作
[root@rh ~]ssh-keygen -t rsa
一直回车下去,其中公共密钥保存在 ~/.ssh/id_rsa.pub 私有密钥保存在 ~/.ssh/id_rsa
chmod 755 ~/.ssh
(2)scp ~/.ssh/id_rsa.pub [email protected]:/root/.ssh/authorized_keys
输入201.11密码
2.200.156的备份传输脚本
用expdp impdp前提建好目录,156目录是DUMP在/opt 11目录是dump_dir在/home/impscript/
select * from dba_directories CREATE DIRECTORY dump_dir AS '/home/impscript/' grant read,write on directory dump_dir to SNKIMP;
vi /home/snkback.sh
#!/bin/bash rm -f /opt/snk.dmp su - oracle -c "/home/oracle/app/oracle/product/11.2.0/dbhome_1/bin/expdp snkData/snk_bfc_Pro DIRECTORY=DUMP DUMPFILE=snk.dmp SCHEMAS=SNKDATA PARALLEL=4" scp /opt/snk.dmp [email protected]:/home/impscript/
然后chmod -r 777 /home/snkback.sh
3.加入计划,每天1点执行
crontab -e
增加0 1 * * * /home/snkback.sh
3.201.11操作
(1)预先建立好DIRECTORY和用户SNKIMP并授权使用
dump_dir在/tmp下,授权目录oracle可使用
chmod -R 777 /home/impscript/*
(2)vi /home/impscript/imp.sh
#!/bin/bash rm -f /home/impscript/snk.log chmod -R 777 /home/impscript/snk.dmp su - oracle -c "/u01/app/product/11.2/bin/sqlplus /nolog" <<EOF connect / as sysdba drop user SNKIMP cascade; drop tablespace SNKIMP including contents and datafiles; create tablespace SNKIMP logging datafile '/extdisk/orclextdata/snkimp01.dbf' size 200m autoextend on next 50m maxsize 20480m extent management local; create user SNKIMP identified by SNKIMP default tablespace SNKIMP; grant connect,resource,dba to SNKIMP; grant read,write on directory dump_dir to SNKIMP; exit EOF su - oracle -c "impdp SNKIMP/SNKIMP@orcl DIRECTORY=dump_dir DUMPFILE=snk.dmp REMAP_SCHEMA=SNKDATA:SNKIMP logfile=snk.log PARALLEL=4" rm -f /home/impscript/snk.dmp
(3)crontab -e
0 2 * * * /home/impscript/imp.sh