在ORACLE数据库中,RMAN备份的脚本非常多,下面介绍一例shell脚本如何通过RMAN备份,以及FTP上传RMAN备份文件以及归档日志文件的脚本。
fullback.sh 里面调用RMAN命令做数据库备份,它使用的cmdfile为/home/oracle/backup/bin/fullback.rcv,同时在/home/oracle/backup/logs目录下生成日志文件。
[oracle@DB-Server bin]$ more fullback.sh
#!/bin/bash
export ORACLE_BASE=/u01/app/oracle
export ORACLE_SID=gps
ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1;
export ORACLE_HOME
TMP=/tmp; export TMP
TMPDIR=$TMP; export TMPDIR
PATH=/usr/sbin:$PATH; export PATH
PATH=$ORACLE_HOME/bin:$PATH; export PATH
LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib;
export LD_LIBRARY_PATH
CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib;
export CLASSPATH
TODAY=`date +%Y_%m_%d`
rman
nocatalog target / cmdfile /home/oracle/backup/bin/fullback.rcv log /home/oracle/backup/logs/fullbackup_$TODAY.log
/home/oracle/backup/bin/ftpbackup.sh
fullback.rcv文件非常简单, 如下所示:
[oracle@DB-Server bin]$ more
/home/oracle/backup/bin/fullback.rcv
run{
allocate channel c4 type disk;
backup as
compressed backupset
skip inaccessible
tag fullbackupwitharchivelog
(database);
backup current controlfile;
backup spfile;
sql "alter system archive log current";
delete noprompt obsolete;
release channel c4;
}
RMAN生成的备份文件,需要通过FTP上传到FTP服务器,一则数据库服务器没有这么多空间存储多天的备份,二则是出于容灾、数据安全需要。
下面脚本中FTP服务器,用户名密码均使用xxx替代,在实际环境中,使用具体的信息替代即可。
[oracle@DB-Server bin]$ more ftpbackup.sh
#!/bin/sh、
rm -f /home/oracle/.netrc
date_yesterday=`date -d'yesterday' +%Y_%m_%d`
date_today=`date +%Y_%m_%d`
echo "default login xxxx password xxxxxx" >> /home/oracle/.netrc
echo "macdef init" >>
/home/oracle/.netrc
echo "binary" >>
/home/oracle/.netrc
echo "cd archivelog" >>
/home/oracle/.netrc
echo "mkdir $date_yesterday" >>
/home/oracle/.netrc
echo "cd $date_yesterday" >>
/home/oracle/.netrc
echo "lcd
/u04/flash_recovery_area/gps/archivelog/$date_yesterday" >>
/home/oracle/.netrc
echo "mput *" >>
/home/oracle/.netrc
echo "cd .." >>
/home/oracle/.netrc
echo "mkdir $date_today"
>>/home/oracle/.netrc
echo "cd $date_today"
>>/home/oracle/.netrc
echo "lcd
/u04/flash_recovery_area/gps/archivelog/$date_today"
>>/home/oracle/.netrc
echo "mput *
">>/home/oracle/.netrc
echo "cd .." >>/home/oracle/.netrc
echo "cd ../backupset" >>
/home/oracle/.netrc
echo "mkdir $date_today" >>
/home/oracle/.netrc
echo "cd $date_today" >>
/home/oracle/.netrc
echo "lcd
/u04/flash_recovery_area/gps/backupset/$date_today" >>
/home/oracle/.netrc
echo "mput *" >>
/home/oracle/.netrc
echo "cd .." >>
/home/oracle/.netrc
echo "cd ../autobackup" >>
/home/oracle/.netrc
echo "mkdir $date_today" >>
/home/oracle/.netrc
echo "cd $date_today" >>
/home/oracle/.netrc
echo "lcd
/u04/flash_recovery_area/gps/autobackup/$date_today" >>
/home/oracle/.netrc
echo "mput *" >>
/home/oracle/.netrc
echo "quit" >> /home/oracle/.netrc
echo "" >> /home/oracle/.netrc
chmod 600 /home/oracle/.netrc
ftp -i -v xxx.xxx.xxx.xxx 8021
>>/home/oracle/backup/logs/ftp$date_today.log 2>&1
另外,关于归档日志也需要每隔2小时上传一次到FTP服务器,2小时上传一次归档日志的shell脚本如下所示:
[oracle@DB-Server bin]$ more ftp2hours.sh
#!/bin/sh
rm -f /home/oracle/.netrc
date_yesterday=`date -d'yesterday' +%Y_%m_%d`
date_today=`date +%Y_%m_%d`
echo "default login xxxx password xxxx"
>> /home/oracle/.netrc
echo "macdef init" >>
/home/oracle/.netrc
echo "binary" >>
/home/oracle/.netrc
echo "cd archivelog" >>
/home/oracle/.netrc
echo "mkdir $date_today"
>>/home/oracle/.netrc
echo "cd $date_today"
>>/home/oracle/.netrc
echo "lcd
/u04/flash_recovery_area/gps/archivelog/$date_today"
>>/home/oracle/.netrc
echo "mput *
">>/home/oracle/.netrc
echo "quit" >> /home/oracle/.netrc
echo "" >> /home/oracle/.netrc
chmod 600 /home/oracle/.netrc
ftp -i -v xxx.xxx.xxx.xxx 8021
>>/home/oracle/backup/logs/ftp2hours.$date_today.log 2>&1
最后需要将RMAN备份生成的日志文件,以及FTP上传备份文件以及归档日志的记录通过邮件形式发送给DBA或系统管理员,
[oracle@DB-Server bin]$ more chkbackandmail.sh
#!/bin/bash
rm -f /home/oracle/backup/bin/sendmail.pl
date_today=`date +%Y_%m_%d`
subject="Oracle Backup Alert Service on
$date_today"
content="Dear colleagues,
Attached
please find the logs of xxx(xxx.xxx.xxx.xxx) oracle database backup and
transfer to FTP Server(xxx.xxx.xxx.xxx), please
review the
file and check whether the backup succeeded or not,and double check all backups
have been dumped to tape, many tha
nks
Best regards
Oracle Alert Services
"
file="/home/oracle/backup/logs/fullbackup_$date_today.log,/home/oracle/backup/logs/ftp$date_today.log"
echo "#!/usr/bin/perl" >>
/home/oracle/backup/bin/sendmail.pl
echo "use Mail::Sender;" >>
/home/oracle/backup/bin/sendmail.pl
echo "\$sender = new Mail::Sender {smtp =>
'xxx.xxx.xxx.xxx', from => 'xxxx@xxx.com'}; ">>
/home/oracle/backup/bin/sendmai
l.pl
echo "\$sender->MailFile({to =>
'xxx@esquel.com',">> /home/oracle/backup/bin/sendmail.pl
echo "cc=>'xxx@xxx.com,xxx@xxx.com,xxx@xxx.com',"
>> /home/oracle/backup/b
in/sendmail.pl
echo "subject => '$subject',">>
/home/oracle/backup/bin/sendmail.pl
echo "msg => '$content',">>
/home/oracle/backup/bin/sendmail.pl
echo "file => '$file'});">>
/home/oracle/backup/bin/sendmail.pl
perl /home/oracle/backup/bin/sendmail.pl
最后在Crontab 作业里面配置调用这些shell脚本。如下如下,在1:01分执行fullback.sh
,每隔两个小时(例如0:50,2:50...)执行一次ftp2hours.sh, 在每天早上8:40执行chkbackandmail.sh 发送fullback.sh
以及ftp2hour.sh的执行日志记录。
原文地址:http://www.cnblogs.com/kerrycode/p/3754169.html
该贴被潇湘隐者编辑于2014-5-27 23:23:57该贴由hui.chen转至本版2014-11-5 16:24:18