Backup MySql – Shell Script
Here is a Shell script using which Mysql Database instance can be backed up. You can schedule this script using cron or any other scheduling utility to run once in a day and take backup of all database schemas in your database.
#!/bin/bash
# Shell script to backup MySql database
MyUSER="mysql_user_name"
MyPASS="mysql_user_password"
MyHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"
# Backup Dest directory, change this if you have someother location
DEST="/backup"
# Main directory where backup will be stored
MBD="$DEST/mysql"
HOST="$(hostname)"
NOW="$(date +"%d-%m-%Y")"
[ ! -d $MBD ] && mkdir -p $MBD || :
# Only root can access it!
$CHOWN 0.0 -R $DEST
$CHMOD 0600 $DEST
# Get all database list first
DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"
for db in $DBS
do
FILE="$MBD/$db.$HOST.$NOW.gz"
# This will create gzipped file for the mysqldump of db schema
$MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > $FILE
fi
done
Most Commented Posts
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

[...] days back I wrote a post about, how to use shell script to backup mysql database. Using this shell script you will have lot of data files which will be created every day. You can [...]