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"
|
2024-05-06 00:27:59 +10:00
|
|
|
SQLSERVER="mysql.comprofix.xyz"
|
2022-10-22 12:01:49 +10:00
|
|
|
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'
|
2024-05-06 00:27:59 +10:00
|
|
|
DBPASS=$(cat ~/.dbpassword)
|
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
|
|
|
|
|
2024-05-06 00:27:59 +10:00
|
|
|
databases=$(mariadb --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
|
2024-05-06 00:27:59 +10:00
|
|
|
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != "sys" ]] && [[ "$db" != "test" ]] && [[ "$db" != _* ]] ; then
|
2016-06-07 20:42:19 +10:00
|
|
|
echo "Dumping database: $db" >> /tmp/dbbackup.msg
|
2024-05-06 00:27:59 +10:00
|
|
|
mariadb-dump --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
|
|
|
|
2024-05-06 00:27:59 +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
|
2024-05-06 00:27:59 +10:00
|
|
|
#cat /tmp/dbbackup.msg #| mail -s "$SUBJECT" "$MAIL"
|
|
|
|
#rm -fr /tmp/dbbackup.msg
|