####------------------------------------------------------------------------------####
## shell db_incremental_database_backup.sh
## author zongziheng
## date 2017.01.20
## func the path and date are set as required
## use meth ./rmanbackup_lev_sjz.sh backup_dest oracle_home oracle_sid
## alter system set db_create_file_dest = '/u01/app/oracle/oradata/';
## alter database enable block change tracking;
## alter database enable block change tracking using file '/../../blcok...';
#!/bin/bash
export lang=en_us
export nls_lang=american_america.al32utf8
backtime=`date +"20%y%m%d%h%m%s"`
rman_backup_data_dest=$1
rman_log_file=${rman_backup_data_dest}/rmanback_lev_${backtime}.log
backup_date=`date +%d`
today=`date`
current_user=`id|cut -d "(" -f2|cut -d ")" -f1`
echo "-----------------$today-------------------">$rman_log_file
export oracle_home=$2
export rman=${oracle_home}/bin/rman
export oracle_sid=$3
export oracle_user=oracle
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
#touch $rman_log_file
chmod 666 $rman_log_file
current_week_daily=`date +%a`
case "$current_week_daily" in
"mon")
bak_level=1
;;
"tue")
"wed")
"thu")
"fri")
"sat")
bak_level=0
"sun")
"*")
bak_level=error
esac
export bak_level=$bak_level
echo "today is : $current_week_daily incremental level= $bak_level">>$rman_log_file
run_shell="
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/oracle 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='"$rman_backup_data_dest"/"$oracle_sid"_lev_"$bak_level"_%u_%t' tag='"$oracle_sid"_lev_"$bak_level"';
sql 'alter system archive log current';
backup archivelog all tag='arc_bak' format='"$rman_backup_data_dest"/arch_%u_%t' skip inaccessible filesperset 5 not backed up 1 times delete input;
backup current controlfile tag='ctlfile_bak' format='"$rman_backup_data_dest"/ctl_file_%u_%t';
backup spfile tag='spfile_bak' format='"$rman_backup_data_dest"/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_shell" >> $rman_log_file
su - $oracle_user -c "$run_shell" >> $rman_log_file
status=$?
else
echo "user command string: $run_shell" >> $rman_log_file
/bin/sh -c "$run_shell" >> $rman_log_file
fi
# ---------------------------------------------------------------------------
# record whether or not the log is completed.
if [ "$status" = "0" ]
logmsg="ended successfully."
logmsg="ended in error!"
echo >> $rman_log_file
echo script $0 >> $rman_log_file
echo ==== $logmsg on `date` ==== >> $rman_log_file
exit $status