mysql – back up and tar gz all databases

  linux
#!/bin/bash
# ====================================
# Back up your mysql databases.
#
# by Alan Lupsha, 2013
# ====================================

# set a list of databases here, space separated, ex: DATABASES=(confluence jira mdm mediaserver mydb mydivemap mysql)
DATABASES=(confluence jira mdm mediaserver mydb mydivemap mysql)

# set the path to where you want your backups made
# ex: BACKUPDIR=/mnt/t1/backups/mysql
BACKUPDIR=/mnt/t1/backups/mysql

# set your mysql root password here, ex: MYSQLROOTPWD=shhh12345
MYSQLROOTPWD=aaaaaaaa

# =========================================
# Build a string of repeating strings.
# Resulting string is $BANNERLINE
# ex: repeat 5 "=" -> returns "====="
# =========================================
function repeat {
  BANNERLINE=""
  for(( c=0; c < $1; c++))
  do
    BANNERLINE="${BANNERLINE}${2}"
  done
}
# =========================================

# =========================================
# Build a banner. Specify the text you want
# in the banner, which is argument $1.
# The borders are called $PAINT, and are $2
# =========================================
function banner {
  # reset the banner line
  BANNERLINE=""
  # the border character
  PAINT="${2}"
  # you can change this to increase the banner size
  BANNERWIDTH=80
  # top line
  repeat $BANNERWIDTH $PAINT
  echo  $BANNERLINE
  # middle line
  TEXTWIDTH=${#1}
  X=0
  X=$(( $BANNERWIDTH - $TEXTWIDTH - 3 ))
  repeat $X " "
  echo "${PAINT} ${1}${BANNERLINE}${PAINT}"
  # bottom line
  repeat $BANNERWIDTH $PAINT
  echo  $BANNERLINE
  echo ""
}
# =========================================

# =========================================
# banner types
# =========================================
function banner1 {
  PAINT="#"
  banner "$1" "$PAINT"
}
function banner2 {
  PAINT="="
  banner "$1" "$PAINT"
}
function banner3 {
  PAINT="~"
  banner "$1" "$PAINT"
}
function banner4 {
  PAINT="."
  banner "$1" "$PAINT"
}

for db in ${DATABASES[@]}; do
  banner1 "Now backing up database: $db"
  banner4 "--> mysqldump --verbose -u root -pSECRET $db | gzip > $BACKUPDIR/`date +'%Y.%m.%d'`_$db.sql.gz"
  mysqldump --verbose -u root -p$MYSQLROOTPWD $db | gzip > $BACKUPDIR/`date +'%Y.%m.%d'`_$db.sql.gz
  banner4 "Finished backing up database ${db}"
done

banner1 "Database backups completed. Have a nice day."