Correct a couple typs and add sql backup

This commit is contained in:
Russ Long 2020-05-11 14:16:49 -04:00
parent 2d6735e0fb
commit 11c7f9b67b
5 changed files with 60 additions and 12 deletions

View File

@ -11,16 +11,6 @@ export B2_ACCOUNT_ID="$B2_ACCOUNT_ID"
export B2_ACCOUNT_KEY="$B2_ACCOUNT_KEY"
export RESTIC_REPOSITORY="$RESTIC_REPOSITORY"
export RESTIC_PASSWORD_FILE="$RESTIC_PASSWORD_FILE"
#dump dbs:
#Uncomment double-commented lines to enable dumping of all MySQL databases, MySQL auth is up to you to configure.
#remove old dumps
##find /backup/sqldumps -type f -mtime +7 | xargs rm -f
#backup dbs to one massive file
##/usr/bin/mysqldump -u root --single-transaction --quick --lock-tables=false --all-databases > /backup/sqldumps/full-backup-$(date +"%Y-%m-%d_%H-%M-%S").sql
#backup dbs to separate files
##for DB in $(mysql -e 'show databases' -s --skip-column-names); do
## mysqldump $DB > "/backup/sqldumps/$DB-$(date +"%Y-%m-%d_%H-%M-%S").sql";
##done
/usr/bin/restic backup $BACKUPDATA
/usr/bin/restic forget --keep-last $KEEPLAST --keep-daily $KEEPDAILY --keep-weekly $KEEPWEEKLY --keep-monthly $KEEPMONTHLY --keep-yearly $KEEPYEARLY

View File

@ -1,5 +1,5 @@
#!/bin/bash
. /opt/b2backup/b2config.txt
. ${PWD}/config.txt
export B2_ACCOUNT_ID="$B2_ACCOUNT_ID"
export B2_ACCOUNT_KEY="$B2_ACCOUNT_KEY"

View File

@ -15,3 +15,9 @@ VM_EMAILSUB="VM Backups Complete on SERVERNAME"
VM_XML_PATH="/path/to/backup/vm/xml"
VM_SNAP_PATH="/path/to/backup/vm/snapshots
VM_KEEP_BACKUP_DAYS="7"
SQL_EMAILSUB="Database Backup Complete"
PGSQL_PATH=/path/to/sqlbackups/postgres
MYSQL_PATH=/path/to/sqlbackups/mysql
SQL_AUTH_FILE=/path/to/sqlauth.txt
PGSQL_CONTAINER_NAMES="space separated list of containers running pgsql"
MYSQL_CONTAINER_NAMES="space separated list of containers running mysql or mariadb"

1
sqlauth.txt Normal file
View File

@ -0,0 +1 @@
container_name:db_username:db_password

51
sqlbackup.sh Normal file
View File

@ -0,0 +1,51 @@
#!/bin/bash
MAILFILE="/tmp/sqlmailtxt"
SERVER_NAME=$HOSTNAME
SENDER=$(whoami)
USER="noreply"
MAILHEADER="/opt/b2backup/mailheader.txt"
MAILFOOTER="/opt/b2backup/mailfooter.txt"
. ${PWD}/config.txt
export PGSQL_CONTAINER_NAMES="$PGSQL_CONTAINER_NAMES"
export MYSQL_CONTAINER_NAMES="$MYSQL_CONTAINER_NAMES"
export PGSQL_PATH="$PGSQL_PATH"
export MYSQL_PATH="$MYSQL_PATH"
export SQL_AUTH_FILE="$SQL_AUTH_FILE"
#backup SQL Containers
##PGSQL
for pgsqlcontainer in $PGSQL_CONTAINER_NAMES; do
pgsqlusername=$(echo $pgsqlcontainer | awk -F"_" '{print $1}')
docker exec -t $pgsqlcontainer pg_dumpall -c -U $pgsqlusername | gzip > "$PGSQL_PATH"/"$pgsqlcontainer"_dump_$(date +%d-%m-%Y_%H_%M_%S).gz
done
##MySQL/Maria
for mysqlcontainer in $MYSQL_CONTAINER_NAMES; do
mysqlusername=$(grep $mysqlcontainer $SQL_AUTH_FILE | awk -F":" '{print $2}')
mysqlpassword=$(grep $mysqlcontainer $SQL_AUTH_FILE | awk -F":" '{print $3}')
mysqldbname=$(echo $mysqlcontainer $SQL_AUTH_FILE | awk -F"_" '{print $1}')
docker exec -t $mysqlcontainer mysqldump -u$mysqlusername -p$mysqlpassword $mysqldbname | gzip > "$MYSQL_PATH"/"$mysqlcontainer"_dump_$(date +%d-%m-%Y_%H_%M_%S).gz
done
for pgsqldel in $(find $PGSQL_PATH -type f -mtime +7); do
rm -f $pgsqldel
done
for mysqldel in $(find $MYSQL_PATH -type f -mtime +7); do
rm -f $mysqldel
done
echo "DB Backup Complete. Currently available backups:" > $MAILFILE
echo "<pre style='font:monospace'>" >> $MAILFILE
echo "PostgreSQL:" >> $MAILFILE
echo "$(ls -lh $PGSQL_PATH)" >> $MAILFILE
echo "MySQL/MariaDB:" >> $MAILFILE
echo "$(ls -lh $MYSQL_PATH)" >> $MAILFILE
(
echo "Subject: $SQL_EMAILSUB"
echo "From: $SENDER"
echo "To: $RECIPIENT"
cat $MAILHEADER $MAILFILE $MAILFOOTER
) | sendmail -t