Compare commits

..

1 commit

Author SHA1 Message Date
Dave Lane
1bef97e05c Update README.md 2022-06-09 07:21:28 +00:00
6 changed files with 29 additions and 41 deletions

View file

@ -1,3 +1,13 @@
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.
## 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
#
# hourly - at 5 minutes past mon-sat
25 * * * * root /home/data/scripts/docker-compose-dbbackup/dbbackup-docker-compose -c default-docker-compose.conf --hourly
25 * * * * root /home/data/mastodon.oeru.org/scripts/dbbackup/dbbackup-docker-compose -c /home/data/mastodon.oeru.org/scripts/dbbackup/mastodon.conf --hourly
#
# daily - at 7:30 pm, mon-sat
30 19 * * * root /home/data/scripts/docker-compose-dbbackup/dbbackup-docker-compose -c default-docker-compose.conf --daily
30 19 * * * root /home/data/mastodon.oeru.org/scripts/dbbackup/dbbackup-docker-compose -c /home/data/mastodon.oeru.org/scripts/dbbackup/mastodon.conf --daily
#
# weekly - at 7:30 pm, sun
30 19 * * sun root /home/data/scripts/docker-compose-dbbackup/dbbackup-docker-compose -c default-docker-compose.conf --weekly
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
#
# monthly - at 8:30 pm, on the first of the last of the month
30 20 1 * * root /home/data/scripts/docker-compose-dbbackup/dbbackup-docker-compose -c default-docker-compose.conf --monthly
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
#
# yearly - at 8:30 pm, on the first of January.
30 20 1 1 * root /home/data/scripts/docker-compose-dbbackup/dbbackup-docker-compose -c default-docker-compose.conf --yearly
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

View file

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

View file

@ -17,18 +17,3 @@ DUMP_CMD='pg_dumpall -c -U postgres'
# email address to send reports to, and subject
EMAIL=webmaster@oerfoundation.org
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 Executable file → Normal file
View file

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