欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Linux运维之Oracle数据库自动备份脚本功能实施

程序员文章站 2022-05-18 14:29:21
linux运维之oracle自动备份脚本功能实施 #!/bin/bash myvar=/home/oracle/bin/rman_cmd mysql=/home/oracle/bi...

linux运维之oracle自动备份脚本功能实施

Linux运维之Oracle数据库自动备份脚本功能实施

#!/bin/bash

myvar=/home/oracle/bin/rman_cmd

mysql=/home/oracle/bin/sql_cmd.sql
myora=/home/oracle/bin/ora_cmd.sql
log=/backup_rman/log/rman_full_`date +%y%m%d%h`.log
#创建备份目录
cd /backup_rman
mkdir bak_`date +%y%m%d%h`
chown oracle:oinstall bak_`date +%y%m%d%h`
chmod 755 bak_`date +%y%m%d%h`
echo "recovery bak_`date +%y%m%d%h` suess" >> $log
sleep 10
#创建备份程序 rman_cmd
if [ ! -f "$myvar" ]; then
 touch $myvar
 chown oracle:oinstall $myvar
 chmod 755 $myvar
 echo "create rmn_cmd uess" >> $log
fi
sleep 10
if [ ! -f "$mysql" ]; then
 touch $mysql
 chown oracle:oinstall $mysql
 chmod 755 $mysql
 echo "create sql_cmd uess" >> $log
fi
sleep 10
if [ ! -f "$myora" ]; then
 touch $myora
 chown oracle:oinstall $myora
 chmod 755 $myora
 echo "create ora_cmd uess" >> $log
fi
sleep 10
##开启oracle归档模式
echo " shutdown immediate; "           >>/home/oracle/bin/sql_cmd.sql
echo " startup mount; "                      >>/home/oracle/bin/sql_cmd.sql
echo " alter database archivelog; "     >>/home/oracle/bin/sql_cmd.sql
echo " exit "                                         >>/home/oracle/bin/sql_cmd.sql
##关闭oracle归档模式
echo " shutdown immediate; "             >>/home/oracle/bin/ora_cmd.sql
echo " startup  mount; "                       >>/home/oracle/bin/ora_cmd.sql
echo " alter database noarchivelog; "  >>/home/oracle/bin/ora_cmd.sql
echo " alter database open; "               >>/home/oracle/bin/ora_cmd.sql
echo " exit "                                          >>/home/oracle/bin/ora_cmd.sql
#编辑备份程序 rman_cmd
echo " crosscheck archivelog all;                          "                                                                          >>/home/oracle/bin/rman_cmd
echo " delete noprompt expired archivelog all;  "                                                                                     >>/home/oracle/bin/rman_cmd
echo " run {  "                                                                                                                       >>/home/oracle/bin/rman_cmd
echo "         configure retention policy to recovery window of 4 days; "                                                >>/home/oracle/bin/rman_cmd
echo "         configure controlfile autobackup off; "                                                                                >>/home/oracle/bin/rman_cmd
echo "         allocate channel c1 device type disk  format '/backup_rman/bak_`date +%y%m%d%h`/rman_full_%t_%u'  maxpiecesize=3g; " >>/home/oracle/bin/rman_cmd
echo "         allocate channel c2 device type disk  format '/backup_rman/bak_`date +%y%m%d%h`/rman_full_%t_%u'  maxpiecesize=3g; " >>/home/oracle/bin/rman_cmd
echo "         allocate channel c3 device type disk  format '/backup_rman/bak_`date +%y%m%d%h`/rman_full_%t_%u'  maxpiecesize=3g; " >>/home/oracle/bin/rman_cmd
echo "         backup as compressed backupset full database format '/backup_rman/bak_`date +%y%m%d%h`/full_bk1_%u%p%s.rmn'; "       >>/home/oracle/bin/rman_cmd
echo "         backup full database format '/backup_rman/bak_`date +%y%m%d%h`/full_bk1_%u%p%s.rmn'; "                               >>/home/oracle/bin/rman_cmd
echo "         backup current controlfile format '/backup_rman/bak_`date +%y%m%d%h`/rman_%t_ctl_%u'; "                              >>/home/oracle/bin/rman_cmd
echo "         backup spfile format '/backup_rman/bak_`date +%y%m%d%h`/rman_%t_spfile_%u'; "                                        >>/home/oracle/bin/rman_cmd
echo "         release channel c1; "                                                                                                  >>/home/oracle/bin/rman_cmd
echo "         release channel c2; "                                                                                                  >>/home/oracle/bin/rman_cmd
echo "         release channel c3; "                                                                                                  >>/home/oracle/bin/rman_cmd
echo " } "                                                                                                                            >>/home/oracle/bin/rman_cmd
echo " crosscheck backupset; "                                                                                                        >>/home/oracle/bin/rman_cmd
echo " delete noprompt expired backup; "                                                                                              >>/home/oracle/bin/rman_cmd
echo " delete noprompt obsolete; "                                                                                                    >>/home/oracle/bin/rman_cmd
echo " crosscheck archivelog all; "                                                                                                   >>/home/oracle/bin/rman_cmd
echo " delete noprompt expired archivelog all;  "                                                                                     >>/home/oracle/bin/rman_cmd
#开始执行备份程序 rman_cmd
source /home/oracle/.bash_profile
/u01/app/oracle/product/11.2.0/dbhome_1/bin/sqlplus / as sysdba @ /home/oracle/bin/sql_cmd.sql >> $log
/u01/app/oracle/product/11.2.0/dbhome_1/bin/rman target / nocatalog cmdfile=/home/oracle/bin/rman_cmd  log=/backup_rman/log/rman_full_`date +%y%m%d%h`.log
/u01/app/oracle/product/11.2.0/dbhome_1/bin/sqlplus / as sysdba @ /home/oracle/bin/ora_cmd.sql >> $log

#rm -rf /home/oracle/bin/rman_cmd
#删除备份程序 rman_cmd 下次备份重新创建
rm -rf $myvar
rm -rf $mysql
rm -rf $myora