Skip to content

Furnished Bckup

File: mysqlbk.sh

#!/bin/bash
#########################################################################
# Backup Database
# You can call this script from cron.
# if you have any issue write back to me
# [email protected] !!! ;) it is microsoft ? yeah dont junk me junk them
#########################################################################

mysqldump=$(which mysqldump)
mysql=$(which mysql)
gzip=$(which gzip)
chown=$(which chown)
DATE=$(date +%Y%m%d)

# Which user suppose to own this file
# If no value specified it is own by the owner of cron.
# you need to apply here e.g OWN_FILE="xyz"

OWN_FILE=""


# Please update to suitable backup folder.
# e.g. BACKUP_DIR="/home/xyz/sqlbackup"

BACKUP_DIR=""

# Add your mysql common password for all database
# For safety create read only user for all database
# Ignore using root password. e.g. DB_PASS="sds%87("

DB_PASS=""

# Add your databse Username following variable
# e.g. DB_USR="xyzdb"

DB_USR=""

# Specify your DB Host

DB_HOST=""

# Ignore following databse from backup

IGNORE="test"

# Get list of all databases
DB_LIST=`$mysql -h $DB_HOST -Bse -u $DB_USR -p$DB_PASS 'show databases'`

for db in $DB_LIST; do

        # set skip variable
        skip=0

        if [ "$IGNORE" != "" ]; then
                for i in $IGNORE; do
                        [ "$db" == "$i" ] && skip=1 || :
                done
        fi

        if [ "$skip" == "0" ]; then
                $mysqldump -R -h $DB_HOST -u $DB_USR -p$DB_PASS $db | $gzip -9 > $BACKUP_DIR/$db.$DATE.sql.gz
        if [ "$OWN_FILE" != ""]; then 
            $chown $OWN_FILE $BACKUP_DIR/$db.$DATE.sql.gz
        fi
        fi

done

exit 0