#!/bin/bash
# database credentials
DATABASEHOST="host"
DATABASEUSER="user"
DATABASEPASSWORD="password"
DATABASESCHEMA="database"
DATABASEENV="backup"
# Local directory of mysqldump file
LOCALDIR=/home/user/db-bkp
# Temporary directory for compressed file
TEMPDIR=/home/user/tmp
# Remote Directory for backups.
REMOTEDIR=/home/user/db-bkp
# Backup host to login to
BACKUPHOST="otherhost"
#--- end config
echo $(date +%H:%M)
echo "Creating the MySQL dump"
mysqldump --host="$DATABASEHOST" --user="$DATABASEUSER" --password="$DATABASEPASSWORD" --single-transaction "$DATABASESCHEMA" > "$LOCALDIR"/"$(date +%Y%m%d)_bkp_$DATABASESCHEMA.sql"
#echo "Generating md5sum"
md5sum "$LOCALDIR"/* > "$LOCALDIR"/checklist.chk
#echo "Compressing the dump and checklist"
tar -cvzf "$TEMPDIR"/$(date +%Y%m%d)"_"$DATABASEENV"_"$DATABASESCHEMA".tar.gz" "$LOCALDIR"/*
#echo "Sending the compressed file to d2"
## scp "$TEMPDIR"/"$DATABASESCHEMA".tar.gz "$BACKUPHOST":"$REMOTEDIR"
#echo "Removing generated files"
#rm "$LOCALDIR"/checklist.chk > /dev/null 2>&1
#rm "$LOCALDIR"/"$(date +%Y%m%d)_bkp_$DATABASESCHEMA.sql" > /dev/null 2>&1
#rm "$TEMPDIR"/"$DATABASESCHEMA".tar.gz > /dev/null 2>&1
rm "$LOCALDIR"/"$(date +%Y%m%d)_bkp_$DATABASESCHEMA.sql"
echo $(date +%H:%M)
substitua host, usuário, senha, banco de dados, LOCALDIR, BACLUPHOST, REMOTEDIR pelos seus próprios parâmetros