Skip to content

Commit 1bb7b51

Browse files
committed
Initial commit
0 parents  commit 1bb7b51

File tree

2 files changed

+268
-0
lines changed

2 files changed

+268
-0
lines changed

dump-tables.sh

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#!/bin/bash
2+
3+
# dump-tables.sh
4+
# Descr: Dump MySQL table data into separate SQL files for every database.
5+
# Usage: Run without args for usage info.
6+
# Author: sysadminstory
7+
# Notes:
8+
# * Script will prompt for password for db access.
9+
# * Script will restart the MySQL service if a dump fails
10+
# * Output files are written from the directory specified on command-line.
11+
12+
usage()
13+
{
14+
echo "MySQL Rescue dumper dump script"
15+
echo ""
16+
echo "Usage: "
17+
echo " $(basename $0) -h <db_host> -u <db_user> -o <output_dir> -r <restart_command> [-d <delay>]"
18+
echo " $(basename $0) -h"
19+
echo ""
20+
echo "Options:"
21+
echo " -s <db_host> Database Host"
22+
echo " -u <db_user> Database Username"
23+
echo " -o <output_dir> Output Directory"
24+
echo " -r <restart_command> MySQL Service restart command"
25+
echo " -d <delay> Delay in seconds to wait after restarting MySQL Service [default: 5]"
26+
echo " -h Show this help"
27+
exit 1
28+
}
29+
30+
# Clear variables
31+
unset DBHOST
32+
unset DBUSER
33+
unset DIR
34+
unset RESTART
35+
unset DELAY
36+
37+
# Set default values
38+
DELAY=5
39+
40+
# Handle arguments
41+
while getopts "s:u:o:r:d:h" o; do
42+
case "${o}" in
43+
s)
44+
DBHOST=${OPTARG}
45+
;;
46+
u)
47+
DBUSER=${OPTARG}
48+
;;
49+
o)
50+
DIR=${OPTARG}
51+
;;
52+
r)
53+
RESTART=${OPTARG}
54+
;;
55+
d)
56+
DELAY=${OPTARG}
57+
;;
58+
*|h)
59+
usage
60+
;;
61+
esac
62+
done
63+
64+
# Check if all parameters are set
65+
if [[ -z "$DBHOST" || -z "$DBUSER" || -z "$DIR" || -z "$RESTART" || -z "$DELAY" ]]
66+
then
67+
usage
68+
fi
69+
70+
# If the destination directory doesn't exist, create it
71+
if [ ! -d "$DIR" ]
72+
then
73+
echo "Creating output directory '$DIR'"
74+
mkdir -p $DIR
75+
fi
76+
77+
# Ask for the password
78+
read -p "Database Server Password :" -s DBPASS
79+
80+
# Get the list of database excluding 'information_schema'
81+
DBS=$(mysql -B -s -h"$DBHOST" -u"$DBUSER" -p"$DBPASS" -e 'show databases' | grep -v information_schema)
82+
83+
# Init global statistics vars
84+
TOTAL_TABLE_COUNT=0
85+
TOTAL_TABLE_COUNT_FAILED=0
86+
TOTAL_TABLE_COUNT_SUCCESS=0
87+
FAILED_TABLES=""
88+
89+
for DB in $DBS
90+
do
91+
echo "Dumping tables into separate SQL files for database '$DB' into the directory '$DIR'"
92+
93+
# Init database statistics vars
94+
TABLE_COUNT=0
95+
TABLE_COUNT_FAILED=0
96+
TABLE_COUNT_SUCCESS=0
97+
98+
# Get the list of tables
99+
TABLES=$(mysql -NBA -h"$DBHOST" -u"$DBUSER" -p"$DBPASS" -D $DB -e 'show tables')
100+
for TABLE in $TABLES
101+
do
102+
echo "Dumping Table : $DB.$TABLE"
103+
mysqldump -f -h"$DBHOST" -u"$DBUSER" -p"$DBPASS" $DB $TABLE --result-file=$DIR/$DB.$TABLE.sql
104+
105+
# If the dumps fails
106+
if [ $? -ne 0 ]
107+
then
108+
echo "Dump of table $DB.$TABLE failed !"
109+
echo "Restarting MySQL using command '$RESTART'"
110+
# Try to restart the MySQL daelon using the command provided
111+
$RESTART
112+
echo "Wating $DELAY seconds before continuing ..."
113+
sleep 10
114+
TABLE_COUNT_FAILED=$(( TABLE_COUNT_FAILED + 1 ))
115+
FAILED_TABLES=$"$FAILED_TABLES\n$DB.$TABLE"
116+
else
117+
118+
TABLE_COUNT_SUCCESS=$(( TABLE_COUNT_SUCCESS + 1 ))
119+
fi
120+
TABLE_COUNT=$(( TABLE_COUNT + 1 ))
121+
done
122+
123+
echo "$TABLE_COUNT_SUCCESS/$TABLE_COUNT table(s) dumped from database '$DB' into the directory '$DIR' ($TABLE_COUNT_FAILED table(s) failed)"
124+
125+
# Calculate global statistics
126+
TOTAL_TABLE_COUNT=$(( TOTAL_TABLE_COUNT + TABLE_COUNT ))
127+
TOTAL_TABLE_COUNT_FAILED=$(( TOTAL_TABLE_COUNT_FAILED + TABLE_COUNT_FAILED ))
128+
TOTAL_TABLE_COUNT_SUCCESS=$(( TOTAL_TABLE_COUNT_SUCCESS + TABLE_COUNT_SUCCESS ))
129+
done
130+
131+
# Show the statistics
132+
echo "Statistics :"
133+
echo "Tried to dump $TOTAL_TABLE_COUNT table(s)"
134+
echo "$TOTAL_TABLE_COUNT_FAILED table(s) dump failed"
135+
echo "$TOTAL_TABLE_COUNT_SUCCESS table(s) dump were successful"
136+
# Show the info about the failed dumps if needed
137+
if [ "$TOTAL_TABLE_COUNT_FAILED" -gt 0 ]
138+
then
139+
echo
140+
echo "The following table dump failed :"
141+
echo -e $FAILED_TABLES
142+
fi

restore-tables.sh

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#!/bin/bash
2+
3+
# restore-tables.sh
4+
# Descr: Restore MySQL table data from separate SQL files.
5+
# Usage: Run without args for usage info.
6+
# Author: sysadminstory
7+
# Notes:
8+
# * Script will prompt for password for db access.
9+
# * Input files are read from the directory specified on command-line.
10+
11+
usage()
12+
{
13+
echo "MySQL Rescue dumper restore script"
14+
echo ""
15+
echo "Usage: "
16+
echo " $(basename $0) -h <db_host> -u <db_user> -i <output_dir>"
17+
echo " $(basename $0) -h"
18+
echo ""
19+
echo "Options:"
20+
echo " -s <db_host> Database Host"
21+
echo " -u <db_user> Database Username"
22+
echo " -i <inout_dir> Output Directory"
23+
echo " -h Show this help"
24+
exit 1
25+
}
26+
27+
# Clear variables
28+
unset DBHOST
29+
unset DBUSER
30+
unset DIR
31+
unset RESTART
32+
unset DELAY
33+
34+
# Set default values
35+
DELAY=5
36+
37+
# Handle arguments
38+
while getopts "s:u:i:h" o; do
39+
case "${o}" in
40+
s)
41+
DBHOST=${OPTARG}
42+
;;
43+
u)
44+
DBUSER=${OPTARG}
45+
;;
46+
i)
47+
DIR=${OPTARG}
48+
;;
49+
*|h)
50+
usage
51+
;;
52+
esac
53+
done
54+
55+
# Check if all parameters are set
56+
if [[ -z "$DBHOST" || -z "$DBUSER" || -z "$DIR" ]]
57+
then
58+
usage
59+
fi
60+
61+
62+
# If the destination directory doesn't exist, create it
63+
if [ ! -d "$DIR" ]
64+
then
65+
echo "Input directory '$DIR' does not exist"
66+
exit 2
67+
fi
68+
69+
# Ask for the password
70+
read -p "Database Server Password :" -s DBPASS
71+
72+
# List the databases of the input directory
73+
DBS=$(ls -1 $DIR | cut -d'.' -f 1 | sort | uniq)
74+
75+
76+
TOTAL_TABLE_COUNT=0
77+
TOTAL_TABLE_COUNT_FAILED=0
78+
TOTAL_TABLE_COUNT_SUCCESS=0
79+
FAILED_FILES=""
80+
81+
for DB in $DBS
82+
do
83+
echo "Restoring tables from separate SQL files to database '$DB' from directory '$DIR'"
84+
85+
# Init database statistics vars
86+
TABLE_COUNT=0
87+
TABLE_COUNT_FAILED=0
88+
TABLE_COUNT_SUCCESS=0
89+
90+
for TABLEFILE in $DIR/$DB.*.sql
91+
do
92+
echo "Importing file : $TABLEFILE"
93+
mysql -h $DBHOST -u $DBUSER $DB < $TABLEFILE
94+
# If the retore fails
95+
if [ $? -ne 0 ]
96+
then
97+
echo "Import of file '$TABLEFILE' failed !"
98+
TABLE_COUNT_FAILED=$(( TABLE_COUNT_FAILED + 1 ))
99+
FAILED_FILES=$"$FAILED_TABLES\n$TABLEFILE"
100+
else
101+
102+
TABLE_COUNT_SUCCESS=$(( TABLE_COUNT_SUCCESS + 1 ))
103+
fi
104+
TABLE_COUNT=$(( TABLE_COUNT + 1 ))
105+
done
106+
107+
echo "$TABLE_COUNT_SUCCESS/$TABLE_COUNT table(s) imported to database '$DB' from directory '$DIR' ($TABLE_COUNT_FAILED table(s) failed)"
108+
109+
TOTAL_TABLE_COUNT=$(( TOTAL_TABLE_COUNT + TABLE_COUNT ))
110+
TOTAL_TABLE_COUNT_FAILED=$(( TOTAL_TABLE_COUNT_FAILED + TABLE_COUNT_FAILED ))
111+
TOTAL_TABLE_COUNT_SUCCESS=$(( TOTAL_TABLE_COUNT_SUCCESS + TABLE_COUNT_SUCCESS ))
112+
113+
done
114+
115+
# Show some statistics
116+
echo "Statistics :"
117+
echo "Tried to import $TOTAL_TABLE_COUNT table(s)"
118+
echo "$TOTAL_TABLE_COUNT_FAILED table(s) import failed"
119+
echo "$TOTAL_TABLE_COUNT_SUCCESS table(s) import were successful"
120+
# Show the info about the failed dumps if needed
121+
if [ "$TOTAL_TABLE_COUNT_FAILED" -gt 0 ]
122+
then
123+
echo
124+
echo "The following file(s) failed :"
125+
echo -e $FAILED_FILES
126+
fi

0 commit comments

Comments
 (0)