ONAP log files consolidation
[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 DATE=`date +"%Y%m%d"`
44 LOG=""
45
46 function restore_all
47 {
48   echo "restore_all started ...@`date`" 
49   echo "set foreign_key_checks=0;" > $TEMP_FILE
50   sed -e 's/LOCK TABLES \(.*\) WRITE;/delete from \1; LOCK TABLES \1 WRITE;/g' $BACKUP_FILE >> $TEMP_FILE
51   echo "set foreign_key_checks=1;" >> $TEMP_FILE
52   #cat $TEMP_FILE
53   echo "Before restore table ..." | tee -a $LOG
54   echo "--" | tee -a $LOG
55   mysql -u${DB_USER} -p${DB_PASSWORD} --verbose < $BACKUP_FILE 2>&1 | tee -a $LOG
56   echo "--" | tee -a $LOG
57   echo "restore_all completed ...@`date`" 
58 }
59
60 function restore_table
61 {
62   database="${1}"
63   table="${2}"
64   echo "restore_table [$database] [$table] started ...@`date`" 
65   # extract sql statement from backup file
66   echo "use $database;" > $TEMP_FILE
67   echo "set sql_safe_updates=0;" >> $TEMP_FILE
68   echo "delete from $table;" >> $TEMP_FILE
69   sed -n -e '/LOCK TABLES `'$table'` WRITE;/,/UNLOCK TABLES;/p' $BACKUP_FILE >> $TEMP_FILE
70   echo "set sql_safe_updates=1;" >> $TEMP_FILE
71   #echo "--"
72   #cat $TEMP_FILE
73   echo "Before restore table ..." 2>&1 | tee -a $LOG
74   echo "--" | tee -a $LOG
75   mysql -u${DB_USER} -p${DB_PASSWORD} --verbose < $TEMP_FILE 2>&1 | tee -a $LOG
76   echo "--" | tee -a $LOG
77   echo "restore_table [$database] [$table] completed ...@`date`" 
78 }
79
80
81 # MAIN
82 echo "db_restore_data.sh started ... `date`" 
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 [ -z ${POLICY_LOGS} ]; then
91     POLICY_LOGS=/var/log/onap
92   fi
93   mkdir -p $POLICY_LOGS/policy/db
94   LOG=$POLICY_LOGS/policy/db/db_restore_data_$DATE.log
95   if [ -f $BACKUP_FILE ]; then 
96     if [ "${TABLE}" != "all" ]; then 
97       restore_table ${DATABASE} ${TABLE}
98     else
99       restore_all
100     fi
101   else
102     echo "BACKUP FILE NOT FOUND: $BACKUP_FILE" 
103   fi
104 else
105   echo "Usage  : db_restore_data.sh db_user_id  db_password  backup_file database table_name" 
106   echo "Example: db_restore_data.sh policy_user password     /opt/app/policy/data/mysql/20150901/backup_xacml_data_20150901102030.sql xacml attribute" 
107 fi
108
109 rm -f $TEMP_FILE
110 echo "db_restore_data.sh completed ... `date`"