How to Shell script backup all Mysql Databases in Linux

This shell script will backup all current Mysql databases, compress(gzip) them and put them in a new folder named by current date (one directory per database).

#!/bin/bash
PATH=/usr/sbin:/sbin:/bin:/usr/bin
MyUSER="your_db_user"
MyPASS="your_db_password"
MyHOST="your_db_host"
SUBFOLDER="$(date +"%Y-%m-%d")"
DEST="/somewhere/on/your/server"
MDB="$DEST/backup/$SUBFOLDER"
if [ ! -d $MDB ]
then
mkdir -p $MDB >/dev/null 2>&1 && echo "Directory $MDB created." ||  echo "Error: Failed to create $MDB directory."
else
echo "Error: $MDB directory exits!"
fi
NOW="$(date +"%Y-%m-%d_%H-%M-%S")"
FILE=""
DBS="$(mysql -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"
for db in $DBS
do
FILE="$MDB/$db.$NOW.sql.gz"
mysqldump -u $MyUSER -h $MyHOST -p$MyPASS --complete-insert $db | gzip -9 > $FILE
echo "Backup $FILE.....DONE"
done
GD Star Rating
loading...
GD Star Rating
loading...

Related Articles

Random Articles

  1. 1 Trackback(s)

  2. Feb 28, 2012: How to enable curl and JSON support with PHP | Network Solution for Windows - Windows system, Linux system, Ubuntu server configuration tip

Post a Comment