Skip to content

Instantly share code, notes, and snippets.

@SalvadorP
Last active August 29, 2015 14:25
Show Gist options
  • Save SalvadorP/e99e58b6e2752cd3f8dd to your computer and use it in GitHub Desktop.
Save SalvadorP/e99e58b6e2752cd3f8dd to your computer and use it in GitHub Desktop.
Bash script to backup databases, sends an email with info about the results, admits one parameter if you want to backup only one specific database
#!/bin/bash
DATABASES=(db1 db2 dbN)
DATE=$(date +"%Y-%m-%d")
USER=''
PASSWORD=''
DIR='/the/backup/route'
if [ ! -d "$DIR" ]; then
mkdir $DIR
fi
if [ -n "$1" ]; then
DATABASES=($1)
fi
echo BACKUP OF DB AT $DATE >> $DIR/results.txt
for i in "${DATABASES[@]}"; do
DB=${i}
FILENAME=backup-$DB-$DATE.sql
FILE=$DIR/$FILENAME
mysqldump --user=$USER --password="$PASSWORD" $DB > "$FILE"
tar -cvzf $FILE.tgz $FILE
rm $FILE
RESULT='';
if [ -f "$FILE.tgz" ]; then
RESULT="$DB Backup Complete and compression done"
fi
echo $RESULT >> $DIR/results.txt
done
# erase backups older than one week
find $DIR/* -mtime +7 -exec rm {} \;
# send an email with the results.txt
# mail -s "DB BACKUPS $DATE" [email protected] <<< $DIR/results.txt
# echo "Subject: DB BACKUPS $DATE" | cat - $DIR/results.txt | sendmail -F [email protected] -t [email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment