Oracle备份脚本(RMAN/EXP)
环境:
OEL+Oracle 11.2.0.1
ORACLE_SID=orcl2
创建如下目录:
[root@DBA2 /]$ mkdir /backup
[root@DBA2 /]$ mkdir /backup/0
[root@DBA2 /]$ mkdir /backup/1
[root@DBA2 /]$ mkdir /backup/2
[root@DBA2 /]$ mkdir /backup/3
[root@DBA2 /]$ mkdir /backup/4
[root@DBA2 /]$ mkdir /backup/5
[root@DBA2 /]$ mkdir /backup/6
更改属主:
[root@DBA2 ~]# chown -R oracle:dba /backup/
脚本内容如下:
#########################################################
### backup.sh ###
#########################################################
##设置环境变量
export ORACLE_BASE=/opt/oracle
export ORACLE_HOME=$ORACLE_BASE/product/OraHome
export ORACLE_SID=orcl2
export ORACLE_OWNER=oracle
export ORACLE_TERM=vt100
export PATH=$PATH:$ORACLE_HOME/bin:$HOME/bin
export PATH=$ORACLE_HOME/bin:$ORACLE_HOME/Apache/Apache/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib:/usr/local/lib
export CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib
export CLASSPATH=$CLASSPATH:$ORACLE_HOME/network/jlib
export PATH=$PATH:/usr/sbin
export PATH=$PATH:/usr/bin
NLS_LANG=AMERICAN_AMERICA.WE8MSWIN1252
export NLS_LANG
NOW=`date +%w`
rq=`date +%Y%m%d%H%M%S`
RMAN=$ORACLE_HOME/bin/rman
TARGET_CONNECT_STR=/
RMAN_LOG_FILE=/backup/$NOW/$ORACLE_SID.backup_all_out.log
CUSER=`id |cut -d"(" -f2 | cut -d ")" -f1`
FILE_NAME=/backup/$NOW/$ORACLE_SID.$rq
if [ "$NOW" = "0" ]
then
BACKUP_TYPE="INCREMENTAL LEVEL 0"
else
BACKUP_TYPE="INCREMENTAL LEVEL 1"
fi
##逻辑备份
exp system/oracle direct=Y full=y file=$FILE_NAME.logic log=/backup/$NOW/orcl2_logic_backup.log
cat /backup/$NOW/orcl2_logic_backup.log >> $RMAN_LOG_FILE
rm /backup/$NOW/orcl2_logic_backup.log
##物理备份
CMD_STR="
ORACLE_HOME=$ORACLE_HOME
export ORACLE_HOME
ORACLE_SID=$ORACLE_SID
export ORACLE_SID
rq=$rq
export rq
$RMAN target $TARGET_CONNECT_STR msglog $RMAN_LOG_FILE append <<EOF
run{
allocate channel c1 type disk;
allocate channel c2 type disk;
backup as compressed backupset $BACKUP_TYPE format '$FILE_NAME.DATAFILE_%s_%t.bak' database;
sql 'ALTER SYSTEM ARCHIVE LOG CURRENT';
backup as compressed backupset format '$FILE_NAME.ARCHIVELOG_%s_%t.bak'
archivelog from time 'SYSDATE-3' until time 'SYSDATE' delete all input;
release channel c1;
release channel c2;
delete noprompt expired backup;
delete noprompt obsolete;
}
EOF
"
if [ "$CUSER" = "root" ]
then
su - $ORACLE_USER -c "$CMD_STR" >> $RMAN_LOG_FILE
RSTAT=$?
else
/usr/bin/ksh -c "$CMD_STR" >> $RMAN_LOG_FILE
RSTAT=$?
fi
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
exit $RSTAT