#!/bin/bash
MYSQL=`which mysql` #mysql version
MYSQLDUMP=`which mysqldump` #mysqldump file
HOSTNAME=`hostname` #hostname of backup server
USERNAME=”username” #mysql server user
PASSWORD=”password” #mysql user password
LOCALSERVER=”localhost” #mysql local server name
BACKUPDIRECTORY=/root/mysqldbbackup #backup directory where backup is stored
NOW=”$(date +”%d-%m-%Y”)” #Backup file format
DATATBASELIST=”" #Mysql database list
IGNOREDATABASE=”exampledb” #Ignore database not to be include in backup
GZIP=`which gzip` #Gzip to compress the backup file
FILENAME=”" #Database backup file names
#store the database list in DATABASELIST
DATABASELIST=`$MYSQL -u $USERNAME -h $LOCALSERVER -p$PASSWORD -Bse ‘show databases’`
#Fetch database on by one and take a backup
for db in $DATABASELIST
do
skipdatabase=-1
if [ "$IGNOREDATABASE" != "" ]; then
for i in $IGNOREDATABASE #check the ignore database list
do
if [ "$db" = "$i" ]; then
skipdatabase=1 #it will skip this database and move to second database list
fi
done
fi
if [ "$skipdatabase" = "-1" ]; then
FILE=”$BACKUPDIRECTORY/$db.$HOSTNAME.$NOW.gz” #Backup file format & Backup directory
#ALL in one command to take backup & compress the backup file
$MYSQLDUMP -u $USERNAME -h $LOCALSERVER -p$PASSWORD $db | $GZIP -9 > $FILE