Oracle Rman timed Backup script ____oracle

Source: Internet
Author: User
Tags chmod mkdir
The recent need for a scheduled Oracle Backup program, can be restored to the latest state, reference http://blog.sina.com.cn/s/blog_7756ebc30100tnqy.html, I have in the shell processing a bit, Get 2 shell scripts, init.sh, oraclebackup.sh, execute init.sh param1 (backup file path) param2 (archive log path), you can set up a scheduled backup, the script will set the timer, For more information, see the opening instructions for the init.sh file, and post the scripting code below:
init.sh

#!/bin/sh
# Oracle Automatic Timing Backup Program
#说明: Init.sh and oraclebackup.sh are 2 shell scripts for Oracle automatic scheduled backups.
# Backup tool: RMAN
# Backup mode: Incremental backup
# Archive Mode: Yes
# Use example:./init.sh/home/dev/databasebackup/home/dev/archivelogbackup
# The first parameter is the directory where the Oracle backup files are stored, and the second is the storage directory of the archived redo logs
# Backup the archive redo log every time the database is backed up, and then delete the archive redo log
# these 2 scripts need to be placed on an Oracle server
# Environment variables need to be configured well in advance Oracle_base,oracle_home,oracle_sid
# Oracle Default non-archive mode after installation
# init.sh need to restart the database during script execution
#init. Sh Script Execution Process:
# 1. Replace the 2 directory parameters with the oraclebackup.sh
# 2. Replace the ORACLE_BASE,ORACLE_HOME,ORACLE_SID 3 environment variables with the oraclebackup.sh
# 3. Configure Rman
# 4. Set Oracle to archive mode
# 5. Generate Timer files
# 6. Set Timer, timing Oracle Backup, backup file default save 15 days
# Perform level 0 backups every Sunday 1:00
# from Monday to Wednesday, Friday to Saturday 1:00 perform a Level 2 incremental backup
# Perform level 1 incremental backups every Thursday 1:00
# Perform tests every Saturday 16:00
# Delete Expired backup data every Sunday 18:00
#
# after init.sh is done, you can also perform a manual backup and restore of the database
# Perform level 0 backup:
# oraclebackup.sh-l0
# Perform Level 1 backup
# ORACLEBACKUP.SH-L1
# Perform level 2 backup
# ORACLEBACKUP.SH-L2
# Detect Backup Integrity
# oraclebackup.sh-c
# Delete Expired backups, save for 15 days by default
# oraclebackup.sh-d
# Restore the database to the latest state
# Oraclebackup.sh-r
#日志:
# The log executed by the script is in the Rmanlog directory of the first parameter (/home/dev/databasebackup), that is,/home/dev/databasebackup/rmanlog,
# Check the log below this folder periodically to see the backup information




Database_path= ""
Archivelog_path= ""
Oracle_base_val= ""
Oracle_home_val= ""
Oracle_sid_val= ""


key1= "Database_path"
Key2= "Archivelog_path"
key3= "Export Oracle_base"
key4= "Export Oracle_home"
key5= "Export Oracle_sid"


#配置RMAN, backup for 15 days, optimized backup, automatic backup control file, control file backup location
template_rmancmd= "Connect target/;
Run {
CONFIGURE RETENTION POLICY to RECOVERY WINDOW of the days;
CONFIGURE BACKUP optimization on;
CONFIGURE Controlfile autobackup on;
CONFIGURE controlfile autobackup FORMAT for DEVICE TYPE DISK to ' oldpath_database/controlfile_backup/controlfile_%f ';
}"

Rmancmd= ""


#替换RMAN命令中的路径参数
function Replacermancmd ()
{
Rmancmd= ' echo ${template_rmancmd} | Sed-n "S#oldpath_database#${database_path} #p" '

echo "==================rmancmd======================"
Echo ${rmancmd}
echo "==============================================="
}


#执行RMAN命令, configuring Rman
function setrmanconf ()
{
Rman log=\ ' ${database_path}/rmanlog/paidb_rman_conf.log\ ' append <<eof
${rmancmd}
Eof
}


#把oracle设置为归档模式
function Setarchivelog ()
{
Sqlplus/as SYSDBA <<eof
Shutdown immediate;
startup Mount;
ALTER DATABASE Archivelog;
Alter system set CONTROL_FILE_RECORD_KEEP_TIME=30 Scope=both;
Alter system set log_archive_dest_1= ' Location=${archivelog_path}/archivelog ' Scope=both;
ALTER DATABASE open;
Alter system archive log start;
Exit
Eof
}


#创建定时器文件crontabfile, set timer
#每周日1:00 perform level 0 backup
#每周一至周三, Friday to Saturday 1:00 perform a Level 2 incremental backup
#每周四1:00 perform level 1 incremental backups
#每周六16:00 performing detection
#每周日18:00 Delete expired backup data
function Setcrontab ()
{
If [-E./crontabfile]
Then
Rm./crontabfile
Fi

Touch./crontabfile

chmod 777./crontabfile

#每周日1:00 perform level 0 backup
echo "0 1 * * 0 ' pwd '/oraclebackup.sh-l0" >> crontabfile

#每周一至周三, Friday to Saturday 1:00 perform a Level 2 incremental backup
echo "0 1 * * 1-3,5-6 ' pwd '/oraclebackup.sh-l2" >> crontabfile

#每周四1:00 perform level 1 incremental backups
echo "0 1 * * 4 ' pwd '/oraclebackup.sh-l1" >> crontabfile

#每周六16:00 performing detection
echo "0 * * 6 ' pwd '/oraclebackup.sh-c" >> crontabfile

#每周日18:00 Delete expired backup data
echo "0 * * 0 ' pwd '/oraclebackup.sh-d" >> crontabfile

Crontab./crontabfile
}


#创建子目录, create archivelog under archive log directory
#数据库备份目录下创建rmanlog, Controlfile_backup,database_backup Directory
#如果已经存在, you do not create
function Makesubdir ()
{
if [!-D "${archivelog_path}/archivelog"]
Then
mkdir ${archivelog_path}/archivelog
Fi

if [!-D "${archivelog_path}/rmanlog"]
Then
mkdir ${database_path}/rmanlog
Fi

if [!-D "${archivelog_path}/controlfile_backup"]
Then
mkdir ${database_path}/controlfile_backup
Fi

if [!-D "${archivelog_path}/database_backup"]
Then
mkdir ${database_path}/database_backup
Fi
}


#把数据库备份目录参数替换到oraclebackup. Sh
function Replacedatabasebakpath ()
{
Sed-i "S#^\ (${key1} *= *\) \ ([^]*\) \ (*.*\) #\1${database_path}\3#"./oraclebackup.sh
}


#把归档日志目录参数替换到oraclebackup. Sh
function Replacearchivelogbakpath ()
{
Sed-i "S#^\ (${key2} *= *\) \ ([^]*\) \ (*.*\) #\1${archivelog_path}\3#"./oraclebackup.sh
}


#把ORACLE_HOME等参数替换到oraclebackup. Sh
function Replaceoraclehome ()
{
Sed-i "S#^\ (${key3} *= *\) \ ([^]*\) \ (*.*\) #\1${oracle_base_val}\3#"./oraclebackup.sh
Sed-i "S#^\ (${key4} *= *\) \ ([^]*\) \ (*.*\) #\1${oracle_home_val}\3#"./oraclebackup.sh
Sed-i "S#^\ (${key5} *= *\) \ ([^]*\) \ (*.*\) #\1${oracle_sid_val}\3#"./oraclebackup.sh
}


function usage ()
{
echo "------------------------------------------------------------------------"
Echo
echo "Example:"
echo "Sample:./init.sh/aaa/bbb/databasebackuppath/aaa/bbb/archivelogbackuppath"
Echo
echo "Note:the path must be exist!"
echo "------------------------------------------------------------------------"
}


#指定的路径不存在
function Pathnotexist ()
{
echo "************************************************************************"
Echo
echo "Path: ${1} not exist!"
Echo
echo "************************************************************************"
}


#环境变量没找到
function Envnotfound ()
{
echo "************************************************************************"
Echo
echo "env ${1} not found!"
Echo
echo "************************************************************************"
}


function Check ()
{
if [!-d "$DATABASE _path"]
Then
Pathnotexist ${1}
Exit 0
Fi

if [!-d "$ARCHIVELOG _path"]
Then
Pathnotexist ${2}
Exit 0
Fi

if [! $ORACLE _base]
Then
Envnotfound "Oracle_base"
Exit 0
Fi

if [! $ORACLE _home]
Then
Envnotfound "Oracle_home"
Exit 0
Fi

if [! $ORACLE _sid]
Then
Envnotfound "Oracle_sid"
Exit 0
Fi
}


function Main ()
{
if (($# < "2"))
Then
Usage
Exit 0
Fi

Database_path=$1

Archivelog_path=$2

database_path=${database_path%/}

archivelog_path=${archivelog_path%/}

Check

Oracle_base_val= $ORACLE _base

Oracle_home_val= $ORACLE _home

Oracle_sid_val= $ORACLE _sid

Replaceoraclehome

Makesubdir

Replacedatabasebakpath

Replacearchivelogbakpath

Replacermancmd

Setrmanconf

echo "--------->start conf Archive Log"

Setarchivelog


echo "--------->end conf Archive Log"

chmod 777./oraclebackup.sh

Setcrontab


}


Main $ $


oraclebakcup.sh

#!/bin/sh
Export oracle_base=
Export Oracle_home=
Export oracle_sid=
Export path= $ORACLE _home/bin: $PATH


Database_path= ""
Archivelog_path= ""


#====================================================
#0级备份rman命令
template_rmancmd_level0= "Connect target/;
Run {
Allocate channel C1 type disk;
Backup incremental level=0 database format ' oldpath_database/database_backup/paidb_level0_%u ' tag= ' level0 ';
SQL ' alter system archive log current ';
Backup Archivelog all format ' oldpath_archive/archivelog/paidb_arch_%u ' delete input;
Release channel C1;
}
";


#1级备份rman命令
template_rmancmd_level1= "Connect target/;
Run {
Allocate channel C1 type disk;
Backup incremental level=1 database format ' oldpath_database/database_backup/paidb_level1_%u ' tag= ' level1 ';
SQL ' alter system archive log current ';
Backup Archivelog all format ' oldpath_archive/archivelog/paidb_arch_%u ' delete input;
Release channel C1;
}
";


#2级备份rman命令
template_rmancmd_level2= "Connect target/;
Run {
Allocate channel C1 type disk;
Backup incremental level=2 database format ' oldpath_database/database_backup/paidb_level2_%u ' tag= ' level2 ';
SQL ' alter system archive log current ';
Backup Archivelog all format ' oldpath_archive/archivelog/paidb_arch_%u ' delete input;
Release channel C1;
}
";
#====================== the Rman command after the replacement path =======================
Rmancmd_level0= ""
Rmancmd_level1= ""
Rmancmd_level2= ""
#==============================================================


#替换0级备份命令中的路径参数
function ReplaceRmanCMDl0 ()
{
Rmancmd_level0= ' echo ${template_rmancmd_level0} | Sed-n "S#oldpath_database#${database_path} #p" '


Rmancmd_level0= ' echo ${rmancmd_level0} | Sed-n "S#oldpath_archive#${archivelog_path} #p" '

echo "################# #RMANCMD ######################"
Echo ${rmancmd_level0}
echo "###############################################"
}


#替换1级备份命令中的路径参数
function ReplaceRmanCMDl1 ()
{
Rmancmd_level1= ' echo ${template_rmancmd_level1} | Sed-n "S#oldpath_database#${database_path} #p" '


Rmancmd_level1= ' echo ${rmancmd_level1} | Sed-n "S#oldpath_archive#${archivelog_path} #p" '

echo "################# #RMANCMD ######################"
Echo ${rmancmd_level1}
echo "###############################################"
}


#替换2级备份命令中的路径参数
function ReplaceRmanCMDl2 ()
{
Rmancmd_level2= ' echo ${template_rmancmd_level2} | Sed-n "S#oldpath_database#${database_path} #p" '


Rmancmd_level2= ' echo ${rmancmd_level2} | Sed-n "S#oldpath_archive#${archivelog_path} #p" '

echo "################# #RMANCMD ######################"
Echo ${rmancmd_level2}
echo "###############################################"
}


#====================================================
#0级备份
function BackupLevel0 ()
{
Rman log=\ ' ${database_path}/rmanlog/paidb_rman_level0.log\ ' append <<eof
${rmancmd_level0}
Eof
}


#1级备份
function BackupLevel1 ()
{
Rman log=\ ' ${database_path}/rmanlog/paidb_rman_level1.log\ ' append <<eof
${RMANCMD_LEVEL1}
Eof
}


#2级备份
function BackupLevel2 ()
{
Rman log=\ ' ${database_path}/rmanlog/paidb_rman_level2.log\ ' append <<eof
${RMANCMD_LEVEL2}
Eof
}


#检查oracle备份文件, if there is a corrupted record, it is placed in the V$database_block_corruption view,
#需要DBA定期检查V $DATABASE _block_corruption View, after the block is repaired,
The corresponding records in the #V $DATABASE _block_corruption are automatically deleted
function Backupcheck ()
{
Rman log=\ ' ${database_path}/rmanlog/rmancheck.log\ ' append <<eof
Connect target/;
Run {
#check Database
Backup validate check logical database;
#check Backup Set
Restore database validate check logical;
#crosscheck Backup
Crosscheck backup;
}
Eof
}


#删除过期的备份文件
function Backupdelete ()
{
Rman log=\ ' ${database_path}/rmanlog/rmandelete.log\ ' append <<eof
Connect target/;
Run {
Delete NoPrompt obsolete;
Delete noprompt expired backup;
}
Eof
}


#恢复数据库到最新状态
function Backuprestore ()
{
Rman log=\ ' ${database_path}/rmanlog/rmanrestore.log\ ' append <<eof
Connect target/;
Run {
Restore database;
Recover database;
}
Eof
}


#====================================================
#0级备份
function backupl0 ()
{
ReplaceRmanCMDl0

BackupLevel0
}


#1级备份
function Backupl1 ()
{
ReplaceRmanCMDl1

BackupLevel1
}


#2级备份
function Backupl2 ()
{
ReplaceRmanCMDl2

BackupLevel2
}


#检查备份文件
function BACKUPCK ()
{
Backupcheck
}


#删除过期的备份文件
function Backupdel ()
{
Backupdelete
}


#恢复数据库到最新状态
function Backupres ()
{
Backuprestore
}


#====================================================


function usage ()
{
echo "------------------------------------------------------------------------"
Echo
echo "Example:"
echo "Sample:./oraclebackup.sh-l0|-l1|-l2|-c|-d|-r"
Echo
Echo Note:-l0 backup level0 Level 0 backups
-L1 backup Level1 Level 1 backups
-L2 backup Level2 Level 2 backups
-C Backup Check checks for backups
-d Backup Delete Remove expired backup
-R Backup restore restores the database to the latest status
echo "------------------------------------------------------------------------"
}


function Main ()
{
if (($# < "1"))
Then
Usage
Exit 0
Fi

Case "$" in
"-l0")
Backupl0
Exit 0
;;
"-l1")
Backupl1
Exit 0
;;
"-l2")
Backupl2
Exit 0
;;
"-C")
Backupck
Exit 0
;;
"-D")
Backupdel
Exit 0
;;
"-R")
Backupres
Exit 0
;;
Esac

Usage
}


Main $
Related Article

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.