#!/bin/sh

# Options used as follows:
# -e|--extended-insert
#     Allows utilization of the new, much faster INSERT syntax.
# --add-drop-table
#     Add a 'drop table' before each create.
# -F|--flush-logs
#     Flush logs file in server before starting dump.


USER="backup"
PASSWD=""
SOCKET="/tmp/mysql.sock"

GZIP="/usr/bin/gzip"
MYSQL="/usr/bin/mysql"
MYSQLDUMP="/usr/bin/mysqldump"
SAVE_PATH="/var/log/server-logs/backup-mysql/"

#for host in 10.10.25.126 192.168.56.217
for host in psychdb01 mon02
do
  dir=${SAVE_PATH}${host}

  [ -d ${dir} ] || /bin/mkdir -p ${dir}
  status=$?
  [ ${status} -eq 0 ] || exit ${status}

  cd ${dir}
  status=$?
  [ ${status} -eq 0 ] || exit ${status}

  DB_LIST=`${MYSQL} -h ${host} -u${USER} -p${PASSWD} -e "SHOW DATABASES" -B --skip-column-names`
  for db in ${DB_LIST}
  do
     ${MYSQLDUMP} -h ${host} --add-drop-table --single-transaction --flush-logs -e -u${USER} -p${PASSWD} ${db} > ${db}.sql

     # Compress the dumpfile
     ${GZIP} -f9 ${db}.sql
  done
done

