Merge "ONAP code change for log files consolidation"
[policy/engine.git] / packages / base / src / files / install / mysql / bin / db_restore.sh
index b0e32da..765b0ca 100644 (file)
@@ -1,7 +1,7 @@
 #!/bin/bash 
 ###
 # ============LICENSE_START=======================================================
-# ECOMP Policy Engine
+# ONAP Policy Engine
 # ================================================================================
 # Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 # ================================================================================
@@ -23,8 +23,8 @@
 # db_restore.sh: Restore database table(s) from database backup file
 # 
 # Usage  : db_restore.sh db_user     db_password backup_file database table_name
-# Example: db_restore.sh policy_user password    /opt/app/policy/data/mysql/20150901/backup_ecomp_sdk_20150910102030.sql ecomp_sdk attribute
-#          db_restore.sh policy_user password    /opt/app/policy/data/mysql/20150901/backup_ecomp_sdk_20150910102030.sql ecomp_sdk all
+# Example: db_restore.sh policy_user password    /opt/app/policy/data/mysql/20150901/backup_onap_sdk_20150910102030.sql onap_sdk attribute
+#          db_restore.sh policy_user password    /opt/app/policy/data/mysql/20150901/backup_onap_sdk_20150910102030.sql onap_sdk all
 #
 # Assumption: Database backup_file is created from mysqldump utility
 #
@@ -38,40 +38,48 @@ BACKUP_FILE=""
 DATABASE=""
 TABLE=""
 TEMP_FILE=/tmp/db_restore_$$.sql
+DATE=`date +"%Y%m%d"`
+LOG=""
 
 function restore_all
 {
-  echo "restore_all started ...@`date`" 
-  echo "Before restore table ..." 
+  echo "restore_all started ...@`date`" | tee -a $LOG
+  echo "Before restore table ..." | tee -a $LOG
   echo "--" 
   mysql -u${DB_USER} -p${DB_PASSWORD} < $BACKUP_FILE 
   echo "--" 
 
-  echo "restore_all completed ...@`date`" 
+  echo "restore_all completed ...@`date`" | tee -a $LOG
 }
 
 function restore_table
 {
   database="${1}"
   table="${2}"
-  echo "restore_table [$database] [$table] started ...@`date`" 
+  echo "restore_table [$database] [$table] started ...@`date`" | tee -a $LOG
   # extract sql statement from backup file
-  echo "use $database;" > $TEMP_FILE
+  echo "use $database;" > $TEMP_FILE 
   echo "set foreign_key_checks=0; " >> $TEMP_FILE
   sed -n -e '/DROP TABLE IF EXISTS `'$table'`;/,/UNLOCK TABLES;/p' $BACKUP_FILE >> $TEMP_FILE
   echo "set foreign_key_checks=1; " >> $TEMP_FILE
   echo "--"
   cat $TEMP_FILE
   echo "--"
-  echo "Before restore table ..." 
+  echo "Before restore table ..." | tee -a $LOG
   mysql -u${DB_USER} -p${DB_PASSWORD} < $TEMP_FILE 
   echo "--" 
-  echo "restore_table [$database] [$table] completed ...@`date`" 
+  echo "restore_table [$database] [$table] completed ...@`date`" | tee -a $LOG
 }
 
 
 # MAIN
-echo "db_restore.sh started ... `date`" 
+if [ -z ${POLICY_LOGS} ]; then
+  POLICY_LOGS=/var/log/onap
+fi
+mkdir -p $POLICY_LOGS/policy/db
+LOG=$POLICY_LOGS/policy/db/db_restore_$DATE.log
+  
+echo "db_restore.sh started ... `date`" | tee -a $LOG
 if [ $# -eq 5 ]; then 
   DB_USER="${1}"
   DB_PASSWORD="${2}"
@@ -86,12 +94,12 @@ if [ $# -eq 5 ]; then
       restore_all
     fi
   else
-    echo "BACKUP FILE NOT FOUND: $BACKUP_FILE" 
+    echo "BACKUP FILE NOT FOUND: $BACKUP_FILE" | tee -a $LOG
   fi
 else
   echo "Usage  : db_restore.sh db_user_id  db_password  backup_file database table_name" 
-  echo "Example: db_restore.sh policy_user password     /opt/app/policy/data/mysql/20150901/backup_ecomp_sdk_20150901102030.sql ecomp_sdk attribute" 
+  echo "Example: db_restore.sh policy_user password     /opt/app/policy/data/mysql/20150901/backup_onap_sdk_20150901102030.sql onap_sdk attribute" 
 fi
 
 rm -f $TEMP_FILE
-echo "db_restore.sh completed ... `date`" 
+echo "db_restore.sh completed ... `date`" | tee -a $LOG