Export mysql databases


The script exports all databases of mysql in individual files. All sql files will be saved in the directory /tmp/db but you can define your desired path.

Enter your mysql username in place of text username and your password in place of text password in the script. Do not remove the inverted commas around password. Put your mysql password inside it.



#!/bin/bash
#Take back up of all databases of mysql

############### variables ###############
bu_path=/tmp/db
user=username
pass=\'password\'
host=127.0.0.1
########################################

mkdir "$bu_path"
IFS=$'\n'
lst=$(sudo mysql -h $host -u $user -p$pass -e 'show databases' | grep -iv "database" | sort;)
for i in $lst
do
sudo mysqldump -h $host -u $user -p$pass "$i" > "$bu_path/$i.sql"
echo "$i exported successfully"
done


Save the code in the text file and save as export.sh
Now give the executing permission to the script 
# chmod a+x /path/of/export.sh
Now run the script.
# cd /path/to/the/directory 

# ./export.sh

No comments:

Post a Comment