Backup all DB in one shell script

#!/bin/bash 
MyUSER="SET-MYSQL-USER-NAME"  # USERNAME
MyPASS="SET-PASSWORD"         # PASSWORD
MyHOST="localhost"            # Hostname
 
# Linux bin paths 'which' = autodetect
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"
 
# Destination directory
DEST="/backup"
 
# Main directory for backup
MBD="$DEST/mysql"
 
# Get hostname - in target backup filename
HOST="$(hostname)"
 
# Get data in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y")"
 
# File to store current backup file
FILE=""
# list of databases
DBS=""
 
# DO NOT BACKUP these databases
EXCLUDEDB="test"
 
[ ! -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
  skipdb=-1
  if [ "$EXCLUDEDB" != "" ];
    then
      for i in $EXCLUDEDB
      do
      [ "$db" == "$i" ] && skipdb=1 || :
      done
  fi
 
  if [ "$skipdb" == "-1" ] ; 
    then
      FILE="$MBD/$db.$HOST.$NOW.gz"
      # the meat of everything
      $MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > $FILE
  fi
done

One thought on “Backup all DB in one shell script

  1. Pingback: Recent URLs tagged Skipdb - Urlrecorder

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">