From 55f484f42dd247f80d54386a1c4fa63aa0bddc6b Mon Sep 17 00:00:00 2001 From: Matthew Saunders Brown Date: Thu, 22 Dec 2022 10:55:01 -0800 Subject: [PATCH] add bashup-backup-postgres.sh, adjust mysql vars --- sbin/bashup-backup-mysql.sh | 10 +++--- sbin/bashup-backup-postgres.sh | 64 ++++++++++++++++++++++++++++++++++ sbin/bashup-restore-mysql.sh | 4 +-- sbin/bashup.sh | 16 +++++++-- 4 files changed, 84 insertions(+), 10 deletions(-) create mode 100755 sbin/bashup-backup-postgres.sh diff --git a/sbin/bashup-backup-mysql.sh b/sbin/bashup-backup-mysql.sh index 1f0c744..43ab586 100755 --- a/sbin/bashup-backup-mysql.sh +++ b/sbin/bashup-backup-mysql.sh @@ -23,18 +23,18 @@ if [[ " ${retention_array[@]} " =~ " ${today} " ]]; then if [[ ! -d $backup_storage_dir/$today/mysql ]]; then mkdir $backup_storage_dir/$today/mysql - mysqladmin --defaults-extra-file=$defaults_extra_file refresh + mysqladmin --defaults-extra-file=$mysql_defaults_extra_file refresh # create array of all existing databases - databases=($(mysql --defaults-extra-file=$defaults_extra_file -E -e 'show databases;'|grep : |awk '{ print $2 }' |tr '\n' ' ')); + databases=($(mysql --defaults-extra-file=$mysql_defaults_extra_file -E -e 'show databases;'|grep : |awk '{ print $2 }' |tr '\n' ' ')); for database in "${databases[@]}"; do - if [[ " ${exclusions[@]} " =~ " ${database} " ]]; then - # do nothing, db is in exclusions array + if [[ " ${mysql_exclusions[@]} " =~ " ${database} " ]]; then + # do nothing, db is in mysql_exclusions array one=1; else - mysqldump --defaults-extra-file=$defaults_extra_file --opt --quote-names --events --databases $database | gzip > $backup_storage_dir/$today/mysql/$database.sql.gz + mysqldump --defaults-extra-file=$mysql_defaults_extra_file --opt --quote-names --events --databases $database | gzip > $backup_storage_dir/$today/mysql/$database.sql.gz fi done diff --git a/sbin/bashup-backup-postgres.sh b/sbin/bashup-backup-postgres.sh new file mode 100755 index 0000000..68610af --- /dev/null +++ b/sbin/bashup-backup-postgres.sh @@ -0,0 +1,64 @@ +#!/bin/bash +# +# Bashup - A set of bash scripts for managing backups. +# https://git.stack-source.com/msb/bashup +# MIT License Copyright (c) 2021 Matthew Saunders Brown + +# load include file +source $(dirname $0)/bashup.sh + +bashup::set-retention_array + +# create backup for today +if [[ " ${retention_array[@]} " =~ " ${today} " ]]; then + + # create backup (date) dir if it doesn't already exist + if [ ! -d "$backup_storage_dir/$today" ]; then + + mkdir $backup_storage_dir/$today + + fi + + # only proceed if postgresql has not already been backed up + if [[ ! -d $backup_storage_dir/$today/postgres ]]; then + + install --owner=postgres --group=postgres --mode=750 --directory $backup_storage_dir/$today/postgres + + if [[ " ${pg_method[@]} " =~ " basebackup " ]]; then + + su -c "pg_basebackup --pgdata=- --format=tar --gzip --wal-method=fetch > $backup_storage_dir/$today/postgres/pg_basebackup.sql.tar.gz" postgres + + fi + + if [[ " ${pg_method[@]} " =~ " dump " ]]; then + + pg_databases=(`su -c "psql --command='SELECT datname FROM pg_database;' --csv|grep -v datname" postgres|tr '\n' ' '`) + + for database in "${pg_databases[@]}"; do + + if [[ " ${pg_dump_exclusions[@]} " =~ " ${database} " ]]; then + # do nothing, db is in pg_dump_exclusions array + one=1; + else + su -c "pg_dump --clean --create $database | gzip > $backup_storage_dir/$today/postgres/$database.sql.gz" postgres + fi + + done + + + fi + + if [[ " ${pg_method[@]} " =~ " dumpall " ]]; then + + su -c "pg_dumpall --clean | gzip > $backup_storage_dir/$today/postgres/pg_dumpall.sql.gz" postgres + + fi + + fi + +fi + +bashup::remove_expired_backups pg +bashup::unmount_storage_dir + +exit 0 diff --git a/sbin/bashup-restore-mysql.sh b/sbin/bashup-restore-mysql.sh index 478c15a..275a0ab 100755 --- a/sbin/bashup-restore-mysql.sh +++ b/sbin/bashup-restore-mysql.sh @@ -101,8 +101,8 @@ fi if [ -d $backup_storage_dir/$backup ]; then if [ -f $backup_storage_dir/$backup/mysql/$dump ]; then echo "running:" - echo "/usr/bin/zcat $backup_storage_dir/$backup/mysql/$dump | mysql --defaults-extra-file=$defaults_extra_file $database" - /usr/bin/zcat $backup_storage_dir/$backup/mysql/$dump | mysql --defaults-extra-file=$defaults_extra_file $database + echo "/usr/bin/zcat $backup_storage_dir/$backup/mysql/$dump | mysql --defaults-extra-file=$mysql_defaults_extra_file $database" + /usr/bin/zcat $backup_storage_dir/$backup/mysql/$dump | mysql --defaults-extra-file=$mysql_defaults_extra_file $database else echo "ERROR: Dump for database $database does not exist in the $backup backup dir." exit 1 diff --git a/sbin/bashup.sh b/sbin/bashup.sh index 97f1a91..4264567 100755 --- a/sbin/bashup.sh +++ b/sbin/bashup.sh @@ -15,7 +15,7 @@ retention_days=7; # backup storage directory backup_storage_dir='/mnt/backups'; -# which backup scripts to run via bashup-cron.sh - options are 'files', 'mysql', 'pdns' +# which backup scripts to run via bashup-cron.sh - options are 'files', 'mysql', 'pdns', 'postgres' bashup_jobs=('files' 'mysql'); # directories to be backed up by files @@ -27,10 +27,20 @@ backup_dirs=('/etc' '/home/' '/root' '/srv/' '/usr/local' '/var/www/'); # only the subdirectories within it # mysql config file that contains 'host' 'user' 'password' vars -defaults_extra_file='/etc/mysql/debian.cnf'; +mysql_defaults_extra_file='/etc/mysql/debian.cnf'; # list of mysql databases to skip -exclusions=('information_schema' 'performance_schema' 'sys' 'wsrep'); +mysql_exclusions=('information_schema' 'performance_schema' 'sys' 'wsrep'); + +# postgres backup method - options are 'basebackup', 'dump', 'dumpall' +# basebackup makes an exact copy of the entire database cluster's files +# dump creates individual sql dumps of each database +# dumpall creates a single sql dump of all databases in one file +# multiple methods can be specified +pg_method=('basebackup' 'dump'); + +# list of postgres databases to skip when using method 'dump' +pg_dump_exclusions=('template0'); # end configurable vars