Compare commits

..

11 commits

6 changed files with 41 additions and 29 deletions

View file

@ -1,13 +1,3 @@
The purpose of this script is to backup the contents of Docker container-based database (either PostgreSQL or MariaDB) to a local 'dump' file (SQL format), which is then compressed to conserve disk space. The purpose of this script is to backup the contents of Docker container-based database (either PostgreSQL or MariaDB) to a local 'dump' file (SQL format), which is then compressed to conserve disk space.
The system is designed to be run regularly (perhaps hourly, or daily) via cron, and to keep a pre-determined (via configuration) number of past, deleting excess backup files to conserve disk space. The system is designed to be run regularly (perhaps hourly, or daily) via cron, and to keep a pre-determined (via configuration) number of past, deleting excess backup files to conserve disk space.
## To install
To install this script, by our convention on Docker Compose hosts, we clone it into `/home/data/scripts` (creating the directory if necessary), via `git clone git@git.oeru.org:oeru/docker-compose-dbbackup.git`.
Then, in that directory, we copy the relevant '\*.conf-sample' configuration file and copy it to a '\*.conf' file and adjust its values. You can try running the backup to test it by running (for example)
`./mariadbbackup-docker-compose --hourly`
which should, if the configuration is valid, create an hourly database backup (starting with the database name with '-hourly' affixed, e.g. )

View file

@ -4,17 +4,17 @@ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# run system backups # run system backups
# #
# hourly - at 5 minutes past mon-sat # hourly - at 5 minutes past mon-sat
25 * * * * root /home/data/mastodon.oeru.org/scripts/dbbackup/dbbackup-docker-compose -c /home/data/mastodon.oeru.org/scripts/dbbackup/mastodon.conf --hourly 25 * * * * root /home/data/scripts/docker-compose-dbbackup/dbbackup-docker-compose -c default-docker-compose.conf --hourly
# #
# daily - at 7:30 pm, mon-sat # daily - at 7:30 pm, mon-sat
30 19 * * * root /home/data/mastodon.oeru.org/scripts/dbbackup/dbbackup-docker-compose -c /home/data/mastodon.oeru.org/scripts/dbbackup/mastodon.conf --daily 30 19 * * * root /home/data/scripts/docker-compose-dbbackup/dbbackup-docker-compose -c default-docker-compose.conf --daily
# #
# weekly - at 7:30 pm, sun # weekly - at 7:30 pm, sun
30 19 * * sun root /home/data/mastodon.oeru.org/scripts/dbbackup/dbbackup-docker-compose -c /home/data/mastodon.oeru.org/scripts/dbbackup/mastodon.conf --weekly 30 19 * * sun root /home/data/scripts/docker-compose-dbbackup/dbbackup-docker-compose -c default-docker-compose.conf --weekly
# #
# monthly - at 8:30 pm, on the first of the last of the month # monthly - at 8:30 pm, on the first of the last of the month
30 20 1 * * root /home/data/mastodon.oeru.org/scripts/dbbackup/dbbackup-docker-compose -c /home/data/mastodon.oeru.org/scripts/dbbackup/mastodon.conf --monthly 30 20 1 * * root /home/data/scripts/docker-compose-dbbackup/dbbackup-docker-compose -c default-docker-compose.conf --monthly
# #
# yearly - at 8:30 pm, on the first of January. # yearly - at 8:30 pm, on the first of January.
30 20 1 1 * root /home/data/mastodon.oeru.org/scripts/dbbackup/dbbackup-docker-compose -c /home/data/mastodon.oeru.org/scripts/dbbackup/mastodon.conf--yearly 30 20 1 1 * root /home/data/scripts/docker-compose-dbbackup/dbbackup-docker-compose -c default-docker-compose.conf --yearly

View file

@ -22,14 +22,16 @@ BU_FROOT_WEEKLY=weekly
BU_FROOT_MONTHLY=monthly BU_FROOT_MONTHLY=monthly
BU_FROOT_YEARLY=yearly BU_FROOT_YEARLY=yearly
# #
# Set backup priority: -20 highest, 0 default, 19 lowest
NICE_VAL=0
# this can be overridden at invocation # this can be overridden at invocation
BU_CONF=default-docker-compose.conf BU_CONF=default-docker-compose.conf
# # output for debugging... # output for debugging... 0 = false, 1 = true
VERBOSE=0 VERBOSE=0
# #
# Stuff that should be universal for this install... # Stuff that should be universal for this install...
# where we can find this app... # where we can find this app...
#MAIN_DIR=`pwd` MAIN_DIR=/home/data/scripts/docker-compose-dbbackup
# determine today's date # determine today's date
DATE=`date '+%Y-%m-%d-%a'` DATE=`date '+%Y-%m-%d-%a'`
# determine today's date # determine today's date
@ -49,6 +51,8 @@ MAIL=`which mail`
# database dump utility # database dump utility
DC=`which docker-compose` DC=`which docker-compose`
#DEF_ARGS="-C -d -O -x" #DEF_ARGS="-C -d -O -x"
# nice
NICE=`which nice`
# #
# pattern for "ls" command to build list of # pattern for "ls" command to build list of
# pruneable backup files... # pruneable backup files...
@ -136,13 +140,13 @@ delete_old() {
do_backup() { do_backup() {
FILE=$1 FILE=$1
CONTAINER=$2 CONTAINER=$2
VER=`$DC exec -T $CONTAINER pg_config --version` VER=`$DC exec $CONTAINER pg_config --version`
verbose "Postgres version $VER - echoing to backup: $FILE" verbose "Postgres version $VER - echoing to backup: $FILE"
echo "--" > $FILE echo "--" > $FILE
echo "-- PostgreSQL Version: $VER" >> $FILE echo "-- PostgreSQL Version: $VER" >> $FILE
echo "--" >> $FILE echo "--" >> $FILE
echo "" >> $FILE echo "" >> $FILE
CMD="$DC exec -T $CONTAINER $DUMP_CMD" CMD="$NICE -${NICE_VAL} $DC exec $CONTAINER $DUMP_CMD"
verbose "doing database dump: $CMD" verbose "doing database dump: $CMD"
$CMD >> $FILE $CMD >> $FILE
} }
@ -230,7 +234,7 @@ do_backup $FILEPATH $DC_CONTAINER
message "completed backup" message "completed backup"
# compress the backup # compress the backup
message "compressing $FILEPATH" message "compressing $FILEPATH"
$GZIP $FILEPATH $NICE -${NICE_VAL} $GZIP $FILEPATH
# return to where you started from... # return to where you started from...
cd $OLD_DIR cd $OLD_DIR
# #

View file

@ -17,3 +17,18 @@ DUMP_CMD='pg_dumpall -c -U postgres'
# email address to send reports to, and subject # email address to send reports to, and subject
EMAIL=webmaster@oerfoundation.org EMAIL=webmaster@oerfoundation.org
EMAIL_SUBJ="Mastodon on OERu Open Postgres Backup Report" EMAIL_SUBJ="Mastodon on OERu Open Postgres Backup Report"
#
# Optional overrides
#
# Default retention
#BU_TO_KEEP_HOURLY=24
#BU_TO_KEEP_DAILY=7
#BU_TO_KEEP_WEEKLY=4
#BU_TO_KEEP_MONTHLY=12
#BU_TO_KEEP_YEARLY=7
#
# Set backup priority: -20 highest, 0 default, 19 lowest
#NICE_VAL=0
#
# Verbosity 0 = false, 1 = true
#VERBOSE=0

21
mariadbbackup-docker-compose Normal file → Executable file
View file

@ -15,7 +15,7 @@ BU_TO_KEEP_WEEKLY=4
BU_TO_KEEP_MONTHLY=12 BU_TO_KEEP_MONTHLY=12
BU_TO_KEEP_YEARLY=7 BU_TO_KEEP_YEARLY=7
# #
BU_FROOT=alldbs #BU_FROOT=alldbs
BU_FROOT_HOURLY=hourly BU_FROOT_HOURLY=hourly
BU_FROOT_DAILY=daily BU_FROOT_DAILY=daily
BU_FROOT_WEEKLY=weekly BU_FROOT_WEEKLY=weekly
@ -23,7 +23,7 @@ BU_FROOT_MONTHLY=monthly
BU_FROOT_YEARLY=yearly BU_FROOT_YEARLY=yearly
# #
# this can be overridden at invocation # this can be overridden at invocation
BU_CONF=default-docker-compose.conf BU_CONF=default-mariadb-docker-compose.conf
# # output for debugging... # # output for debugging...
VERBOSE=0 VERBOSE=0
# #
@ -135,9 +135,9 @@ delete_old() {
# #
do_backup() { do_backup() {
FILE=$1 FILE=$1
DATABASE="$(get_value DC_DB_NAME_VAR $DC_FILE)" DATABASE="$(get_value $DC_DB_NAME_VAR $DC_FILE)"
USER="$(get_value DC_DB_USER_VAR $DC_FILE)" USER="$(get_value $DC_DB_USER_VAR $DC_FILE)"
PASSWORD="$(get_value DC_DB_PASSWORD_VAR $DC_FILE)" PASSWORD="$(get_value $DC_DB_PASSWORD_VAR $DC_FILE)"
verbose "debug - DB details: USER = $USER, PASSWORD = $PASSWORD, DB = $DATABASE" verbose "debug - DB details: USER = $USER, PASSWORD = $PASSWORD, DB = $DATABASE"
VER=`$DC exec $DC_CONTAINER mysql --version` VER=`$DC exec $DC_CONTAINER mysql --version`
verbose "MariaDB version $VER - echoing to backup: $FILE" verbose "MariaDB version $VER - echoing to backup: $FILE"
@ -162,7 +162,7 @@ while test $# -ne 0 ; do
case $1 in case $1 in
--config|-c) --config|-c)
shift # shift from the flag to the value shift # shift from the flag to the value
verbose "setting configuration directory to $1" verbose "setting configuration file to $1"
BU_CONF=$1 BU_CONF=$1
;; ;;
--hourly|-h) --hourly|-h)
@ -191,13 +191,13 @@ done
# #
# #
# create the blank email report # create the blank email report
create_tmp_email #create_tmp_email
# #
if test -f $BU_CONF ; then if test -f $BU_CONF ; then
verbose "Reading default in $BU_CONF" verbose "Reading default in $BU_CONF"
source $BU_CONF source $BU_CONF
else else
error "Couldn't find or read $BU_CONF" message "Couldn't find or read $BU_CONF"
exit 1 exit 1
fi fi
# #
@ -212,6 +212,9 @@ cd $DC_DIR
# a timestamp for logging purposes # a timestamp for logging purposes
STAMP=`date '+%Y-%m-%d_%H-%M-%S'` STAMP=`date '+%Y-%m-%d_%H-%M-%S'`
# #
# set the root based on the name of the database...
BU_FROOT="$(get_value $DC_DB_NAME_VAR $DC_FILE)"
#
# generate the filename # generate the filename
INC="BU_FROOT_$TASK" INC="BU_FROOT_$TASK"
FILEPART=$BU_FROOT-${!INC} FILEPART=$BU_FROOT-${!INC}
@ -236,5 +239,5 @@ cd $OLD_DIR
# #
# sent resulting email report # sent resulting email report
# #
send_email_report #send_email_report
exit 0 exit 0