#!/bin/bash

export PATH=/bin:/usr/bin:/usr/local/bin
TODAY=`date +"%m-%d-%y-%H%M"`

DB_BACKUP_PATH='/home/ECISadm/backup'
MYSQL_HOST='localhost'
MYSQL_PORT='3306'
MYSQL_USER='ECIS_admin'
MYSQL_PASSWORD='password'
DATABASE_NAME='ECIS_production'
BACKUP_RETAIN_DAYS=5   ## Number of days to keep local backup copy

#################################################################

#Remove older backups
find ${DB_BACKUP_PATH} -mtime +${BACKUP_RETAIN_DAYS} -type f -delete

echo "Backup started for database - ${DATABASE_NAME}"

mysqldump \
   --single-transaction \
   -h ${MYSQL_HOST} \
   -P ${MYSQL_PORT} \
   -u ${MYSQL_USER} \
   -p${MYSQL_PASSWORD} \
   ${DATABASE_NAME} | gzip > ${DB_BACKUP_PATH}/${DATABASE_NAME}-${TODAY}.sql.gz

rsync -avp -e 'ssh -p 1222' ${DB_BACKUP_PATH}/* jield@your-server-hostname:/volume1/jield-backup

if [ $? -eq 0 ]; then
  echo "Database backup successfully completed"
else
  echo "Error found during backup"
fi