Last active
October 24, 2019 05:37
-
-
Save rajeshisnepali/bbf08ed3dff9214b1b81f03cefb0c11b to your computer and use it in GitHub Desktop.
mysql dump database with specific database [folder name].
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# colors | |
GREEN='\033[0;32m' | |
NC='\033[0m' # No Color | |
# dbDump functions | |
dbDump() { | |
# backup location | |
LOCATION=~/dumps/ | |
# create dir if not exists | |
if [ ! -d $LOCATION ] | |
then | |
mkdir $LOCATION | |
fi | |
# for dumping multiple databases | |
for d in $@ | |
do | |
mkdir -p $LOCATION$d; | |
FINAL_LOCATION=$LOCATION$d/; | |
mysqldump -u root -p$mysql_pswd $d|gzip > $FINAL_LOCATION$(date +%Y%m%d'_'%H:%M).sql.gz | |
echo -e "Mysql Database ${GREEN}$d${NC} dumped." | |
done | |
} | |
# get db names for the dump | |
databaseDumpProcess() { | |
read -p "Enter database name{s} for dumping [multiple names with spaces]:" db_names | |
dbDump $db_names | |
} | |
# checks if mysql password matched | |
checkMysqlPasswordMatched() { | |
until mysql -u root -p$mysql_pswd -e ";" ; do | |
read -s -p "Can't connect, please retry: " mysql_pswd | |
done | |
} | |
### Input mysql password as hidden characters ### | |
read -s -p "Enter Mysql Password: " mysql_pswd | |
checkMysqlPasswordMatched | |
printf "%s\n" | |
read -p "Do you want to list the databases before dumping [y]?" yn | |
case $yn in | |
[Yy]* ) | |
mysql -u root -p$mysql_pswd -e 'show databases'; | |
databaseDumpProcess | |
exit;; | |
[Nn]* ) | |
databaseDumpProcess | |
exit;; | |
* ) mysql -u root -p$mysql_pswd -e 'show databases'; | |
databaseDumpProcess | |
exit;; | |
esac | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment