sh 具有ARCH或FULL选项的常规备份脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 具有ARCH或FULL选项的常规备份脚本相关的知识,希望对你有一定的参考价值。

#!powershell
# author: fr-criticaldata-data at fr.clara.net

# Arguments
[CmdletBinding()] param ( 
  [Parameter(Mandatory=$True)][Alias('n')][string] $name,
  [Parameter()][Alias('m')][string] $mode = "ARCH",
  [Parameter()][Alias('l')][int] $level = 0
)

# Set environment variables
$env:ORACLE_SID = $name
$env:NLS_DATE_FORMAT="YYYY/MM/DD HH24:MI:SS"

# Set locale variables
$version=1.0
$rman_dest="D:\BACKUP\$name"
$rman_retention="REDUNDANCY 1"
$rman_tag=$(Get-Date -Format 'yyyymmdd')
$output="$PSScriptRoot\logs\${mode}_${name}_$(Get-Date -Format 'yyyymmdd_hhmm').log"

# Functions

Function Configure-Rman {
  [CmdletBinding()] param ( [string] $dest, [string] $retention )
  @"
    configure retention policy to $retention;
    configure default device type to disk;
    configure controlfile autobackup on;
    configure controlfile autobackup format for device type disk to '$dest\CTL_%F';
    configure device type disk parallelism 1 backup type to compressed backupset;
"@ | rman target / log=$output append
}

Function Do-Crosscheck {
  @"
    run {
      allocate channel t1 type disk;
      report schema;
      show all;
      
      crosscheck backup;
      crosscheck datafilecopy all;
      crosscheck archivelog all;
	    release channel t1;
    }
"@ | rman target / log=$output append
}

Function Do-Purge {
  @"
    delete noprompt expired backup;
    delete noprompt obsolete device type disk;
    delete noprompt expired archivelog all;  
"@ | rman target / log=$output append
}

Function Take-Full {
  [CmdletBinding()] param ( [string] $dest, [string] $tag, [int] $level )
  @"
    run {
      allocate channel t1 type disk;
      
      backup as compressed backupset incremental level $level
        format '$dest\DB${level}_%d_%u_%s_%p' tag=DB${level}_${tag} database;
      backup as compressed backupset 
        format '$dest\ARC_%d_%u_%s_%p' tag=ARC_$tag
        filesperset 100 (archivelog all not backed up 1 times);
      
      delete noprompt archivelog all backed up 1 times to disk;
      
      release channel t1;
    }  
"@ | rman target / log=$output append
}

Function Take-Archive {
  [CmdletBinding()] param ( [string] $dest, [string] $tag )
  @"
    run {
      allocate channel t1 type disk;
      
      backup as compressed backupset 
        format '$dest\ARC_%d_%u_%s_%p' tag=ARC_$tag
        filesperset 100 (archivelog all not backed up 1 times);
      
      delete noprompt archivelog all backed up 1 times to disk;
      
      release channel t1;
    }  
"@ | rman target / log=$output append
}

Function Take-Spfile {
  [CmdletBinding()] param ( [string] $dest, [string] $tag )
  @"
    run {
      allocate channel t1 type disk;      
      backup spfile;      
      release channel t1;
    }  
"@ | rman target / log=$output append  
}

# Perform backup and purge
Configure-Rman -Dest $rman_dest -Retention $rman_retention
Do-Crosscheck

Switch ($mode) {
  "FULL" { Take-Full -Dest $rman_dest -Tag $rman_tag -Level $level }
  "ARCH" { Take-Archive -Dest $rman_dest -Tag $rman_tag }
}

Do-Purge
#!/bin/bash

unset ORACLE_SID
export NLS_DATE_FORMAT="YYYY/MM/DD HH24:MI:SS"

## Variables
VERSION=1.0
CURDIR=$(dirname $0)
SCRIPT=$(basename $0 .sh)
OUTPUT=$CURDIR/logs/${SCRIPT}_$(date '+%Y%m%d_%H%M').log
RMAN_DEST=/var/dump/oracle
RMAN_RETENTION="REDUNDANCY 1"
RMAN_MODE="ARCH"
RMAN_LEVEL=0
RMAN_TAG=$(date '+%Y%m%d')

## Functions
function script_help {
  echo "Usage: $SCRIPT -n SID -m MODE"
  echo "  -n Database name"
  echo "  -m Backup mode : ARCH (default), FULL"
  echo "  -l Level of incremental backup : 0 (default), 1"
  exit
}
[ $# -eq 0 ] && script_help;

function configure_rman {
  rman target / nocatalog << EOF | tee -a $OUTPUT 2>&1
    configure retention policy to ${RMAN_RETENTION} ;
    configure default device type to disk;
    configure controlfile autobackup on ;
    configure controlfile autobackup format for device type disk to '${RMAN_DEST}/CTL_%F';
    configure device type disk parallelism 1 backup type to compressed backupset;
EOF
}

function do_crosscheck {
  rman target / nocatalog << EOF | tee -a $OUTPUT 2>&1
    run {
      allocate channel t1 type disk;
      report schema;
      show all;
      
      crosscheck backup;
      crosscheck datafilecopy all;
      crosscheck archivelog all;
	  release channel t1;
    }
EOF
}

function do_purge {
  rman target / nocatalog << EOF | tee -a $OUTPUT 2>&1
    delete noprompt expired backup;
    delete noprompt obsolete device type disk;
    delete noprompt expired archivelog all;
EOF
}

function take_full {
  rman target / nocatalog << EOF | tee -a $OUTPUT 2>&1
    run {
      allocate channel t1 type disk;
      
      backup as compressed backupset incremental level ${RMAN_LEVEL} 
        format '${RMAN_DEST}/DB${RMAN_LEVEL}_%d_%u_%s_%p' tag=DB${RMAN_LEVEL}_${RMAN_TAG} database;
      backup as compressed backupset 
        format '${RMAN_DEST}/ARC_%d_%u_%s_%p' tag=ARC_${RMAN_TAG}
        filesperset 10 (archivelog all not backed up 1 times);
      
      delete noprompt archivelog all backed up 1 times to disk;
      
      release channel t1;
    }
EOF
}

function take_archive {
  rman target / nocatalog << EOF | tee -a $OUTPUT 2>&1
    run {
      allocate channel t1 type disk;
      
      backup as compressed backupset 
        format '${RMAN_DEST}/ARC_%d_%u_%s_%p' tag=ARC_${RMAN_TAG}
        filesperset 10 (archivelog all not backed up 1 times);
      
      delete noprompt archivelog all backed up 1 times to disk;
      
      release channel t1;
    }
EOF
}

## Main program
while getopts :n:m:l: opt; do
  case $opt in
    n)
	  DBNAME=$OPTARG
	  ;;
	m)
	  RMAN_MODE=$OPTARG
	  ;;
	l)
	  RMAN_LEVEL=$OPTARG
	  ;;
	\?)
	  script_help
	  ;;
  esac
done

[ -z "$DBNAME" ] && script_help
. oraenv <<< $DBNAME

## Perform backup and purge
configure_rman
do_crosscheck
[ "$RMAN_MODE" = "ARCH" ] && take_archive
[ "$RMAN_MODE" = "FULL" ] && take_full
do_purge

以上是关于sh 具有ARCH或FULL选项的常规备份脚本的主要内容,如果未能解决你的问题,请参考以下文章

Oracle数据库RMAN的自动备份脚本简介

一周RMAN备份脚本

innobackup增量备份脚本

XtraBackup自动恢复脚本

备份恢复脚本

sh 最终的Arch Linux安装脚本