daily pastebin goal
85%
SHARE
TWEET

Untitled

a guest Oct 17th, 2017 450 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. usage() {
  4. cat <<EOF
  5.  $0 [-0]|[-1] database
  6.  Main options:
  7.  -0     - incremental level 0
  8.  -1     - incremental level 1
  9.  
  10.  Additional options - most are controlled automatically:
  11.  database
  12.  
  13. --------------------------------------------------------
  14.     purpose: Do daily full backups.
  15.              Delete any files in backup directory more than 5 days old.
  16.              Back up data files, archived log files, and control file.
  17.              Rman should delete backup files more than 2 days old.
  18.              ** This script is not RAC-aware. **
  19.       input: database name (case insensitive), archivelog location
  20.      output: messages to stdout
  21.  
  22.     example: $0 -0 bib_prod
  23.  
  24. EOF
  25. echo "$*"
  26. }
  27.  
  28. # Save args for logfile
  29. args="$*"
  30.  
  31. #. ~/.oracle_env.sh
  32.  
  33. export ORACLE_HOME=/u/app/oracle/product/12.1.0/dbhome_1
  34. export NLS_DATE_FORMAT='YYYY.MM.DD HH24:MI:SS'
  35. BACKUPROOT=/db_other/bkp
  36. MAX_DAYS=21
  37.  
  38. _MAIL=/usr/local/bin/msmtp
  39. _MAIL_OPTS="--host=172.25.1.1 -f noreplay@baikalinvestbank.ru IT-sso@baikalinvestbank.ru"
  40.  
  41. channel_options=" maxpiecesize 32G "
  42. crosscheck="crosscheck archivelog all device type disk;crosscheck backup device type disk;"
  43. delete="delete all input"
  44. delete1="delete noprompt obsolete device type disk;"
  45.  
  46. #
  47. # These 2 lists are used for e-mail notification
  48. #
  49. # List of detected errors.
  50. errors=""
  51. # List of detected warnings
  52. warnings=""
  53.  
  54. #
  55. # Functions to process errors / warnings and send reports.
  56. #
  57. pt_error() {
  58.     errors="$*"
  59.     pt_info "***ERROR: $*"
  60. }
  61.  
  62. pt_warning() {
  63.         warnings="$*"
  64.         pt_info "***WARNING: $*"
  65. }
  66.  
  67. pt_info() {
  68.         if [[ "$LOGFILE" == "" ]]
  69.         then
  70.                 echo "$*"
  71.         else
  72.                 echo "$*" | tee -a $LOGFILE
  73.         fi
  74. }
  75.  
  76. finish() {
  77.         ko=0
  78.         email="$REPORT_MAIL"
  79.         subject="***BACKUP OK $DB_NAME `hostname`  sz=${SIZE}M fr=${FREE}% $args"
  80.         if [[ "$warnings" != "" ]]
  81.         then
  82.                 email="$ERROR_MAIL $REPORT_MAIL"
  83.                 subject="***BACKUP WARNINGS $DB_NAME `hostname` $args sz=${SIZE}M fr=${FREE}% - $warnings"
  84.                 ko=4
  85.         fi
  86.         if [[ "$errors" != "" ]]
  87.         then
  88.                 email="$ERROR_MAIL $REPORT_MAIL"
  89.                 subject="***BACKUP FAILED $DB_NAME `hostname` $args - $errors"
  90.                 ko=3
  91.         fi
  92.         cd $BACKUPROOT/logs
  93.         gzip -9 $LOGFILEBASEN
  94.  
  95.         $_MAIL $_MAIL_OPTS <<EOF
  96. From: noreplay@baikalinvestbank.ru
  97. To: IT-sso@baikalinvestbank.ru
  98. Subject: $subject
  99.  
  100. $(cat $REPORTS)
  101.  
  102. $(echo -n -e "\n------=============================================------\n")
  103.  
  104. $(cat $STATUSFILE)
  105.  
  106. $(uuencode $LOGFILEBASEN.gz $LOGFILEBASEN.gz)
  107.  
  108. EOF
  109.  
  110.         echo $subject
  111.         find $BACKUPROOT -empty -type d -delete
  112.         find /db_other/bkp/logs -mtime +7 -delete
  113.     echo -n -e "--- ====== BACKUP DONE ====== ---\n `date` \n------==========================------\n"
  114.     rsync -avz --delete /db_other/bkp/ /mnt/db_other_52/bkp/
  115.     echo -n -e "--- ====== RSYNC COPY DONE ====== ---\n `date` \n------==========================------\n"
  116.     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
  117.     echo -n -e "--- ====== REMOTE DB RESTORE on db2 DONE ====== ---\n `date` \n------==========================------\n"
  118.     #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
  119.     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
  120.     echo -n -e "--- ====== RSYNC SAMBA COPY DONE ====== ---\n `date` \n------==========================------\n"
  121.     #ssh oracle@db2 "rsync -avz --delete /db_other/bkp/ /mnt/db-ang/" > /home/oracle/ora_bkp_scripts/angarsk_rsync.log 2>&1
  122.     #echo -n -e "--- ====== RSYNC ANGARSK COPY DONE ====== ---\n `date` \n------==========================------\n"
  123.     zip -r /u/app/oracle/product/12.1.0/dbhome_1.zip /u/app/oracle/product/12.1.0/dbhome_1
  124.     #rsync -avz --delete --exclude 'lost+found' /u/ /mnt/db_other_52/u/
  125.    
  126.         exit $ko
  127. }
  128.  
  129. if [ "$#" -lt 2 ]; then
  130.     usage "Too few rguments."; exit 1
  131. fi
  132.  
  133. if [ "$#" -gt 2 ]; then
  134.     usage "Too many rguments."; exit 1
  135. fi
  136.  
  137. while [[ "$1" != "" ]]
  138. do
  139. case "$1" in
  140. -0)             inc=0;  incremental="incremental level 0"; shift;;
  141. -1)             inc=1;  incremental="incremental level 1"; shift;;
  142. -*)        usage "Bad argument: $1" ; exit 1 ;;
  143. *)        
  144.            db_name=$1
  145.            export ORACLE_SID=$1
  146.            shift
  147. esac
  148. done
  149. if [[ "$db_name" = "" ]]
  150. then
  151.         usage "DB name not specified"; exit 1
  152. fi
  153.  
  154. #----------- end argument analyze
  155. # 4. Convert DB name - calculate lower and upper names
  156. db_name=`echo $db_name | tr '[:upper:]' '[:lower:]'`
  157. DB_NAME=`echo $db_name | tr '[:lower:]' '[:upper:]'`
  158. #
  159.  
  160. if [[ "$day" == "" ]]
  161. then
  162.         day=`date +%F`
  163. fi
  164. BACKUPDIR="$BACKUPROOT/$day/DB";
  165. BACKUPLOGDIR="$BACKUPROOT/$day/ARCH"
  166.  
  167. if [[ $inc == 0 ]]
  168. then
  169. TAG=weekl0_`date +%Y%m%d%H%M`
  170. else
  171. TAG=daily1_`date +%Y%m%d%H%M`
  172. fi
  173. #
  174. LOGFILE=$BACKUPROOT/logs/${DB_NAME}_`hostname`_`date +%Y%m%d-%H%M`.log
  175. LOGFILEBASEN=`basename $LOGFILE`
  176. REPORTS=$BACKUPROOT/logs/${DB_NAME}_`hostname`_`date +%Y%m%d-%H%M`.rep
  177. STATUSFILE=$BACKUPROOT/logs/${DB_NAME}_`hostname`_`date +%Y%m%d-%H%M`.status
  178. #
  179. mkdir -p $BACKUPDIR $BACKUPLOGDIR $BACKUPROOT/logs
  180. if [[ ! -d $BACKUPDIR || ! -d $BACKUPLOGDIR ]]; then
  181.   pt_error "*** mkdir error creating $BACKUPDIR or $BACKUPLOGDIR"
  182.   rm $LOCKFILE
  183.   finish
  184. fi
  185.  
  186. #
  187. # Set up lock.
  188. #  
  189. LOCKFILE=/var/tmp/backup_${DB_NAME}.pid
  190. if test -f $LOCKFILE
  191. then
  192.         pt_error "*** Backup is already running: pid `cat $LOCKFILE`"
  193.         finish
  194. fi
  195. echo $$ > $LOCKFILE || exit 2
  196.  
  197. if [[ "$MAX_DAYS" -gt 0 ]]
  198. then
  199.      find $BACKUPROOT -mtime +$MAX_DAYS -type f -name "*.obk" -print | xargs rm -f  
  200.      find $BACKUPROOT -mtime +$MAX_DAYS -type f -name "*.abk" -print | xargs rm -f
  201.      find $BACKUPROOT -mtime +$MAX_DAYS -type d -print | xargs rmdir --ignore-fail-on-non-empty  
  202.  
  203. fi
  204.  
  205. cat > /tmp/rman1.$$ << EOSQL
  206.   connect TARGET;
  207.   set controlfile autobackup format for device type disk to  '$BACKUPDIR/${db_name}_%F.ctl';
  208.   configure controlfile autobackup on;
  209.   run {
  210.     allocate channel d1 type disk  $channel_options  format '$BACKUPDIR/${db_name}_%U.obk';
  211.     backup  $incremental as compressed backupset tag '${TAG}_Data' database include current controlfile skip inaccessible;
  212.     release channel d1;
  213.     $delete1
  214.   }
  215.   SQL 'ALTER SYSTEM ARCHIVE LOG CURRENT';
  216.   run {
  217.         set backup copies 1;
  218.         allocate channel d1 type disk  $channel_options  format '$BACKUPLOGDIR/${db_name}_%U.abk';
  219.         backup as compressed backupset tag '${TAG}_Logs' archivelog all not backed up $delete skip inaccessible;
  220.         release channel d1;
  221.         $crosscheck
  222.         $delete1
  223.   }
  224.   quit;
  225. EOSQL
  226. echo Running "$ORACLE_HOME/bin/rman  $cat < /tmp/rman1.$$"
  227. cat /tmp/rman1.$$
  228. $ORACLE_HOME/bin/rman MSGNO $cat < /tmp/rman1.$$  2>& 1 | tee -a $LOGFILE
  229. RC=$?
  230. if [ $RC -ne 0 ]; then
  231.   pt_error RMAN error for $DB_NAME $ORACLE_SID
  232. else
  233.  if grep RMAN-08540 $LOGFILE && ! grep RMAN-00558 $LOGFILE > /dev/null
  234.  then
  235.         pt_info RMAN created complete backup set
  236.        echo OK > $STATUSFILE
  237.   else
  238.        pt_warning RMAN did not created complete backup set
  239.   fi
  240. fi
  241. if grep failure $LOGFILE > /dev/null
  242. then
  243.     pt_warning "rman completed with errors `grep failure $LOGFILE | head -1`"
  244. fi
  245.  
  246. #
  247. # Comment it if we need scripts for debug purpose.
  248. rm -f /tmp/*.$$
  249.  
  250. #Unlock this process
  251. rm $LOCKFILE
  252. #
  253. # Now running reports
  254. #
  255. echo "=======================  SUMMARY REPORT =======================================================" > $REPORTS
  256. du -s $BACKUPROOT/* >> $REPORTS
  257. du -sk $BACKUPROOT/$day
  258. ((SIZE=`du -sk $BACKUPROOT/$day | awk '{print $1}'`/1000))
  259. FREE=`df -P $BACKUPROOT/. |  sed -n 's/.* \([^ ]*\)%.*/\1/p'`
  260. df -k $BACKUPROOT/. $ARCH_FS >> $REPORTS
  261. $ORACLE_HOME/bin/rman $cat <<EOF 2>& 1 | tee -a $REPORTS
  262. connect target;
  263. list backup summary;
  264. quit;
  265. EOF
  266. echo "=======================  DETAILED LOGS  =======================================================" >> $REPORTS
  267. finish
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top