add bashup-backup-postgres.sh, adjust mysql vars

This commit is contained in:
Matthew Saunders Brown 2022-12-22 10:55:01 -08:00
parent 2cb9ab35dc
commit 55f484f42d
4 changed files with 84 additions and 10 deletions

View File

@ -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

64
sbin/bashup-backup-postgres.sh Executable file
View File

@ -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

View File

@ -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

View File

@ -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