#/bin/bash
while [ 1 ]
do
echo " #############################################"
echo " Quick Backup and Recovery"
echo " Source and Destination Mysql Must Be Same"
echo " #############################################"
echo " 1) Back up database test"
echo " 2) Recovery database test"
echo " 3) Clear data in database test"
echo " q) Quit"
echo -n " Your Option:"
read option_char
current_dir=$(pwd)
case ${option_char} in
"1")
rm -f $current_dir/test_bk.sql
mysqldump -t -c -uroot -proot test > $current_dir/test_backup.sql
echo " Database test already backup..."
;;
"2")
mysql -u root --password='root' -e "
use test
source $current_dir/test_backup.sql"
echo " Database test already recovery..."
;;
"3")
rm -f $current_dir/truncate_test.sql
chmod 777 $current_dir
mysql -u root --password='root' -e "
SELECT CONCAT('TRUNCATE TABLE ',TABLE_NAME,';') FROM information_schema.TABLES WHERE TABLE_SCHEMA='test' into outfile '/tmp/truncate_test.sql';"
mv /tmp/truncate_test.sql $current_dir/
mysql -u root --password='root' -e "
use test
source $current_dir/truncate_test.sql"
echo " Clear data of test successful..."
;;
"q"|"quit"|"exit"|'Q'|"QUIT"|"Quit")
break
;;
*)
echo "your option is invalid, please input again..."
;;
esac
done