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.sh: Restore database table(s) from database backup file
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
29 # Assumption: Database backup_file is created from mysqldump utility
31 # Note: use lower case table name
40 TEMP_FILE=/tmp/db_restore_$$.sql
46 echo "restore_all started ...@`date`" | tee -a $LOG
47 echo "Before restore table ..." | tee -a $LOG
49 mysql -u${DB_USER} -p${DB_PASSWORD} < $BACKUP_FILE
52 echo "restore_all completed ...@`date`" | tee -a $LOG
55 function restore_table
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
68 echo "Before restore table ..." | tee -a $LOG
69 mysql -u${DB_USER} -p${DB_PASSWORD} < $TEMP_FILE
71 echo "restore_table [$database] [$table] completed ...@`date`" | tee -a $LOG
76 if [ -z ${POLICY_LOGS} ]; then
77 POLICY_LOGS=/var/log/onap
79 mkdir -p $POLICY_LOGS/policy/db
80 LOG=$POLICY_LOGS/policy/db/db_restore_$DATE.log
82 echo "db_restore.sh started ... `date`" | tee -a $LOG
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}
97 echo "BACKUP FILE NOT FOUND: $BACKUP_FILE" | tee -a $LOG
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"
105 echo "db_restore.sh completed ... `date`" | tee -a $LOG