#!/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 "
" >> $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