Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- usage() {
- cat <<EOF
- $0 [-0]|[-1] database
- Main options:
- -0 - incremental level 0
- -1 - incremental level 1
- Additional options - most are controlled automatically:
- database
- --------------------------------------------------------
- purpose: Do daily full backups.
- Delete any files in backup directory more than 5 days old.
- Back up data files, archived log files, and control file.
- Rman should delete backup files more than 2 days old.
- ** This script is not RAC-aware. **
- input: database name (case insensitive), archivelog location
- output: messages to stdout
- example: $0 -0 bib_prod
- EOF
- echo "$*"
- }
- # Save args for logfile
- args="$*"
- #. ~/.oracle_env.sh
- export ORACLE_HOME=/u/app/oracle/product/12.1.0/dbhome_1
- export NLS_DATE_FORMAT='YYYY.MM.DD HH24:MI:SS'
- BACKUPROOT=/db_other/bkp
- MAX_DAYS=21
- _MAIL=/usr/local/bin/msmtp
- _MAIL_OPTS="--host=172.25.1.1 -f noreplay@baikalinvestbank.ru IT-sso@baikalinvestbank.ru"
- channel_options=" maxpiecesize 32G "
- crosscheck="crosscheck archivelog all device type disk;crosscheck backup device type disk;"
- delete="delete all input"
- delete1="delete noprompt obsolete device type disk;"
- #
- # These 2 lists are used for e-mail notification
- #
- # List of detected errors.
- errors=""
- # List of detected warnings
- warnings=""
- #
- # Functions to process errors / warnings and send reports.
- #
- pt_error() {
- errors="$*"
- pt_info "***ERROR: $*"
- }
- pt_warning() {
- warnings="$*"
- pt_info "***WARNING: $*"
- }
- pt_info() {
- if [[ "$LOGFILE" == "" ]]
- then
- echo "$*"
- else
- echo "$*" | tee -a $LOGFILE
- fi
- }
- finish() {
- ko=0
- email="$REPORT_MAIL"
- subject="***BACKUP OK $DB_NAME `hostname` sz=${SIZE}M fr=${FREE}% $args"
- if [[ "$warnings" != "" ]]
- then
- email="$ERROR_MAIL $REPORT_MAIL"
- subject="***BACKUP WARNINGS $DB_NAME `hostname` $args sz=${SIZE}M fr=${FREE}% - $warnings"
- ko=4
- fi
- if [[ "$errors" != "" ]]
- then
- email="$ERROR_MAIL $REPORT_MAIL"
- subject="***BACKUP FAILED $DB_NAME `hostname` $args - $errors"
- ko=3
- fi
- cd $BACKUPROOT/logs
- gzip -9 $LOGFILEBASEN
- $_MAIL $_MAIL_OPTS <<EOF
- From: noreplay@baikalinvestbank.ru
- To: IT-sso@baikalinvestbank.ru
- Subject: $subject
- $(cat $REPORTS)
- $(echo -n -e "\n------=============================================------\n")
- $(cat $STATUSFILE)
- $(uuencode $LOGFILEBASEN.gz $LOGFILEBASEN.gz)
- EOF
- echo $subject
- find $BACKUPROOT -empty -type d -delete
- find /db_other/bkp/logs -mtime +7 -delete
- echo -n -e "--- ====== BACKUP DONE ====== ---\n `date` \n------==========================------\n"
- rsync -avz --delete /db_other/bkp/ /mnt/db_other_52/bkp/
- echo -n -e "--- ====== RSYNC COPY DONE ====== ---\n `date` \n------==========================------\n"
- ssh oracle@db2 "/home/oracle/ora_bkp_scripts/usd_recover_bib_imp.sh $BACKUPROOT/$day" > /home/oracle/ora_bkp_scripts/recover_bib_imp.log 2>&1
- echo -n -e "--- ====== REMOTE DB RESTORE on db2 DONE ====== ---\n `date` \n------==========================------\n"
- #ssh oracle@db2 "mount /mnt/nas-t;rsync -avz --delete /db_other/bkp/ /mnt/nas-t/oracle/db1/db_other/bkp/;umount /mnt/nas-t" > /home/oracle/ora_bkp_scripts/smb_rsync.log 2>&1
- ssh oracle@db2 "mount /mnt/nas-t;rsync -avz /db_other/bkp/ /mnt/nas-t/oracle/db1/db_other/bkp/;find /mnt/nas-t/oracle/db1/db_other/bkp -mtime +7 -delete;umount /mnt/nas-t" > /home/oracle/ora_bkp_scripts/smb_rsync.log 2>&1
- echo -n -e "--- ====== RSYNC SAMBA COPY DONE ====== ---\n `date` \n------==========================------\n"
- #ssh oracle@db2 "rsync -avz --delete /db_other/bkp/ /mnt/db-ang/" > /home/oracle/ora_bkp_scripts/angarsk_rsync.log 2>&1
- #echo -n -e "--- ====== RSYNC ANGARSK COPY DONE ====== ---\n `date` \n------==========================------\n"
- zip -r /u/app/oracle/product/12.1.0/dbhome_1.zip /u/app/oracle/product/12.1.0/dbhome_1
- #rsync -avz --delete --exclude 'lost+found' /u/ /mnt/db_other_52/u/
- exit $ko
- }
- if [ "$#" -lt 2 ]; then
- usage "Too few rguments."; exit 1
- fi
- if [ "$#" -gt 2 ]; then
- usage "Too many rguments."; exit 1
- fi
- while [[ "$1" != "" ]]
- do
- case "$1" in
- -0) inc=0; incremental="incremental level 0"; shift;;
- -1) inc=1; incremental="incremental level 1"; shift;;
- -*) usage "Bad argument: $1" ; exit 1 ;;
- *)
- db_name=$1
- export ORACLE_SID=$1
- shift
- esac
- done
- if [[ "$db_name" = "" ]]
- then
- usage "DB name not specified"; exit 1
- fi
- #----------- end argument analyze
- # 4. Convert DB name - calculate lower and upper names
- db_name=`echo $db_name | tr '[:upper:]' '[:lower:]'`
- DB_NAME=`echo $db_name | tr '[:lower:]' '[:upper:]'`
- #
- if [[ "$day" == "" ]]
- then
- day=`date +%F`
- fi
- BACKUPDIR="$BACKUPROOT/$day/DB";
- BACKUPLOGDIR="$BACKUPROOT/$day/ARCH"
- if [[ $inc == 0 ]]
- then
- TAG=weekl0_`date +%Y%m%d%H%M`
- else
- TAG=daily1_`date +%Y%m%d%H%M`
- fi
- #
- LOGFILE=$BACKUPROOT/logs/${DB_NAME}_`hostname`_`date +%Y%m%d-%H%M`.log
- LOGFILEBASEN=`basename $LOGFILE`
- REPORTS=$BACKUPROOT/logs/${DB_NAME}_`hostname`_`date +%Y%m%d-%H%M`.rep
- STATUSFILE=$BACKUPROOT/logs/${DB_NAME}_`hostname`_`date +%Y%m%d-%H%M`.status
- #
- mkdir -p $BACKUPDIR $BACKUPLOGDIR $BACKUPROOT/logs
- if [[ ! -d $BACKUPDIR || ! -d $BACKUPLOGDIR ]]; then
- pt_error "*** mkdir error creating $BACKUPDIR or $BACKUPLOGDIR"
- rm $LOCKFILE
- finish
- fi
- #
- # Set up lock.
- #
- LOCKFILE=/var/tmp/backup_${DB_NAME}.pid
- if test -f $LOCKFILE
- then
- pt_error "*** Backup is already running: pid `cat $LOCKFILE`"
- finish
- fi
- echo $$ > $LOCKFILE || exit 2
- if [[ "$MAX_DAYS" -gt 0 ]]
- then
- find $BACKUPROOT -mtime +$MAX_DAYS -type f -name "*.obk" -print | xargs rm -f
- find $BACKUPROOT -mtime +$MAX_DAYS -type f -name "*.abk" -print | xargs rm -f
- find $BACKUPROOT -mtime +$MAX_DAYS -type d -print | xargs rmdir --ignore-fail-on-non-empty
- fi
- cat > /tmp/rman1.$$ << EOSQL
- connect TARGET;
- set controlfile autobackup format for device type disk to '$BACKUPDIR/${db_name}_%F.ctl';
- configure controlfile autobackup on;
- run {
- allocate channel d1 type disk $channel_options format '$BACKUPDIR/${db_name}_%U.obk';
- backup $incremental as compressed backupset tag '${TAG}_Data' database include current controlfile skip inaccessible;
- release channel d1;
- $delete1
- }
- SQL 'ALTER SYSTEM ARCHIVE LOG CURRENT';
- run {
- set backup copies 1;
- allocate channel d1 type disk $channel_options format '$BACKUPLOGDIR/${db_name}_%U.abk';
- backup as compressed backupset tag '${TAG}_Logs' archivelog all not backed up $delete skip inaccessible;
- release channel d1;
- $crosscheck
- $delete1
- }
- quit;
- EOSQL
- echo Running "$ORACLE_HOME/bin/rman $cat < /tmp/rman1.$$"
- cat /tmp/rman1.$$
- $ORACLE_HOME/bin/rman MSGNO $cat < /tmp/rman1.$$ 2>& 1 | tee -a $LOGFILE
- RC=$?
- if [ $RC -ne 0 ]; then
- pt_error RMAN error for $DB_NAME $ORACLE_SID
- else
- if grep RMAN-08540 $LOGFILE && ! grep RMAN-00558 $LOGFILE > /dev/null
- then
- pt_info RMAN created complete backup set
- echo OK > $STATUSFILE
- else
- pt_warning RMAN did not created complete backup set
- fi
- fi
- if grep failure $LOGFILE > /dev/null
- then
- pt_warning "rman completed with errors `grep failure $LOGFILE | head -1`"
- fi
- #
- # Comment it if we need scripts for debug purpose.
- rm -f /tmp/*.$$
- #Unlock this process
- rm $LOCKFILE
- #
- # Now running reports
- #
- echo "======================= SUMMARY REPORT =======================================================" > $REPORTS
- du -s $BACKUPROOT/* >> $REPORTS
- du -sk $BACKUPROOT/$day
- ((SIZE=`du -sk $BACKUPROOT/$day | awk '{print $1}'`/1000))
- FREE=`df -P $BACKUPROOT/. | sed -n 's/.* \([^ ]*\)%.*/\1/p'`
- df -k $BACKUPROOT/. $ARCH_FS >> $REPORTS
- $ORACLE_HOME/bin/rman $cat <<EOF 2>& 1 | tee -a $REPORTS
- connect target;
- list backup summary;
- quit;
- EOF
- echo "======================= DETAILED LOGS =======================================================" >> $REPORTS
- finish
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement