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
44 echo "restore_all started ...@`date`"
45 echo "Before restore table ..."
47 mysql -u${DB_USER} -p${DB_PASSWORD} < $BACKUP_FILE
50 echo "restore_all completed ...@`date`"
53 function restore_table
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
66 echo "Before restore table ..."
67 mysql -u${DB_USER} -p${DB_PASSWORD} < $TEMP_FILE
69 echo "restore_table [$database] [$table] completed ...@`date`"
74 echo "db_restore.sh started ... `date`"
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}
89 echo "BACKUP FILE NOT FOUND: $BACKUP_FILE"
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"
97 echo "db_restore.sh completed ... `date`"