3 # ============LICENSE_START=======================================================
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
12 # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 # db_restore_data.sh: Restore only data for database table(s) from database backup file
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
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
33 # Note: use lower case table name
42 TEMP_FILE=/tmp/db_restore_data_$$.sql
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
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`"
58 function restore_table
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
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`"
80 echo "db_restore_data.sh started ... `date`"
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}
95 echo "BACKUP FILE NOT FOUND: $BACKUP_FILE"
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"
103 echo "db_restore_data.sh completed ... `date`"