1. 程式人生 > >RMAN刪除備份集_筆記

RMAN刪除備份集_筆記

1自動刪除
刪除歸檔日誌
delete input;  
report obsolete命令報告過期備份
RMAN> report obsolete;

delete obsolete刪除所有以前的備份(包括資料檔案、歸檔、控制檔案)
RMAN> delete obsolete;
RMAN> crosscheck archivelog all;
 
2手動刪除
手工刪除備份集,之後用如下命令同步
RMAN>crosscheck backup;
RMAN>delete expired backup;

編寫的oracle rman指令碼

 #!/bin/bash

    # Usage:

    # backup_rman_v1.1.sh [all|repeat|sync]

    # all: list all backup file.

    # repeat: repeat level-1 increment backup.

    # sync:    sync backup file to target database.

    export ORACLE_HOME=/u01/app/11.2.0

    export ORACLE_SID=health

    export LD_LIBRARY_PATH=${ORACLE_HOME}/lib:${ORACLE_HOME}/ctx/lib

    export PATH=$PATH:${ORACLE_HOME}/bin

    #export NLS_LANG="American_america.zhs16gbk"

    export NLS_LANG="American_america.al32utf8"

    export ORACLE_OWNER=oracle

    export backup_dir=/sda/backup

    export log=/sda/backup/log

    rsync_dir=$backup_dir #sync dir

    controlfile_dir=$backup_dir/controlfile

    username=oracle #target OS ,oracle user

    password=go2north #target oracle user password

    target_host=61.158.131.94

    today_backup=`date +'%Y-%m-%d'`

    ########set display color#########

    white=$(echo -e "\e[39;40m")

    green=$(echo -e "\e[36;40m")

    red=$(echo -e "\e[31;40m")

    purple=$(echo -e "\e[35;40m")

    yellow=$(echo -e "\e[33;40m")

    blue=$(echo -e "\e[34;40m")

    ########color set end ############

    # data backup status.

    # 0: backup failed.

    # 2: default

    # 9: success

    backup_status=2

    #database status check ,If it's not turn on,the value is 0,or else 1

    ora_stat=`ps -ef | grep -i 'ora_smon_*' |grep -v grep| wc -l`

    #database mode check,If it's archive mode,that value is 1,or else 0;

    arch=`ps -ef | grep -i 'ora_arc_*' | grep -v grep | wc -l`

    function open_database()

    {

    if [ "$ora_stat" = 0 ]; then

    cat 《 EOF | $ORACLE_HOME/bin/sqlplus '/as sysdba'

    shutdown immediate;

    startup;

    quit;

    EOF

    backup_status=2

    if [ "$?" = 1 ]; then

    echo "database unable strtup!"

    backup_status=0

    exit 1

    fi

    fi

    }

    function open_archive_mode()

    {

    if [ "$arch" = 0 ]; then #if arch=1,nothing,because it was already on archive mode

    echo "****************open archive mode*************"

    cat 《 EOF | $ORACLE_HOME/bin/sqlplus '/as sysdba'

    shutdown immediate;

    startup mount;

    alter database archivelog;

    alter database open;

    quit;

    EOF

    fi

    }

    function return_initaliztion_mode()

    {

    if [ "$arch" = 0 -a "$backup_status" > 0 ]; then

    #if arch=1,nothing,because initialization mode is archive mode

    echo "********* return initialization database mode**********"

    cat 《 EOF | $ORACLE_HOME/bin/sqlplus '/as sysdba'

    shutdown immediate;

    startup mount;

    alter database noarchivelog;

    alter database open;

    quit;

    EOF

    fi

    if [ "$?" = 0 ]; then

    echo "return initalization database successfully."

    fi

    echo "************return initialization database mode *********" ;

    }

    function increment_backup_level_1() # incremental level-1 backup

    {

    #open_database

    #open_archive_mode

    echo "******** `date +'%Y%m%d'` Do level-1 increment backup…*********************" 2>&1;

    cat 《 EOF | $ORACLE_HOME/bin/rman target / |tee $log/rman_increment_db_`date +'%y%m%d%H%M'`.log

    configure maxsetsize to 20g;

    configure controlfile autobackup on;

    configure controlfile autobackup format for device type disk to '$controlfile_dir/%F';

    run {

    allocate channel c01 type disk;

    backup incremental level 1 database format '$backup_dir/increment_db_%d_%s_%t_%p' tag="increment_db_`date +'%y%m%d%H%M'`";

    release channel c01;

    }

    configure controlfile autobackup off;

    crosscheck backup of database;

    crosscheck archivelog all;

    delete noprompt obsolete ;

    delete noprompt expired backup;

    delete noprompt backup completed before 'sysdate-30';

    delete noprompt archivelog until time 'sysdate-14';

    EOF

    if [ "$?" = 0 ];then

    echo "*******************level-1 backup completed!************************"

    backup_status=9

    else

    echo "*****************level-1 backup databae failed,please contact oracle dba*******"

    backup_status=0

    fi

    return $backup_status

    #return_initaliztion_mode

    }

    function level_0_backup_database()

    {

    #open_database

    #open_archive_mode

    echo "************* Do level-0 backup ****************"

    cat 《 EOF | $ORACLE_HOME/bin/rman target / |tee $log/rman_full_db_`date +'%y%m%d%H%M'`.log

    configure retention policy to redundancy 7;

    configure maxsetsize to 20g;

    configure controlfile autobackup on;

    configure controlfile autobackup format for device type disk to '$controlfile_dir/%F';

    crosscheck backup of database;

    crosscheck archivelog all;

    delete noprompt obsolete ;

    delete noprompt expired backup;

    delete noprompt backup completed before 'sysdate-7';

    delete noprompt archivelog until time 'sysdate-7';

    run {

    allocate channel c1 type disk;

    backup incremental level 0 database format '$backup_dir/full_db_%d_%s_%t_%p' tag="full_db_`date +'%y%m%d%H%M'`";

    release channel c1 ;

    }

    configure controlfile autobackup off;

    quit;

    EOF

    if [ "$?" = 0 ];then

    echo "*******************level-0 backup completed!************************"

    backup_status=9

    else

    echo "******************level-0 backup databae failed,please contact oracle dba*******"

    backup_status=0
 fi

    return $backup_status

    #return_initaliztion_mode

    }

    function repeat_increment_backup()

    {

    if [ "$#" = 0 ]; then

    exit 0

    else

    if [ "$1" = "repeat" ]; then

    echo "************do database increment backup again**************"

    increment_backup_level_1 $ORACLE_HOME $log $backup_dir

    echo "************repeat increment backup completed!**************"

    else

    echo "command error,please use parameter 'repeat'"

    exit 0

    fi

    fi

    }

    # sync target database backup files #

    function sync()

    {

    ping $target_host -c 1 > /dev/null # test network link #

    if [ $? != 0 ] ; then

    echo "sync host:$red $target_host $white link failed!,please check network."

    exit 1

    fi

    if [ -f /usr/bin/rsync ]; then

    #check resync command #

    cat 《 EOF > sync

    #!/usr/bin/expect

    expect "password:"

    send "$password\n";

    send "quit\n";

    interact

    EOF

    echo "********copy backup files to target database********"

    if [ -f sync -a -f /usr/bin/expect ]; then

    chmod +x sync

    ./sync

    rm -rf ./sync

    #list sync files

    backup_file=`ls -ltR --full-time $backup_dir/ | egrep -i "increment_|c-" | grep -i $today_backup | awk '{print $6 " " substr($7,1,8) " " $9}'`

    echo "sync files:"

    echo "$blue"

    j=0

    for i in $backup_file

    do

    ((j++))

    a[$j]=$i

    if [ $j = 3 ]; then

    echo "${a[`expr $j - 2`]} ${a[`expr $j - 1`]} ${a[$j]}"

    j=0

    fi

    done

    echo "$white"

    echo " transtion has succeed.please check the backup files on target database."

    exit 0

    else

    echo "command expect not found, please install Tcl/expect"

    exit 1

    fi

    else

    echo "command rsync not found,please install!"

    exit 1

    fi

    }

    if [ -f $log/autobak_`date +'%Y%m%d'`.log ]; then

    rm -rf $log/autobak_`date +'%Y%m%d'`.log

    fi

    (

    ##### backup level #####

    level_0_backup_status=`find $backup_dir/ -name 'full_db_*'| grep -i full_db |grep -v grep | wc -l` 2>&1

    level_1_backup_status=`ls -l --full-time $backup_dir/ |grep -i 'increment_db_*'| grep -i $today_backup|grep -v grep | wc -l` 2>&1

    #Always perform 0 backup

    level_0_backup_status=0

    if [ $level_0_backup_status = 0 -a $backup_status = 2 ]; then

    level_0_backup_database

    backup_status=$?

    fi

    if [ $level_1_backup_status = 0 -a $backup_status = 2 ]; then

    increment_backup_level_1

    backup_status=$?

    fi

    ##### backup level end ######

    # ############Today's database backup information##########

    # check today's backup status #

    check_backup=`ls -l --full-time $backup_dir/ | egrep -i "increment_db_|full_db_" | awk '{print $6}' | grep -i $today_backup | wc -l`

    # check today's controlfile backup information #

    control_file=`ls -lt --full-time $controlfile_dir/ | grep -i "c-*" | grep -i $today_backup | awk '{print $6 " " substr($7,1,8) " " $9}'`

    # check today's increment backup information #

    backup_file_info=`ls -lt --full-time $backup_dir/ | egrep -i "increment_db_|full_db_" | grep -i $today_backup | awk '{print $6 " " substr($7,1,8) " " $9}'`

    log_file_info=`ls -lt --full-time $log/ | egrep -i "increment_db_|full_db_" | grep -i $today_backup | awk '{print $6 " " substr($7,1,8) " " $9}'`

    if [ "$1" = "all" ] ; then

    backup_file_info=`ls -lt --full-time $backup_dir/ | egrep -i "increment_db_|full_db" | awk '{print $6 " " substr($7,1,8) " " $9}'`

    control_file=`ls -lt --full-time $controlfile_dir/ | grep -i "c-*"| awk '{print $6 " " substr($7,1,8) " " $9}'`

    fi

    # print today's backup information including controlfile and log information #

    if [ $check_backup -ge 0 ]; then

    if [ "$1" = "repeat" ] ; then

    repeat_increment_backup $1

    else

    echo " ############Today's database backup information########## "

    if [ "$1" = "all" ]; then

    today_backup=`ls -l --full-time $backup_dir/ | grep -i full_db_* | awk '{print $6}'`

    echo "List date $purple ${today_backup[0]} $white level-0 backup database after file information"

    else

    echo "Date $purple $today_backup $white database backup is completed."

    fi

    echo "backup file directory: $backup_dir"

    echo "backup file information: $green"

    echo ""

    j=0

    for i in $backup_file_info

    do

    ((j++))

    a[$j]=$i

    if [ $j = 3 ]; then

    echo "${a[`expr $j - 2`]} ${a[`expr $j - 1`]} $backup_dir/${a[$j]}"

    j=0

    fi

    done

    echo "$white"

    echo "Controlfile information:$yellow"

    echo ""

    j=0

    for p in $control_file;do

    ((j++))

    a[$j]=$p

    if [ $j = 3 ] ; then

    echo "${a[`expr $j - 2`]} ${a[`expr $j - 1`]} $controlfile_dir/${a[$j]}"

    j=0

    fi

    done

    echo "$white"

    echo "log information:$blue"

    echo ""

    j=0

    for p in $log_file_info;do

    ((j++))

    a[$j]=$p

    if [ $j = 3 ] ; then

    echo "${a[`expr $j - 2`]} ${a[`expr $j - 1`]} $log/${a[$j]}"

    j=0

    fi

    done

    echo "$white"

    echo "If you want increment backup database again,please use \"repeat\" parameter"

    echo " ############Today database backup information the end ########## "

    fi

    fi

    # end print backup information #

    # copy backup file #

    # if [ "$1" = "sync" ] ; then

    #    backup_status=9

    # fi

    # if [ "$backup_status" = 9 ]; then

    #    sync

    # else

    # echo "Today's Backup file is synced. please check whether it's in the target database."

    # echo "If you want to sync again,please use \"sync\" parameter."

    # exit 0

    # fi

    echo "If you want to view all backup information,Please use \"all\" parameter."

    ) | tee -a $log/autobak_`date +'%Y%m%d'`.log

    mail -s "`date +'%Y%m%d'` database backup information" $mail_to_admin < $log/autobak_`date +'%Y%m%d'`.log