现在的位置: 首页 > 综合 > 正文

linux rman备份脚本

2013年04月26日 ⁄ 综合 ⁄ 共 16734字 ⁄ 字号 评论关闭

nocatalog下全备
SKIP 选项
Excludes datafiles or archived redo logs from the backup set according to the criteria specified by the following keywords.
Note: You can also specify this option in the backupSpec clause.

OFFLINE
Specifies that offline datafiles should be excluded from the backup set.

READONLY
Specifies that read-only datafiles should be excluded from the backup set.

INACCESSIBLE
Specifies that datafiles or archived redo logs that cannot be read due to I/O errors should be excluded from the backup set.

A datafile is only considered inaccessible if it cannot be read. Some offline datafiles can still be read because they still exist on disk. Others have been deleted or moved and

so cannot be read, making them inaccessible.

FILESPERSET = integer
Specifies the maximum number of input files in each backup set. If you set FILESPERSET = n, then RMAN never includes more than n files in a backup set. The default for

FILESPERSET is the lesser of these two values: 64, number of input files divided by the number of channels. For example, if you back up 100 datafiles by using two channels, RMAN sets FILESPERSET to 50.

RMAN always attempts to create enough backup sets so that all allocated channels have work to do. An exception to the rule occurs when there are more channels than files to back up. For example, if RMAN backs up two datafiles when three channels are allocated
and FILESPERSET = 1, then one channel is necessarily idle.

示例:
平均文件数指:文件数/通道数。
allocate channel 提供备份并发度,若平均文件数<filesperset则会按照 平均文件数/备份集 进行备份,若超过则按照filesperset的数量生成备份集;例如:
1、run {
allocate channel ch1 type disk;
allocate channel ch2 type disk;
backup datafile 3,4,5,6 filesperset 3;
release channel ch1;
release channel ch2;
}
平均数是 4(文件数)/2(channel数) = 2 ,小于filesperset 3,则生成2个备份集,每个备份集包含2个数据文件

2、run {
allocate channel ch1 type disk;
allocate channel ch2 type disk;
backup datafile 3,4,5,6 filesperset 1;
release channel ch1;
release channel ch2;
}
则生成4个备份集,每个包含一个数据文件

 

rman nocatalog target sys/oracle msglog rmanLog.out append  --将命令执行的日志追加到rmanLog.out文件,如果没有append,则会覆盖已有的rmanLog.out文件。

备份脚本:

#########################################################################
##   t_database_backup.sh      ##
##   created by Tianlesoftware   ##
##        2010-7-16                 ##
#########################################################################
#!/bin/sh
# ---------------------------------------------------------------------------
# Determine the user which is executing this script.
# ---------------------------------------------------------------------------
CUSER=`id |cut -d"(" -f2 | cut -d ")" -f1`

# ---------------------------------------------------------------------------
# Put output in <this file name>.out. Change as desired.
# Note: output directory requires write permission.
# ---------------------------------------------------------------------------
RMAN_LOG_FILE=${0}.out
# ---------------------------------------------------------------------------
# You may want to delete the output file so that backup information does
# not accumulate.  If not, delete the following lines.
# ---------------------------------------------------------------------------
if [ -f "$RMAN_LOG_FILE" ]
then
rm -f "$RMAN_LOG_FILE"
fi
# -----------------------------------------------------------------
# Initialize the log file.
# -----------------------------------------------------------------
echo >> $RMAN_LOG_FILE
chmod 666 $RMAN_LOG_FILE
# ---------------------------------------------------------------------------
# Log the start of this script.
# ---------------------------------------------------------------------------
echo Script $0 >> $RMAN_LOG_FILE
echo ==== started on `date` ==== >> $RMAN_LOG_FILE
echo >> $RMAN_LOG_FILE
# ---------------------------------------------------------------------------
# Oracle home path.
# ---------------------------------------------------------------------------
ORACLE_HOME=/home/oracle/product/10.2.0/db_1
export ORACLE_HOME
# ---------------------------------------------------------------------------
# the Oracle SID of the target database.
# ---------------------------------------------------------------------------
ORACLE_SID=oralife
export ORACLE_SID
# ---------------------------------------------------------------------------
# The Oracle DBA user id (account).
# ---------------------------------------------------------------------------
ORACLE_USER=oracle
export ORACLE_USER
# ---------------------------------------------------------------------------
# Set the Oracle Recovery Manager name.
# ---------------------------------------------------------------------------
RMAN=$ORACLE_HOME/bin/rman
# ---------------------------------------------------------------------------
# Print out the value of the variables set by this script.
# ---------------------------------------------------------------------------
echo >> $RMAN_LOG_FILE
echo   "RMAN: $RMAN" >> $RMAN_LOG_FILE
echo   "ORACLE_SID: $ORACLE_SID" >> $RMAN_LOG_FILE
echo   "ORACLE_USER: $ORACLE_USER" >> $RMAN_LOG_FILE
echo   "ORACLE_HOME: $ORACLE_HOME" >> $RMAN_LOG_FILE
# ---------------------------------------------------------------------------
# Print out the value of the variables set by bphdb.
# ---------------------------------------------------------------------------
#echo  >> $RMAN_LOG_FILE
#echo   "NB_ORA_FULL: $NB_ORA_FULL" >> $RMAN_LOG_FILE
#echo   "NB_ORA_INCR: $NB_ORA_INCR" >> $RMAN_LOG_FILE
#echo   "NB_ORA_CINC: $NB_ORA_CINC" >> $RMAN_LOG_FILE
#echo   "NB_ORA_SERV: $NB_ORA_SERV" >> $RMAN_LOG_FILE
#echo   "NB_ORA_POLICY: $NB_ORA_POLICY" >> $RMAN_LOG_FILE
# ---------------------------------------------------------------------------
# NOTE: This script assumes that the database is properly opened. If desired,
# this would be the place to verify that.
# ---------------------------------------------------------------------------
echo >> $RMAN_LOG_FILE
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Call Recovery Manager to initiate the backup.
# ---------------------------------------------------------------------------

CMD_STR="
ORACLE_HOME=$ORACLE_HOME
export ORACLE_HOME
ORACLE_SID=$ORACLE_SID
export ORACLE_SID
$RMAN nocatalog target sys/admin  msglog $RMAN_LOG_FILE append << EOF
RUN {
allocate channel c1 type disk;
allocate channel c2 type disk;

BACKUP FORMAT '/home/oracle/backup/oralife_%U_%T' skip inaccessible filesperset 5  DATABASE TAG oralife_hot_db_bk;

sql 'alter system archive log current';

BACKUP FORMAT '/home/oracle/backup/arch_%U_%T' skip inaccessible filesperset 5 ARCHIVELOG ALL DELETE INPUT;

backup current controlfile tag='bak_ctlfile' format='/home/oracle/backup/ctl_file_%U_%T';
backup spfile tag='spfile' format='/home/oracle/backup/oralife_spfile_%U_%T';

release channel c2;
release channel c1;
}
report obsolete;
delete noprompt obsolete;
crosscheck backup;
delete noprompt expired backup;
list backup summary;
#EOF
"
# Initiate the command string

if [ "$CUSER" = "root" ]
then
    echo "Root Command String: $CMD_STR" >> $RMAN_LOG_FILE
    su - $ORACLE_USER -c "$CMD_STR" >> $RMAN_LOG_FILE
    RSTAT=$?
else
    echo "User Command String: $CMD_STR" >> $RMAN_LOG_FILE
    /bin/sh -c "$CMD_STR" >> $RMAN_LOG_FILE
    RSTAT=$?
fi

# ---------------------------------------------------------------------------
# Log the completion of this script.
# ---------------------------------------------------------------------------
if [ "$RSTAT" = "0" ]
then
    LOGMSG="ended successfully"
else
    LOGMSG="ended in error"
fi

echo >> $RMAN_LOG_FILE
echo Script $0 >> $RMAN_LOG_FILE
echo ==== $LOGMSG on `date` ==== >> $RMAN_LOG_FILE
echo >> $RMAN_LOG_FILE

/bin/mailx -s "RMAN Backup SID " myEmail@xx.com < $RMAN_LOG_FILE

exit $RSTAT

当设置控制文件自动备份时,手动备份的控制文件,与spfile被标识为obsolete而被删除。
备份控制文件与spfile输出片段:

Starting backup at 2011-11-13 17:18:43
channel c1: starting full datafile backupset
channel c1: specifying datafile(s) in backupset
including current control file in backupset
channel c1: starting piece 1 at 2011-11-13 17:18:44
channel c1: finished piece 1 at 2011-11-13 17:18:45
piece handle=/home/oracle/backup/ctl_file_22mrim3j_1_1_20111113 tag=BAK_CTLFILE comment=NONE
channel c1: backup set complete, elapsed time: 00:00:02
Finished backup at 2011-11-13 17:18:45

Starting backup at 2011-11-13 17:18:45
channel c1: starting full datafile backupset
channel c1: specifying datafile(s) in backupset
including current SPFILE in backupset
channel c1: starting piece 1 at 2011-11-13 17:18:47
channel c1: finished piece 1 at 2011-11-13 17:18:48
piece handle=/home/oracle/backup/oralife_spfile_23mrim3m_1_1_20111113 tag=SPFILE comment=NONE
channel c1: backup set complete, elapsed time: 00:00:02
Finished backup at 2011-11-13 17:18:48

删除obsolete备份信息输出片段:

RMAN retention policy will be applied to the command
RMAN retention policy is set to redundancy 1
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=133 devtype=DISK
Deleting the following obsolete backups and copies:
Type                 Key    Completion Time    Filename/Handle
-------------------- ------ ------------------ --------------------
Backup Set           45     2011-11-13 17:11:30
  Backup Piece       45     2011-11-13 17:11:30 /home/oracle/backup/oralife_1lmrilkl_1_1_20111113
Backup Set           46     2011-11-13 17:12:00
  Backup Piece       46     2011-11-13 17:12:00 /home/oracle/backup/oralife_1mmrilkl_1_1_20111113
Backup Set           49     2011-11-13 17:12:18
  Backup Piece       49     2011-11-13 17:12:18 /home/oracle/backup/arch_1pmrilnh_1_1_20111113
Backup Set           50     2011-11-13 17:12:18
  Backup Piece       50     2011-11-13 17:12:18 /home/oracle/backup/arch_1qmrilnh_1_1_20111113
Backup Set           51     2011-11-13 17:12:25
  Backup Piece       51     2011-11-13 17:12:25 /home/oracle/backup/ctl_file_1rmrilnn_1_1_20111113
Backup Set           52     2011-11-13 17:12:28
  Backup Piece       52     2011-11-13 17:12:28 /home/oracle/backup/oralife_spfile_1smrilnr_1_1_20111113
Backup Set           55     2011-11-13 17:18:25
  Backup Piece       55     2011-11-13 17:18:25 +DGROUP1/oralife/autobackup/2011_11_13/s_767121502.270.767121505
Backup Set           58     2011-11-13 17:18:45
  Backup Piece       58     2011-11-13 17:18:45 /home/oracle/backup/ctl_file_22mrim3j_1_1_20111113
Backup Set           59     2011-11-13 17:18:47
  Backup Piece       59     2011-11-13 17:18:47 /home/oracle/backup/oralife_spfile_23mrim3m_1_1_20111113
deleted backup piece
backup piece handle=+DGROUP1/oralife/autobackup/2011_11_13/s_767121502.270.767121505 recid=55 stamp=767121504
deleted backup piece
backup piece handle=/home/oracle/backup/ctl_file_22mrim3j_1_1_20111113 recid=58 stamp=767121524
deleted backup piece
backup piece handle=/home/oracle/backup/oralife_spfile_23mrim3m_1_1_20111113 recid=59 stamp=767121527
Deleted 3 objects

这跟保留策略设置为1有关,rman会把之前的备份设置为obsolete,可以将保留策略设置为>1。

nocatalog下增量备份

notBackedUpSpec
Syntax Element    Description
NOT BACKED UP    Backs up only those files (of the files specified on the command) that RMAN has never backed up. This option is a convenient way to back up new files after adding them to the database.
SINCE TIME = 'date_string'    Specifies the date after which RMAN should back up files that have no backups. The date_string is either a date in the current NLS_DATE_FORMAT, or a SQL date expression such as 'SYSDATE-1'. When
calculating the number of backups for a file, RMAN only considers backups created on the same device type as the current backup.
This option is a convenient way to back up files that were not backed up during a previous failed backup. For example, you back up the database, but the instance fails halfway through. You can restart the backup with the NOT BACKED UP SINCE TIME clause and
avoid backing up those files that you already backed up. If AS BACKUPSET is set, then this feature is only useful if RMAN generates multiple backup sets during the backup.

When determining whether a file has been backed up, the SINCE date is compared with the completion time of the most recent backup. For BACKUP AS BACKUPSET, the completion time for a file in a backup set is the completion time of the entire backup set. In other
words, all files in the same backup set have the same completion time.

integer TIMES    Backs up only those archived logs that have not been backed up at least integer times. To determine the number of backups for a file, RMAN only considers backups created on the same device type as the current
backup.
This option is a convenient way to back up archived logs on a specified media (for example, you want to keep at least three copies of each log on tape).

BACKUP ARCHIVELOG ALL not backed up 2 times FORMAT 'G:/oracle/product/10.2.0/backup/arch_%U_%T' ;
not backed up 2 times
如果某归档日志已经备份了2次,rman就不再备份此归档日志。测试示例:

--第一次全备归档日志
RMAN> BACKUP ARCHIVELOG ALL not backed up 2 times FORMAT 'G:/oracle/product/10.2.0/backup/arch_%U_%T' ;

启动 backup 于 15-11月-11
当前日志已存档
使用目标数据库控制文件替代恢复目录
分配的通道: ORA_DISK_1
通道 ORA_DISK_1: sid=158 devtype=DISK
通道 ORA_DISK_1: 正在启动存档日志备份集
通道 ORA_DISK_1: 正在指定备份集中的存档日志
输入存档日志线程 =1 序列 =57 记录 ID=4 时间戳=767297711
输入存档日志线程 =1 序列 =58 记录 ID=5 时间戳=767297724
输入存档日志线程 =1 序列 =59 记录 ID=6 时间戳=767297730
输入存档日志线程 =1 序列 =60 记录 ID=7 时间戳=767297731
输入存档日志线程 =1 序列 =61 记录 ID=8 时间戳=767297737
输入存档日志线程 =1 序列 =62 记录 ID=9 时间戳=767297772
通道 ORA_DISK_1: 正在启动段 1 于 15-11月-11
通道 ORA_DISK_1: 已完成段 1 于 15-11月-11
段句柄=G:\ORACLE\PRODUCT\10.2.0\BACKUP\ARCH_02MRO27F_1_1_20111115 标记=TAG20111115T181614 注释=NONE
通道 ORA_DISK_1: 备份集已完成, 经过时间:00:00:02
完成 backup 于 15-11月-11

--第二次全备归档日志
RMAN> BACKUP ARCHIVELOG ALL not backed up 2 times FORMAT 'G:/oracle/product/10.2.0/backup/arch_%U_%T' ;

启动 backup 于 15-11月-11
当前日志已存档
使用通道 ORA_DISK_1
通道 ORA_DISK_1: 正在启动存档日志备份集
通道 ORA_DISK_1: 正在指定备份集中的存档日志
输入存档日志线程 =1 序列 =57 记录 ID=4 时间戳=767297711
输入存档日志线程 =1 序列 =58 记录 ID=5 时间戳=767297724
输入存档日志线程 =1 序列 =59 记录 ID=6 时间戳=767297730
输入存档日志线程 =1 序列 =60 记录 ID=7 时间戳=767297731
输入存档日志线程 =1 序列 =61 记录 ID=8 时间戳=767297737
输入存档日志线程 =1 序列 =62 记录 ID=9 时间戳=767297772
输入存档日志线程 =1 序列 =63 记录 ID=10 时间戳=767297851
通道 ORA_DISK_1: 正在启动段 1 于 15-11月-11
通道 ORA_DISK_1: 已完成段 1 于 15-11月-11
段句柄=G:\ORACLE\PRODUCT\10.2.0\BACKUP\ARCH_03MRO29S_1_1_20111115 标记=TAG20111115T181732 注释=NONE
通道 ORA_DISK_1: 备份集已完成, 经过时间:00:00:02
完成 backup 于 15-11月-11

--第三次全备归档日志,已备份过两次的归档日志,rman不再备份,只是忽略。
RMAN> BACKUP ARCHIVELOG ALL not backed up 2 times FORMAT 'G:/oracle/product/10.2.0/backup/arch_%U_%T' ;

启动 backup 于 15-11月-11
当前日志已存档
使用通道 ORA_DISK_1
正在略过存档日志文件 G:\ORACLE\PRODUCT\10.2.0\DB_1\FLASH_RECOVERY_AREA\ORA10G\ARCHIVELOG\2011_11_15\O1_MF_1_57_7D4GXH8Q_
.ARC; 已经备份 2 次
正在略过存档日志文件 G:\ORACLE\PRODUCT\10.2.0\DB_1\FLASH_RECOVERY_AREA\ORA10G\ARCHIVELOG\2011_11_15\O1_MF_1_58_7D4GXW5B_
.ARC; 已经备份 2 次
正在略过存档日志文件 G:\ORACLE\PRODUCT\10.2.0\DB_1\FLASH_RECOVERY_AREA\ORA10G\ARCHIVELOG\2011_11_15\O1_MF_1_59_7D4GY27O_
.ARC; 已经备份 2 次
正在略过存档日志文件 G:\ORACLE\PRODUCT\10.2.0\DB_1\FLASH_RECOVERY_AREA\ORA10G\ARCHIVELOG\2011_11_15\O1_MF_1_60_7D4GY33J_
.ARC; 已经备份 2 次
正在略过存档日志文件 G:\ORACLE\PRODUCT\10.2.0\DB_1\FLASH_RECOVERY_AREA\ORA10G\ARCHIVELOG\2011_11_15\O1_MF_1_61_7D4GY9D2_
.ARC; 已经备份 2 次
正在略过存档日志文件 G:\ORACLE\PRODUCT\10.2.0\DB_1\FLASH_RECOVERY_AREA\ORA10G\ARCHIVELOG\2011_11_15\O1_MF_1_62_7D4GZDHH_
.ARC; 已经备份 2 次
通道 ORA_DISK_1: 正在启动存档日志备份集
通道 ORA_DISK_1: 正在指定备份集中的存档日志
输入存档日志线程 =1 序列 =63 记录 ID=10 时间戳=767297851
输入存档日志线程 =1 序列 =64 记录 ID=11 时间戳=767297944
通道 ORA_DISK_1: 正在启动段 1 于 15-11月-11
通道 ORA_DISK_1: 已完成段 1 于 15-11月-11
段句柄=G:\ORACLE\PRODUCT\10.2.0\BACKUP\ARCH_04MRO2CO_1_1_20111115 标记=TAG20111115T181904 注释=NONE
通道 ORA_DISK_1: 备份集已完成, 经过时间:00:00:02
完成 backup 于 15-11月-11

delete input选项,删除刚刚备份的归档日志,而不管你这个归档日志有没有备份指定的次数。所以not backed up n times与delete input联合使用,应该没有意义。

在执行脚本之前,先修改几个参数值:
1. DB 参数:
修改控制文件的保存时间,从默认的7天改成14天
SQL> show parameter control
SQL> alter system set control_file_record_keep_time=14 scope=both;
 
2. RMAN 参数:
开启控制文件的自动备份,开启之后在数据库备份或者数据文件(比如添加数据文件)有修改的时候都会自动备份控制文件和spfile文件。
RMAN> CONFIGURE CONTROLFILE AUTOBACKUP ON;
RMAN> CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;

##################################################################
##   rman_incremental_backup.sh               ##
##    created by Tianlesoftware                 ##
##        2011-1-25                         ##
##################################################################
#!/bin/ksh
export LANG=en_US
BACKUP_DATE=`date +%d`
RMAN_LOG_FILE=${0}.out
TODAY=`date`
CUSER=`id|cut -d "(" -f2|cut -d ")" -f1`
echo "-----------------$TODAY-------------------">$RMAN_LOG_FILE
ORACLE_HOME=/home/oracle/product/10.2.0/db_1
export ORACLE_HOME
RMAN=$ORACLE_HOME/bin/rman
export RMAN
ORACLE_SID=oralife
export ORACLE_SID
ORACLE_USER=oracle
export ORACLE_USER
 
echo "ORACLE_SID: $ORACLE_SID">>$RMAN_LOG_FILE
echo "ORACLE_HOME:$ORACLE_HOME">>$RMAN_LOG_FILE
echo "ORACLE_USER:$ORACLE_USER">>$RMAN_LOG_FILE
echo "==========================================">>$RMAN_LOG_FILE
echo "BACKUP DATABASE BEGIN......">>$RMAN_LOG_FILE
echo "                   ">>$RMAN_LOG_FILE
chmod 666 $RMAN_LOG_FILE
 
WEEK_DAILY=`date +%a`
case  "$WEEK_DAILY" in
       "Mon")
            BAK_LEVEL=2
            ;;
       "Tue")
            BAK_LEVEL=2
            ;;
       "Wed")
            BAK_LEVEL=2
            ;;
       "Thu")
            BAK_LEVEL=1
            ;;
       "Fri")
            BAK_LEVEL=2
            ;;
       "Sat")
            BAK_LEVEL=2
            ;;
       "Sun")
            BAK_LEVEL=0
            ;;
       "*")
            BAK_LEVEL=error
esac
 
export BAK_LEVEL=$BAK_LEVEL
echo "Today is : $WEEK_DAILY  incremental level= $BAK_LEVEL">>$RMAN_LOG_FILE
 
RUN_STR="
BAK_LEVEL=$BAK_LEVEL
export BAK_LEVEL
ORACLE_HOME=$ORACLE_HOME
export ORACLE_HOME
ORACLE_SID=$ORACLE_SID
export ORACLE_SID
$RMAN nocatalog TARGET sys/admin msglog $RMAN_LOG_FILE append <<EOF
run
{
allocate channel c1 type disk;
allocate channel c2 type disk;
backup  incremental level= $BAK_LEVEL  skip inaccessible filesperset 5 Database 
format='/home/oracle/backup/oralife_lev"$BAK_LEVEL"_%U_%T'  tag='oralife_lev"$BAK_LEVEL"' ;
sql 'alter system archive log current';
backup archivelog all tag='arc_bak' format='/home/oracle/backup/arch_%U_%T' skip inaccessible  filesperset 5 delete input;
backup current controlfile tag='bak_ctlfile' format='/home/oracle/backup/ctl_file_%U_%T';
backup spfile tag='spfile' format='/home/oracle/backup/oralife_spfile_%U_%T';
release channel c2;
release channel c1;
}
report obsolete;
delete noprompt obsolete;
crosscheck backup;
delete noprompt expired backup;
list backup summary;

#EOF
"
 # Initiate the command string
 
if [ "$CUSER" = "root" ]
then
    echo "Root Command String: $RUN_STR" >> $RMAN_LOG_FILE    
    su - $ORACLE_USER -c "$RUN_STR" >> $RMAN_LOG_FILE
    RSTAT=$?
else
    echo "User Command String: $RUN_STR" >> $RMAN_LOG_FILE    
    /bin/sh -c "$RUN_STR" >> $RMAN_LOG_FILE
    RSTAT=$?
fi
 
# ---------------------------------------------------------------------------
# Log the completion of this script.
# ---------------------------------------------------------------------------
 
if [ "$RSTAT" = "0" ]
then
    LOGMSG="ended successfully"
else
    LOGMSG="ended in error"
fi
echo >> $RMAN_LOG_FILE
echo Script $0 >> $RMAN_LOG_FILE
echo ==== $LOGMSG on `date` ==== >> $RMAN_LOG_FILE
echo >> $RMAN_LOG_FILE
/bin/mailx -s "RMAN Backup SID " myEmail@xx.com < $RMAN_LOG_FILE
exit $RSTAT

还要使用linux find exec rm来删除无用的备份的归档日志。

参考:

http://blog.csdn.net/tianlesoftware/article/details/5740630

 

 

 

抱歉!评论已关闭.