如何实现ORACLE数据库的远程自动备份(完整版)
主数据库已经正常工作,之前的数据没有做自动备份,为防止数据丢失,现考虑使用自动备份机制并且远程存储到其它计算机上。具体的实现步骤和环境如下:
服务器A 服务器B 操作系统均为:LINUX 1、 ORACLE_backup_all.sh: #!/bin/sh #name:oracle_backup.sh #PS:oracle automation backup script #write by:iceboard # create in 2014-07-28 # 预先设置变量,然后导出相关数据 ORACLE_BASE=/home/oraBase ORACLE_HOME=/home/oraProduct ORACLE_SID=orcl #ORACLE_TERM=test1 ORACLE_OWNER=oracle export ORACLE_BASE export ORACLE_HOME export ORACLE_SID #export ORACLE_TERM export ORACLE_OWNER NLS_LANG="AMERICAN_AMERICA.ZHS16GBK"; export NLS_LANG #LANG=zh; export LANG cd /home/oracle/backup $ORACLE_HOME/bin/exp username[email=/password@orcl]t/oracle119*@lmssww[/email] file=oracle_`date +%Y-%m-%d`.dmp log=/home/oracle/backup/oracle-`date +%Y-%m-%d`.log feedback=10000 inctype=complete filename=oracle-`date +%Y-%m-%d`-all.tar tar cf $filename oracle_`date +%Y-%m-%d`.dmp filenamegz=oracle-`date +%Y-%m-%d`-all.tar.gz /usr/bin/gzip -f $filename #欲实现远程文件拷贝,其功能与下面的FTP服务拷贝同效 scp -P 端口号 /home/oracle/backup/$filenamegz root@192.168.0.2:/home/backupDB 文件存放于/home/oracle 2、 #chmod +x /home/oracle/oracle_backup_all.sh 3、 Crontab –e #Oracle database backup script #writ by :iceboard #date :2014-7-29 0 2 * * 2-6 /home/oracle/oracle_backup_up.sh 0 2 * * 0 /home/oracle/oracle_backup_full.sh 0 2 * * 1 /home/oracle/oracle_backup_all.sh 上面几行为注释代码,下面的三行,设置了自动执行的时间,分别表示在每周2 通过crontab –l 4、 5、 在服务器A $ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/oracle/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: 2b:59:60:47:2c:7c:e0:4f:4f:26:1e:a3:58:d9:48:49 oracle@A OK 第二步,将生成的密钥拷贝到服务器B $ scp /root/.ssh/id_rsa.pub root@192.168.0.2:/tmp/ The authenticity of host 'B (192.168.0.2)' can't be established. RSA key fingerprint is e1:c2:a8:4e:99:4e:39:d9:05:29:9d:14:36:91:b5:bc. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.0.2' (RSA) to the list of known hosts. [email=root@192.168.0.2's]root@192.168.0.2's[/email] password: id_rsa.pub 100% 228 0.2KB/s 00:00 第三步:用root cd .ssh/ # cat /tmp/id_rsa.pub >>authorized_keys chmod 400 authorized_keys rm -f /tmp/id_rsa.pub 第四步:测试一下刚刚的密钥,看是否能远程登录,如下: oracle:/home/oracle # ssh -p 714 root@192.168.0.2 Last login: Tue Jul 29 10:13:59 2008 from 192.168.0.1 Have a lot of fun... bbs:~ # OK 再试一个文档远程拷贝看能否成功。 oracle:/home/oracle # scp -P 端口号 /home/oracle/oracle_backup.sh root@192.168.0.2:/tmp/ oracle_backup.sh OK 6、至此,我们的自动备份功能便可以正常执行了。以上的操作环境均是在ROOT权限下执行,LINUX的版本为RH4U5。测试通过,程序正常执行。 |