From 11c7f9b67b9bc6525a2ba99c4dd3210351659e6e Mon Sep 17 00:00:00 2001 From: Russ Long Date: Mon, 11 May 2020 14:16:49 -0400 Subject: [PATCH] Correct a couple typs and add sql backup --- b2backup.sh | 10 --------- b2mount.sh | 2 +- config-example.txt | 8 +++++++- sqlauth.txt | 1 + sqlbackup.sh | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 sqlauth.txt create mode 100644 sqlbackup.sh diff --git a/b2backup.sh b/b2backup.sh index b59a6fb..626a449 100644 --- a/b2backup.sh +++ b/b2backup.sh @@ -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 diff --git a/b2mount.sh b/b2mount.sh index 277ba1a..ecabe9c 100644 --- a/b2mount.sh +++ b/b2mount.sh @@ -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" diff --git a/config-example.txt b/config-example.txt index 6301c15..5c66933 100644 --- a/config-example.txt +++ b/config-example.txt @@ -14,4 +14,10 @@ RESTORE_DIR="/path/to/mount/snapshot/repo" 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" \ No newline at end of file +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" \ No newline at end of file diff --git a/sqlauth.txt b/sqlauth.txt new file mode 100644 index 0000000..2060f1f --- /dev/null +++ b/sqlauth.txt @@ -0,0 +1 @@ +container_name:db_username:db_password \ No newline at end of file diff --git a/sqlbackup.sh b/sqlbackup.sh new file mode 100644 index 0000000..c4f2e24 --- /dev/null +++ b/sqlbackup.sh @@ -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 "
" >> $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