2014-11-27 00:27:17 +10:00
|
|
|
#!/bin/bash
|
2016-06-07 20:42:19 +10:00
|
|
|
# Script Name: dbbackup
|
|
|
|
# Author: Matt McKinnon
|
|
|
|
# Date: 7th June 2016
|
|
|
|
# Description:
|
|
|
|
# This script will backup your mysql databases.
|
|
|
|
# Send an email report of databases that have been backed up.
|
|
|
|
# Rotate backups for 7 days
|
|
|
|
#
|
|
|
|
# NOTE:
|
|
|
|
# A user will need to be grated permissions on the databases
|
|
|
|
# Login to mysql with your root user.
|
|
|
|
#
|
|
|
|
# CREATE USER 'dbbackup'@'localhost' IDENTIFIED BY 'PASSWORD';
|
2017-04-04 23:49:53 +10:00
|
|
|
# GRANT LOCK TABLES, SELECT, SHOW VIEW, RELOAD, REPLICATION CLIENT, EVENT, TRIGGER ON *.* TO 'dbbackup'@'localhost';
|
|
|
|
|
|
|
|
|
2017-06-15 21:06:29 +10:00
|
|
|
MAILTO="support@comprofix.com"
|
|
|
|
MAILFROM="support@comprofix.com"
|
2022-10-22 12:01:49 +10:00
|
|
|
SQLSERVER="docker.comprofix.xyz"
|
|
|
|
SMTP="comprofix-com.mail.protection.outlook.com"
|
2016-06-07 20:42:19 +10:00
|
|
|
|
|
|
|
BAKDATE=$(date +%Y%m%d)
|
2022-10-22 12:01:49 +10:00
|
|
|
SUBJECT="$SQLSERVER Database Backup Completed - $BAKDATE"
|
2014-11-27 00:27:17 +10:00
|
|
|
DBUSER='dbbackup'
|
2018-05-04 22:07:48 +10:00
|
|
|
DBPASS='MdCg8uTSEWhmv7+D'
|
2022-10-22 12:01:49 +10:00
|
|
|
BACKUPDIR='/mnt/nfs/archive/db/'
|
2014-11-27 00:27:17 +10:00
|
|
|
|
2015-06-13 14:17:16 +10:00
|
|
|
rotate_backups() {
|
2015-06-15 15:56:36 +10:00
|
|
|
find $BACKUPDIR -type f -mtime +7 -exec rm -frv {} \;
|
2015-06-13 14:17:16 +10:00
|
|
|
|
2015-06-15 15:56:36 +10:00
|
|
|
}
|
2015-06-13 14:17:16 +10:00
|
|
|
|
|
|
|
rotate_backups
|
|
|
|
|
2022-10-22 12:01:49 +10:00
|
|
|
databases=$(mysql --user=$DBUSER --password=$DBPASS --host=$SQLSERVER -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)
|
2015-12-06 16:28:04 +10:00
|
|
|
|
2014-11-27 00:27:17 +10:00
|
|
|
for db in $databases; do
|
|
|
|
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] ; then
|
2016-06-07 20:42:19 +10:00
|
|
|
echo "Dumping database: $db" >> /tmp/dbbackup.msg
|
2022-10-22 12:01:49 +10:00
|
|
|
mysqldump --force --opt --user=$DBUSER --password=$DBPASS --host=$SQLSERVER --databases $db > $BACKUPDIR/$db.$BAKDATE.sql
|
2014-11-27 00:27:17 +10:00
|
|
|
fi
|
|
|
|
|
|
|
|
done
|
2016-06-07 20:42:19 +10:00
|
|
|
|
2022-10-22 12:01:49 +10:00
|
|
|
sendemail -o tls=yes -s $SMTP -t $MAILTO -f "$SQLSERVER <$MAILFROM>" -u "$SUBJECT" -m "$(cat /tmp/dbbackup.msg)" -q
|
2017-04-04 23:49:53 +10:00
|
|
|
|
|
|
|
#Use Below to use systems postfix or local MTA
|
2022-10-22 12:01:49 +10:00
|
|
|
#cat /tmp/dbbackup.msg | mail -s "$SUBJECT" "$MAIL"
|
2016-06-07 20:42:19 +10:00
|
|
|
rm -fr /tmp/dbbackup.msg
|