0f63535c775c730f0203e4b6106539633de4fe6f
[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
42 function restore_all
43 {
44   echo "restore_all started ...@`date`" | tee -a $LOG
45   echo "Before restore table ..." | tee -a $LOG
46   echo "--" 
47   mysql -u${DB_USER} -p${DB_PASSWORD} < $BACKUP_FILE 
48   echo "--" 
49
50   echo "restore_all completed ...@`date`" | tee -a $LOG
51 }
52
53 function restore_table
54 {
55   database="${1}"
56   table="${2}"
57   echo "restore_table [$database] [$table] started ...@`date`"
58   # extract sql statement from backup file
59   echo "use $database;" > $TEMP_FILE 
60   echo "set foreign_key_checks=0; " >> $TEMP_FILE
61   sed -n -e '/DROP TABLE IF EXISTS `'$table'`;/,/UNLOCK TABLES;/p' $BACKUP_FILE >> $TEMP_FILE
62   echo "set foreign_key_checks=1; " >> $TEMP_FILE
63   echo "--"
64   cat $TEMP_FILE
65   echo "--"
66   echo "Before restore table ..." | tee -a $LOG
67   mysql -u${DB_USER} -p${DB_PASSWORD} < $TEMP_FILE 
68   echo "--" 
69   echo "restore_table [$database] [$table] completed ...@`date`"
70 }
71
72
73 # MAIN
74 echo "db_restore.sh started ... `date`"
75 if [ $# -eq 5 ]; then 
76   DB_USER="${1}"
77   DB_PASSWORD="${2}"
78   BACKUP_FILE="${3}"
79   typeset -l DATABASE="${4}"
80   typeset -l TABLE="${5}"
81   echo "DB_USER: $DB_USER" 
82   if [ -f $BACKUP_FILE ]; then 
83     if [ "${TABLE}" != "all" ]; then 
84       restore_table ${DATABASE} ${TABLE}
85     else
86       restore_all
87     fi
88   else
89     echo "BACKUP FILE NOT FOUND: $BACKUP_FILE"
90   fi
91 else
92   echo "Usage  : db_restore.sh db_user_id  db_password  backup_file database table_name" 
93   echo "Example: db_restore.sh policy_user password     /opt/app/policy/data/mysql/20150901/backup_onap_sdk_20150901102030.sql onap_sdk attribute" 
94 fi
95
96 rm -f $TEMP_FILE
97 echo "db_restore.sh completed ... `date`"