ONAP log files consolidation
[policy/engine.git] / packages / base / src / files / install / mysql / bin / db_restore.sh
1 #!/bin/bash 
2 ###
3 # ============LICENSE_START=======================================================
4 # ONAP Policy Engine
5 # ================================================================================
6 # Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 # ================================================================================
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11
12 #      http://www.apache.org/licenses/LICENSE-2.0
13
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 # ============LICENSE_END=========================================================
20 ###
21
22 #
23 # db_restore.sh: Restore database table(s) from database backup file
24
25 # Usage  : db_restore.sh db_user     db_password backup_file database table_name
26 # Example: db_restore.sh policy_user password    /opt/app/policy/data/mysql/20150901/backup_onap_sdk_20150910102030.sql onap_sdk attribute
27 #          db_restore.sh policy_user password    /opt/app/policy/data/mysql/20150901/backup_onap_sdk_20150910102030.sql onap_sdk all
28 #
29 # Assumption: Database backup_file is created from mysqldump utility
30 #
31 # Note: use lower case table name
32 #
33 #
34
35 DB_USER=""
36 DB_PASSWORD=""
37 BACKUP_FILE=""
38 DATABASE=""
39 TABLE=""
40 TEMP_FILE=/tmp/db_restore_$$.sql
41 DATE=`date +"%Y%m%d"`
42 LOG=""
43
44 function restore_all
45 {
46   echo "restore_all started ...@`date`" | tee -a $LOG
47   echo "Before restore table ..." | tee -a $LOG
48   echo "--" 
49   mysql -u${DB_USER} -p${DB_PASSWORD} < $BACKUP_FILE 
50   echo "--" 
51
52   echo "restore_all completed ...@`date`" | tee -a $LOG
53 }
54
55 function restore_table
56 {
57   database="${1}"
58   table="${2}"
59   echo "restore_table [$database] [$table] started ...@`date`" | tee -a $LOG
60   # extract sql statement from backup file
61   echo "use $database;" > $TEMP_FILE 
62   echo "set foreign_key_checks=0; " >> $TEMP_FILE
63   sed -n -e '/DROP TABLE IF EXISTS `'$table'`;/,/UNLOCK TABLES;/p' $BACKUP_FILE >> $TEMP_FILE
64   echo "set foreign_key_checks=1; " >> $TEMP_FILE
65   echo "--"
66   cat $TEMP_FILE
67   echo "--"
68   echo "Before restore table ..." | tee -a $LOG
69   mysql -u${DB_USER} -p${DB_PASSWORD} < $TEMP_FILE 
70   echo "--" 
71   echo "restore_table [$database] [$table] completed ...@`date`" | tee -a $LOG
72 }
73
74
75 # MAIN
76 if [ -z ${POLICY_LOGS} ]; then
77   POLICY_LOGS=/var/log/onap
78 fi
79 mkdir -p $POLICY_LOGS/policy/db
80 LOG=$POLICY_LOGS/policy/db/db_restore_$DATE.log
81   
82 echo "db_restore.sh started ... `date`" | tee -a $LOG
83 if [ $# -eq 5 ]; then 
84   DB_USER="${1}"
85   DB_PASSWORD="${2}"
86   BACKUP_FILE="${3}"
87   typeset -l DATABASE="${4}"
88   typeset -l TABLE="${5}"
89   echo "DB_USER: $DB_USER" 
90   if [ -f $BACKUP_FILE ]; then 
91     if [ "${TABLE}" != "all" ]; then 
92       restore_table ${DATABASE} ${TABLE}
93     else
94       restore_all
95     fi
96   else
97     echo "BACKUP FILE NOT FOUND: $BACKUP_FILE" | tee -a $LOG
98   fi
99 else
100   echo "Usage  : db_restore.sh db_user_id  db_password  backup_file database table_name" 
101   echo "Example: db_restore.sh policy_user password     /opt/app/policy/data/mysql/20150901/backup_onap_sdk_20150901102030.sql onap_sdk attribute" 
102 fi
103
104 rm -f $TEMP_FILE
105 echo "db_restore.sh completed ... `date`" | tee -a $LOG