blob: b0e32dac3e11e2359d1173e02ade353a0bba2492 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
#!/bin/bash
###
# ============LICENSE_START=======================================================
# ECOMP Policy Engine
# ================================================================================
# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
# ================================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============LICENSE_END=========================================================
###
#
# db_restore.sh: Restore database table(s) from database backup file
#
# Usage : db_restore.sh db_user db_password backup_file database table_name
# Example: db_restore.sh policy_user password /opt/app/policy/data/mysql/20150901/backup_ecomp_sdk_20150910102030.sql ecomp_sdk attribute
# db_restore.sh policy_user password /opt/app/policy/data/mysql/20150901/backup_ecomp_sdk_20150910102030.sql ecomp_sdk all
#
# Assumption: Database backup_file is created from mysqldump utility
#
# Note: use lower case table name
#
#
DB_USER=""
DB_PASSWORD=""
BACKUP_FILE=""
DATABASE=""
TABLE=""
TEMP_FILE=/tmp/db_restore_$$.sql
function restore_all
{
echo "restore_all started ...@`date`"
echo "Before restore table ..."
echo "--"
mysql -u${DB_USER} -p${DB_PASSWORD} < $BACKUP_FILE
echo "--"
echo "restore_all completed ...@`date`"
}
function restore_table
{
database="${1}"
table="${2}"
echo "restore_table [$database] [$table] started ...@`date`"
# extract sql statement from backup file
echo "use $database;" > $TEMP_FILE
echo "set foreign_key_checks=0; " >> $TEMP_FILE
sed -n -e '/DROP TABLE IF EXISTS `'$table'`;/,/UNLOCK TABLES;/p' $BACKUP_FILE >> $TEMP_FILE
echo "set foreign_key_checks=1; " >> $TEMP_FILE
echo "--"
cat $TEMP_FILE
echo "--"
echo "Before restore table ..."
mysql -u${DB_USER} -p${DB_PASSWORD} < $TEMP_FILE
echo "--"
echo "restore_table [$database] [$table] completed ...@`date`"
}
# MAIN
echo "db_restore.sh started ... `date`"
if [ $# -eq 5 ]; then
DB_USER="${1}"
DB_PASSWORD="${2}"
BACKUP_FILE="${3}"
typeset -l DATABASE="${4}"
typeset -l TABLE="${5}"
echo "DB_USER: $DB_USER"
if [ -f $BACKUP_FILE ]; then
if [ "${TABLE}" != "all" ]; then
restore_table ${DATABASE} ${TABLE}
else
restore_all
fi
else
echo "BACKUP FILE NOT FOUND: $BACKUP_FILE"
fi
else
echo "Usage : db_restore.sh db_user_id db_password backup_file database table_name"
echo "Example: db_restore.sh policy_user password /opt/app/policy/data/mysql/20150901/backup_ecomp_sdk_20150901102030.sql ecomp_sdk attribute"
fi
rm -f $TEMP_FILE
echo "db_restore.sh completed ... `date`"
|