-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore-tables.sh
executable file
·126 lines (106 loc) · 2.76 KB
/
restore-tables.sh
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
# restore-tables.sh
# Descr: Restore MySQL table data from separate SQL files.
# Usage: Run without args for usage info.
# Author: sysadminstory
# Notes:
# * Script will prompt for password for db access.
# * Input files are read from the directory specified on command-line.
usage()
{
echo "MySQL Rescue dumper restore script"
echo ""
echo "Usage: "
echo " $(basename $0) -h <db_host> -u <db_user> -i <output_dir>"
echo " $(basename $0) -h"
echo ""
echo "Options:"
echo " -s <db_host> Database Host"
echo " -u <db_user> Database Username"
echo " -i <inout_dir> Output Directory"
echo " -h Show this help"
exit 1
}
# Clear variables
unset DBHOST
unset DBUSER
unset DIR
unset RESTART
unset DELAY
# Set default values
DELAY=5
# Handle arguments
while getopts "s:u:i:h" o; do
case "${o}" in
s)
DBHOST=${OPTARG}
;;
u)
DBUSER=${OPTARG}
;;
i)
DIR=${OPTARG}
;;
*|h)
usage
;;
esac
done
# Check if all parameters are set
if [[ -z "$DBHOST" || -z "$DBUSER" || -z "$DIR" ]]
then
usage
fi
# If the destination directory doesn't exist, create it
if [ ! -d "$DIR" ]
then
echo "Input directory '$DIR' does not exist"
exit 2
fi
# Ask for the password
read -p "Database Server Password :" -s DBPASS
# List the databases of the input directory
DBS=$(ls -1 $DIR | cut -d'.' -f 1 | sort | uniq)
TOTAL_TABLE_COUNT=0
TOTAL_TABLE_COUNT_FAILED=0
TOTAL_TABLE_COUNT_SUCCESS=0
FAILED_FILES=""
for DB in $DBS
do
echo "Restoring tables from separate SQL files to database '$DB' from directory '$DIR'"
# Init database statistics vars
TABLE_COUNT=0
TABLE_COUNT_FAILED=0
TABLE_COUNT_SUCCESS=0
for TABLEFILE in $DIR/$DB.*.sql
do
echo "Importing file : $TABLEFILE"
mysql -h $DBHOST -u $DBUSER $DB < $TABLEFILE
# If the retore fails
if [ $? -ne 0 ]
then
echo "Import of file '$TABLEFILE' failed !"
TABLE_COUNT_FAILED=$(( TABLE_COUNT_FAILED + 1 ))
FAILED_FILES=$"$FAILED_TABLES\n$TABLEFILE"
else
TABLE_COUNT_SUCCESS=$(( TABLE_COUNT_SUCCESS + 1 ))
fi
TABLE_COUNT=$(( TABLE_COUNT + 1 ))
done
echo "$TABLE_COUNT_SUCCESS/$TABLE_COUNT table(s) imported to database '$DB' from directory '$DIR' ($TABLE_COUNT_FAILED table(s) failed)"
TOTAL_TABLE_COUNT=$(( TOTAL_TABLE_COUNT + TABLE_COUNT ))
TOTAL_TABLE_COUNT_FAILED=$(( TOTAL_TABLE_COUNT_FAILED + TABLE_COUNT_FAILED ))
TOTAL_TABLE_COUNT_SUCCESS=$(( TOTAL_TABLE_COUNT_SUCCESS + TABLE_COUNT_SUCCESS ))
done
# Show some statistics
echo "Statistics :"
echo "Tried to import $TOTAL_TABLE_COUNT table(s)"
echo "$TOTAL_TABLE_COUNT_FAILED table(s) import failed"
echo "$TOTAL_TABLE_COUNT_SUCCESS table(s) import were successful"
# Show the info about the failed dumps if needed
if [ "$TOTAL_TABLE_COUNT_FAILED" -gt 0 ]
then
echo
echo "The following file(s) failed :"
echo -e $FAILED_FILES
fi