RMAN backup script column sharing

Source: Internet
Author: User

In ORACLE databases, there are many RMAN backup scripts. The following describes how to use shell scripts to back up data through RMAN, upload RMAN backup files through FTP, and archive log files.

Fullback. sh, which uses the/home/oracle/backup/bin/fullback command to back up the database. rcv. log files are generated in the/home/oracle/backup/logs directory.

   1: [oracle@DB-Server bin]$ more fullback.sh
   2:  
   3: #!/bin/bash
   4:  
   5: export ORACLE_BASE=/u01/app/oracle
   6:  
   7: export ORACLE_SID=gps
   8:  
   9: ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1; export ORACLE_HOME
  10:  
  11: TMP=/tmp; export TMP
  12:  
  13: TMPDIR=$TMP; export TMPDIR
  14:  
  15: PATH=/usr/sbin:$PATH; export PATH
  16:  
  17: PATH=$ORACLE_HOME/bin:$PATH; export PATH
  18:  
  19: LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib; export LD_LIBRARY_PATH
  20:  
  21: CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib;
  22:  
  23: export CLASSPATH
  24:  
  25: TODAY=`date +%Y_%m_%d`
  26:  
  27: rman nocatalog target / cmdfile /home/oracle/backup/bin/fullback.rcv log /home/oracle/backup/logs/fullbackup_$TODAY.log
  28:  
  29: /home/oracle/backup/bin/ftpbackup.sh
  30:  

The fullback. rcv file is very simple, as shown below:

   1: [oracle@DB-Server bin]$ more /home/oracle/backup/bin/fullback.rcv
   2:  
   3: run{
   4:  
   5: allocate channel c4 type disk;
   6:  
   7: backup as compressed backupset
   8:  
   9: skip inaccessible
  10:  
  11: tag fullbackupwitharchivelog
  12:  
  13: (database);
  14:  
  15: backup current controlfile;
  16:  
  17: backup spfile;
  18:  
  19: sql "alter system archive log current";
  20:  
  21: delete noprompt obsolete;
  22:  
  23: release channel c4;
  24:  
  25: }
  26:  

The backup file generated by RMAN needs to be uploaded to the FTP server through FTP. A database server does not have so much space to store backups for multiple days, and the other is for Disaster Tolerance and data security needs.

In the following script, the FTP server and username and password are all replaced by xxx. In the actual environment, use specific information instead.

   1: [oracle@DB-Server bin]$ more ftpbackup.sh 
   2:  
   3: #!/bin/sh、
   4:  
   5: rm -f /home/oracle/.netrc
   6:  
   7: date_yesterday=`date -d'yesterday' +%Y_%m_%d`
   8:  
   9: date_today=`date +%Y_%m_%d`
  10:  
  11: echo "default login xxxx password xxxxxx" >> /home/oracle/.netrc
  12:  
  13: echo "macdef init" >> /home/oracle/.netrc
  14:  
  15: echo "binary" >> /home/oracle/.netrc
  16:  
  17: echo "cd archivelog" >> /home/oracle/.netrc
  18:  
  19: echo "mkdir $date_yesterday" >> /home/oracle/.netrc
  20:  
  21: echo "cd $date_yesterday" >> /home/oracle/.netrc
  22:  
  23: echo "lcd /u04/flash_recovery_area/gps/archivelog/$date_yesterday" >> /home/oracle/.netrc
  24:  
  25: echo "mput *" >> /home/oracle/.netrc
  26:  
  27: echo "cd .." >> /home/oracle/.netrc
  28:  
  29: echo "mkdir $date_today" >>/home/oracle/.netrc
  30:  
  31: echo "cd $date_today" >>/home/oracle/.netrc
  32:  
  33: echo "lcd /u04/flash_recovery_area/gps/archivelog/$date_today" >>/home/oracle/.netrc
  34:  
  35: echo "mput * ">>/home/oracle/.netrc
  36:  
  37: echo "cd .." >>/home/oracle/.netrc
  38:  
  39: echo "cd ../backupset" >> /home/oracle/.netrc
  40:  
  41: echo "mkdir $date_today" >> /home/oracle/.netrc
  42:  
  43: echo "cd $date_today" >> /home/oracle/.netrc
  44:  
  45: echo "lcd /u04/flash_recovery_area/gps/backupset/$date_today" >> /home/oracle/.netrc
  46:  
  47: echo "mput *" >> /home/oracle/.netrc
  48:  
  49: echo "cd .." >> /home/oracle/.netrc
  50:  
  51: echo "cd ../autobackup" >> /home/oracle/.netrc
  52:  
  53: echo "mkdir $date_today" >> /home/oracle/.netrc
  54:  
  55: echo "cd $date_today" >> /home/oracle/.netrc
  56:  
  57: echo "lcd /u04/flash_recovery_area/gps/autobackup/$date_today" >> /home/oracle/.netrc
  58:  
  59: echo "mput *" >> /home/oracle/.netrc
  60:  
  61: echo "quit" >> /home/oracle/.netrc
  62:  
  63: echo "" >> /home/oracle/.netrc
  64:  
  65: chmod 600 /home/oracle/.netrc
  66:  
  67: ftp -i -v xxx.xxx.xxx.xxx 8021 >>/home/oracle/backup/logs/ftp$date_today.log 2>&1
  68:  

In addition, you need to upload archived logs to the FTP server every two hours. The shell script for uploading archived logs every two hours is as follows:

   1: [oracle@DB-Server bin]$ more ftp2hours.sh 
   2:  
   3: #!/bin/sh
   4:  
   5: rm -f /home/oracle/.netrc
   6:  
   7: date_yesterday=`date -d'yesterday' +%Y_%m_%d`
   8:  
   9: date_today=`date +%Y_%m_%d`
  10:  
  11: echo "default login xxxx password xxxx" >> /home/oracle/.netrc
  12:  
  13: echo "macdef init" >> /home/oracle/.netrc
  14:  
  15: echo "binary" >> /home/oracle/.netrc
  16:  
  17: echo "cd archivelog" >> /home/oracle/.netrc
  18:  
  19: echo "mkdir $date_today" >>/home/oracle/.netrc
  20:  
  21: echo "cd $date_today" >>/home/oracle/.netrc
  22:  
  23: echo "lcd /u04/flash_recovery_area/gps/archivelog/$date_today" >>/home/oracle/.netrc
  24:  
  25: echo "mput * ">>/home/oracle/.netrc
  26:  
  27: echo "quit" >> /home/oracle/.netrc
  28:  
  29: echo "" >> /home/oracle/.netrc
  30:  
  31: chmod 600 /home/oracle/.netrc
  32:  
  33: ftp -i -v xxx.xxx.xxx.xxx 8021 >>/home/oracle/backup/logs/ftp2hours.$date_today.log 2>&1
  34:  

Finally, you need to send the log files generated by RMAN backup and FTP Backup files and archived logs to the DBA or system administrator by email,

   1: [oracle@DB-Server bin]$ more chkbackandmail.sh 
   2: #!/bin/bash
   3: rm -f /home/oracle/backup/bin/sendmail.pl
   4: date_today=`date +%Y_%m_%d`
   5: subject="Oracle Backup Alert Service on $date_today"
   6: content="Dear colleagues,
   7:  
   8:    Attached please find the logs of xxx(xxx.xxx.xxx.xxx) oracle database backup and transfer to FTP Server(xxx.xxx.xxx.xxx), please
   9:  review the file and check whether the backup succeeded or not,and double check all backups have been dumped to tape, many tha
  10: nks
  11:  
  12:  
  13:  
  14:  
  15: Best regards
  16: Oracle Alert Services
  17:  
  18: "
  19: file="/home/oracle/backup/logs/fullbackup_$date_today.log,/home/oracle/backup/logs/ftp$date_today.log"
  20: echo "#!/usr/bin/perl" >> /home/oracle/backup/bin/sendmail.pl
  21: echo "use Mail::Sender;" >> /home/oracle/backup/bin/sendmail.pl
  22: echo "\$sender = new Mail::Sender {smtp => 'xxx.xxx.xxx.xxx', from => 'xxxx@xxx.com'}; ">> /home/oracle/backup/bin/sendmai
  23: l.pl
  24: echo "\$sender->MailFile({to => 'xxx@esquel.com',">> /home/oracle/backup/bin/sendmail.pl
  25: echo "cc=>'xxx@xxx.com,xxx@xxx.com,xxx@xxx.com'," >> /home/oracle/backup/b
  26: in/sendmail.pl
  27: echo "subject => '$subject',">> /home/oracle/backup/bin/sendmail.pl
  28: echo "msg => '$content',">> /home/oracle/backup/bin/sendmail.pl
  29: echo "file => '$file'});">> /home/oracle/backup/bin/sendmail.pl
  30: perl /home/oracle/backup/bin/sendmail.pl

Finally, configure and call these shell scripts in the Crontab job. For example, execute fullback. sh, every two hours (for example ...) execute one ftp2hours. sh: Execute chkbackandmail at every morning. sh sends fullback. sh and ftp2hour. sh execution log.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.