c4fec027fc8e97c14848aa771b919d82da110380
[policy/engine.git] / packages / base / src / files / install / mysql / bin / db_restore_data.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_data.sh: Restore only data for database table(s) from database backup file
24
25 # Usage  : db_restore_data.sh db_user     db_password backup_file database table_name
26 # Example: db_restore_data.sh policy_user password    /opt/app/policy/data/mysql/20150901/backup_xacml_data_20150910102030.sql xacml attribute
27 #          db_restore_data.sh policy_user password    /opt/app/policy/data/mysql/20150901/backup_xacml_data_20150910102030.sql xacml all
28 #
29 # Assumption: 
30 #   1. Database backup_file is created from db_backup_data.sh (contains only data)
31 #   2. Data in table(s) will be wiped out and loaded from backup file
32 #
33 # Note: use lower case table name
34 #
35 #
36
37 DB_USER=""
38 DB_PASSWORD=""
39 BACKUP_FILE=""
40 DATABASE=""
41 TABLE=""
42 TEMP_FILE=/tmp/db_restore_data_$$.sql
43
44 function restore_all
45 {
46   echo "restore_all started ...@`date`" 
47   echo "set foreign_key_checks=0;" > $TEMP_FILE
48   sed -e 's/LOCK TABLES \(.*\) WRITE;/delete from \1; LOCK TABLES \1 WRITE;/g' $BACKUP_FILE >> $TEMP_FILE
49   echo "set foreign_key_checks=1;" >> $TEMP_FILE
50   #cat $TEMP_FILE
51   echo "Before restore table ..." | tee -a $LOG
52   echo "--" | tee -a $LOG
53   mysql -u${DB_USER} -p${DB_PASSWORD} --verbose < $BACKUP_FILE 2>&1 | tee -a $LOG
54   echo "--" | tee -a $LOG
55   echo "restore_all completed ...@`date`" 
56 }
57
58 function restore_table
59 {
60   database="${1}"
61   table="${2}"
62   echo "restore_table [$database] [$table] started ...@`date`" 
63   # extract sql statement from backup file
64   echo "use $database;" > $TEMP_FILE
65   echo "set sql_safe_updates=0;" >> $TEMP_FILE
66   echo "delete from $table;" >> $TEMP_FILE
67   sed -n -e '/LOCK TABLES `'$table'` WRITE;/,/UNLOCK TABLES;/p' $BACKUP_FILE >> $TEMP_FILE
68   echo "set sql_safe_updates=1;" >> $TEMP_FILE
69   #echo "--"
70   #cat $TEMP_FILE
71   echo "Before restore table ..." 2>&1 | tee -a $LOG
72   echo "--" | tee -a $LOG
73   mysql -u${DB_USER} -p${DB_PASSWORD} --verbose < $TEMP_FILE 2>&1 | tee -a $LOG
74   echo "--" | tee -a $LOG
75   echo "restore_table [$database] [$table] completed ...@`date`" 
76 }
77
78
79 # MAIN
80 echo "db_restore_data.sh started ... `date`" 
81 if [ $# -eq 5 ]; then 
82   DB_USER="${1}"
83   DB_PASSWORD="${2}"
84   BACKUP_FILE="${3}"
85   typeset -l DATABASE="${4}"
86   typeset -l TABLE="${5}"
87   echo "DB_USER: $DB_USER" 
88   if [ -f $BACKUP_FILE ]; then 
89     if [ "${TABLE}" != "all" ]; then 
90       restore_table ${DATABASE} ${TABLE}
91     else
92       restore_all
93     fi
94   else
95     echo "BACKUP FILE NOT FOUND: $BACKUP_FILE" 
96   fi
97 else
98   echo "Usage  : db_restore_data.sh db_user_id  db_password  backup_file database table_name" 
99   echo "Example: db_restore_data.sh policy_user password     /opt/app/policy/data/mysql/20150901/backup_xacml_data_20150901102030.sql xacml attribute" 
100 fi
101
102 rm -f $TEMP_FILE
103 echo "db_restore_data.sh completed ... `date`"