serverscripts/dbbackup

53 lines
1.7 KiB
Plaintext
Raw Normal View History

#!/bin/bash
# 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';
# GRANT LOCK TABLES, SELECT, SHOW VIEW, RELOAD, REPLICATION CLIENT, EVENT, TRIGGER ON *.* TO 'dbbackup'@'localhost';
MAILTO="support@comprofix.com"
2024-06-21 21:50:10 +10:00
THISSERVER=$(hostname -f)
MAILFROM="$THISSERVER@comprofix.com"
SMTP="comprofix-com.mail.protection.outlook.com"
2024-06-21 21:50:10 +10:00
SQLSERVER="mysql.comprofix.xyz"
BAKDATE=$(date +%Y%m%d)
SUBJECT="$SQLSERVER Database Backup Completed - $BAKDATE"
DBUSER='dbbackup'
2024-05-06 00:27:59 +10:00
DBPASS=$(cat ~/.dbpassword)
BACKUPDIR='/mnt/nfs/archive/db/'
rotate_backups() {
2015-06-15 15:56:36 +10:00
find $BACKUPDIR -type f -mtime +7 -exec rm -frv {} \;
2015-06-15 15:56:36 +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)
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
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
fi
done
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
#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