213 lines
6.3 KiB
Bash
Executable File
213 lines
6.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
|
|
## - Database connection data
|
|
## -
|
|
db_user=root
|
|
db_pass=
|
|
db_host=localhost
|
|
db_port=3306
|
|
|
|
## - Some needed binaries
|
|
## -
|
|
mysql_bin=`which mysql`
|
|
mysqldump_bin=`which mysqldump`
|
|
gunzip_bin=`which gunzip`
|
|
find_bin=`which find`
|
|
|
|
echononl(){
|
|
echo X\\c > /tmp/shprompt$$
|
|
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
|
|
echo "$*\\c" 1>&2
|
|
else
|
|
echo -n "$*" 1>&2
|
|
fi
|
|
rm /tmp/shprompt$$
|
|
}
|
|
fatal(){
|
|
echo ""
|
|
echo Fehler: $*
|
|
echo -e "\n\t\033[31m\033[1mSkript wird abgebrochen\033[m\033[m\n"
|
|
exit 1
|
|
}
|
|
|
|
|
|
echo ""
|
|
echo -n "Give password for database user \"${db_user}\" [$db_pass]: "
|
|
stty -echo
|
|
read _pass
|
|
stty echo
|
|
while [ "X" == "X$_pass" ];do
|
|
echononl "Enry must not be empty! []: "
|
|
stty -echo
|
|
read _pass
|
|
stty echo
|
|
done
|
|
db_pass=$_pass
|
|
echo ""
|
|
|
|
|
|
PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin
|
|
|
|
BACKUP_BASE_DIR="/data/backup/mysql"
|
|
|
|
if [ ! -d $BACKUP_BASE_DIR ] ; then
|
|
echo
|
|
echo " Backup directory not found"
|
|
echo
|
|
exit 1
|
|
fi
|
|
|
|
|
|
logfile=restore_db_mysql.log
|
|
|
|
|
|
|
|
databases=`$find_bin $BACKUP_BASE_DIR -maxdepth 1 -mindepth 1 -type d -exec basename {} \;`
|
|
|
|
echo ""
|
|
echo "Do you want me restoring also the MySQL intern Database \"mysql\""
|
|
echo ""
|
|
_opt1="Yes, restore all table of intern database \"mysql\""
|
|
_opt2="Only add not present entries to table \"user\" and table \"db\""
|
|
_opt3="No, let all tables from MySQL intern database \"mysql\" untouched"
|
|
_opt4="QUIT - exit script NOW"
|
|
|
|
options=("$_opt1" "$_opt2" "$_opt3" "$_opt4")
|
|
PS3="Choose (1-4):"
|
|
|
|
_restore_mysql=3
|
|
select opt in "${options[@]}"
|
|
do
|
|
case $opt in
|
|
$_opt1)
|
|
_restore_mysql=1
|
|
break
|
|
;;
|
|
$_opt2)
|
|
_restore_mysql=2
|
|
break
|
|
;;
|
|
$_opt3)
|
|
_restore_mysql=3
|
|
break
|
|
;;
|
|
$_opt4)
|
|
exit 2
|
|
;;
|
|
*)
|
|
echo ""
|
|
echo "!! INVALID CHOISE !!"
|
|
echo ""
|
|
echo "1) $_opt1"
|
|
echo "2) $_opt2"
|
|
echo "3) $_opt3"
|
|
echo ""
|
|
;;
|
|
esac
|
|
done
|
|
|
|
|
|
echo -e "\n\tRestoring Databases\n"
|
|
for db in $databases ; do
|
|
|
|
#[ "$db" = "information_schema" ] && continue
|
|
|
|
_list=`ls -d -t ${BACKUP_BASE_DIR}/$db/*.sql.gz`
|
|
file=`echo $_list | awk '{print$1}'`
|
|
|
|
if [ "$db" = "information_schema" ]; then
|
|
echo ""
|
|
echononl "Do you want to restore database \"$db\" too? [yes/no]: "
|
|
read OK
|
|
while [ "X$OK" != "Xyes" -a "X$OK" != "XYes" -a "X$OK" != "Xja" -a "X$OK" != "XJa" \
|
|
-a "X$OK" != "XNo" -a "X$OK" != "Xno" -a "X$OK" != "Xn" -a "X$OK" != "Xnein" -a "X$OK" != "XNein" ]; do
|
|
echononl "Incorrect entry! [yes/no]: "
|
|
read OK
|
|
done
|
|
echo ""
|
|
if [ $OK = "Yes" -o $OK = "yes" -o "$OK" = "ja" -o "$OK" = "Ja" ]; then
|
|
echo -e "\t\trestoring ${db} (`basename $file`) .."
|
|
gunzip_bin < $file | mysql_bin -h$db_host -u$db_user -p'$db_pass $db'
|
|
fi
|
|
elif [ "$db" = "performance_schema" ]; then
|
|
echo ""
|
|
echononl "Do you want to restore database \"$db\" too? [yes/no]: "
|
|
read OK
|
|
while [ "X$OK" != "Xyes" -a "X$OK" != "XYes" -a "X$OK" != "Xja" -a "X$OK" != "XJa" \
|
|
-a "X$OK" != "XNo" -a "X$OK" != "Xno" -a "X$OK" != "Xn" -a "X$OK" != "Xnein" -a "X$OK" != "XNein" ]; do
|
|
echononl "Incorrect entry! [yes/no]: "
|
|
read OK
|
|
done
|
|
echo ""
|
|
if [ $OK = "Yes" -o $OK = "yes" -o "$OK" = "ja" -o "$OK" = "Ja" ]; then
|
|
echo -e "\t\trestoring ${db} (`basename $file`) .."
|
|
gunzip_bin < $file | mysql_bin -h$db_host -u$db_user -p'$db_pass $db'
|
|
fi
|
|
elif [ "$db" = "mysql" ]; then
|
|
|
|
if [ "$_restore_mysql" = "1" ]; then
|
|
|
|
## - save entry for User "debian-sys-maint"
|
|
## -
|
|
declare -i num
|
|
num=0
|
|
num=`$mysql_bin -u$db_user -P$db_port -u$db_user -p$db_pass $db -N -s -e "SELECT count(*) from user Where USER = 'debian-sys-maint'"`
|
|
if [ $num -gt 0 ];then
|
|
COLUMNS=`$mysql_bin -h$db_host -P$db_port -u$db_user -p$db_pass -N -s -e "SHOW COLUMNS FROM user FROM mysql" | awk '{print$1}'`
|
|
stmt_update="";
|
|
for column in $COLUMNS ; do
|
|
val=""
|
|
val=`mysql -h$db_host -P$db_port -u$db_user -p$db_pass mysql -N -s -e "SELECT $column FROM user WHERE User = 'debian-sys-maint'"`
|
|
stmt_update="UPDATE user SET $column = '$val' WHERE User = 'debian-sys-maint';$stmt_update"
|
|
done
|
|
fi
|
|
|
|
echo -e "\t\trestoring ${db} (`basename $file`) .."
|
|
$gunzip_bin < $file | $mysql_bin -h$db_host -u$db_user -p$db_pass $db
|
|
|
|
if [ $num -gt 0 ];then
|
|
stmt_insert="INSERT INTO user (Host,User) VALUES ('localhost','debian-sys-maint')"
|
|
$mysql_bin -u$db_user -P$db_port -u$db_user -p$db_pass $db -N -s -e "$stmt_insert"
|
|
$mysql_bin -u$db_user -P$db_port -u$db_user -p$db_pass $db -N -s -e "$stmt_update"
|
|
fi
|
|
|
|
# FLUSH PRIVILEGES
|
|
$mysql_bin -h$db_host -P$db_port -u$db_user -p$db_pass -N -s -e "FLUSH PRIVILEGES"
|
|
|
|
echo ""
|
|
echo "If password for database user \"root\" has changed within restoring \"${db}\""
|
|
echo -n "database, enter the new one. If not, only type <return>: "
|
|
stty -echo
|
|
read _pass
|
|
stty echo
|
|
if [ "X" != "X$_pass" ];then
|
|
db_pass=$_pass
|
|
fi
|
|
echo -e "\n"
|
|
elif [ "$_restore_mysql" = "2" ]; then
|
|
echo ""
|
|
_tables="db user"
|
|
for _table in $_tables ; do
|
|
_list_table=`ls -d -t ${BACKUP_BASE_DIR}/$db/mysql-${_table}-*.sql`
|
|
file_table=`echo $_list_table | awk '{print$1}'`
|
|
|
|
echo -e "\t\trestoring table \"$_table\" from database ${db} (`basename $file_table`) .."
|
|
$mysql_bin -f -h$db_host -P$db_port -u$db_user -p$db_pass $db < $file_table
|
|
done
|
|
echo ""
|
|
fi
|
|
else
|
|
echo -e "\t\trestoring ${db} (`basename $file`) .."
|
|
$mysql_bin -h$db_host -P$db_port -u$db_user -p$db_pass -N -s -e "DROP DATABASE IF EXISTS $db"
|
|
$mysql_bin -h$db_host -P$db_port -u$db_user -p$db_pass -N -s -e "CREATE DATABASE $db CHARACTER SET utf8 COLLATE utf8_general_ci"
|
|
$gunzip_bin < $file | $mysql_bin -h$db_host -P$db_port -u$db_user -p$db_pass $db
|
|
fi
|
|
|
|
done
|
|
echo
|
|
|
|
$mysql_bin -h$db_host -P$db_port -u$db_user -p$db_pass -N -s -e "FLUSH PRIVILEGES"
|
|
|
|
exit 0
|