#!/bin/sh

#this script will restore all sql files in the current directory
#NOTE it will skip the mysql.sql, performance_schema.sql and information_schema.sql, should they exist.

#If you need to restore mysql.sql, then call the script like this:
#./restore_sql_files.sh with_mysql

#but make sure you know the da_admin password that is being restored.
#you might need to put it in /usr/local/directadmin/conf/mysql.conf after.


WITH_MYSQL=0;
if [ "$#" -gt 0 ]; then
	case "$1" in
		with_mysql) WITH_MYSQL=1;
			;;
	esac
fi

if [ "$WITH_MYSQL" -eq 1 ]; then
	LIST="ls *.sql | grep -v performance_schema | grep -v information_schema | cut -d. -f1"
else
	LIST="ls *.sql | grep -v mysql.sql | grep -v performance_schema | grep -v information_schema | cut -d. -f1"
fi


DA_MYSQL=/usr/local/directadmin/conf/mysql.conf
MYSQLUSER=`grep "^user=" ${DA_MYSQL} | cut -d= -f2`
MYSQLPASSWORD=`grep "^passwd=" ${DA_MYSQL} | cut -d= -f2`

for i in `eval $LIST`; do
{
	echo "restoring database $i";
	mysql -u${MYSQLUSER} -p${MYSQLPASSWORD} < $i.sql
};
done;

if [ "$WITH_MYSQL" -eq 1 ]; then
	echo "Because you've just restored the mysql datbase, you may need to restart mysqld to flush the privileges";
	echo "Also, your password in ${DA_MYSQL} may be invalid now if the da_admin password from the mysql.sql has changed.";
fi

exit 0;
