diff --git a/Makefile b/Makefile index f0080b6..519d9d7 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,10 @@ BACKUP_ENABLED ?= no DB_NAME ?= template1 # Cron args BACKUP_CRON ?= 10 5 * * * +# The address to which error messages will be sent +# to change address you need delete file /etc/cron.d/backup, +# change address and update deploy +EMAIL_ADMIN ?= admin@domain.local # dcape container name prefix DCAPE_PROJECT_NAME ?= dcape @@ -26,6 +30,10 @@ BACKUP_ENABLED=$(BACKUP_ENABLED) DB_NAME=$(DB_NAME) # Cron args BACKUP_CRON=$(BACKUP_CRON) +# The address to which error messages will be sent +# to change address you need delete file /etc/cron.d/backup, +# change address and update deploy +EMAIL_ADMIN=$(EMAIL_ADMIN) # dcape postgresql container name DCAPE_DB=$(DCAPE_DB) @@ -39,21 +47,49 @@ export CONFIG_DEF define EXP_SCRIPT [[ "$$DCAPE_DB_DUMP_DEST" ]] || { echo "DCAPE_DB_DUMP_DEST not set. Exiting" ; exit 1 ; } ; \ +WEEK_PARITY=$$(($$(date +%U) %2)); \ +DAY_TO_PROC=4; \ +DAY_OF_MONTH=$$(date +%d); \ +DAY_OF_WEEK=$$(date +%u); \ +MONTH_TO_KEEP=(1 * 30); \ +WEEKS_TO_KEEP=(2 * 7); \ +DAYS_TO_KEEP=3; \ +find $$DCAPE_DB_DUMP_DEST -type f -mtime +$$MONTH_TO_KEEP -name "*-monthly.tgz" | xargs --no-run-if-empty 'rm -f' ';' \ +find $$DCAPE_DB_DUMP_DEST -type f -mtime +$$WEEKS_TO_KEEP -name "*-weekly.tgz" | xargs --no-run-if-empty 'rm -f' ';' \ +find $$DCAPE_DB_DUMP_DEST -type f -mtime +$$DAYS_TO_KEEP -name "*-daily.tgz" | xargs --no-run-if-empty 'rm -f' ';' \ DBS=$$@ ; \ [[ "$$DBS" ]] || DBS=all ; \ dt=$$(date +%y%m%d) ; \ if [[ $$DBS == "all" ]] ; then \ echo "Exporting all databases..." ; \ - DBS=$$(psql --tuples-only -P format=unaligned -U postgres \ + DBS=$$(psql --tuples-only -P format=unaligned -U postgres \ -c "SELECT datname FROM pg_database WHERE NOT datistemplate AND datname <> 'postgres'") ; \ fi ; \ -echo "Backup DBs: $$DBS" ; \ for d in $$DBS ; do \ - dest=$$DCAPE_DB_DUMP_DEST/$${d%%.*}-$${dt}.tgz ; \ - echo -n $${dest}... ; \ - [ -f $$dest ] && { echo Skip ; continue ; } ; \ + echo -n "Make daily backup DBs: $$DBS" ; \ + dest=$$DCAPE_DB_DUMP_DEST/$${d%%.*}-$${dt}-daily.tgz ; \ + echo -n "$${dest}..." ; \ + [ -f $$dest ] && { echo Exist ; } ; \ pg_dump -d $$d -U postgres -Ft | gzip > $$dest || echo "error" ; \ - echo Done ; \ + echo "Daily done!" ; \ + echo -n "Make weekly backup DBs: $$DBS" ; \ + dest=$$DCAPE_DB_DUMP_DEST/$${d%%.*}-$${dt}-weekly.tgz ; \ + echo -n "$${dest}..." ; \ + [ -f $$dest ] && { echo Exist ; } ; \ + if [ $$WEEK_PARITY == "0" ]; then \ + if [[ $$DAY_OF_WEEK == $$DAY_TO_PROC ]]; then \ + pg_dump -d $$d -U postgres -Ft | gzip > $$dest || echo "error" ; \ + echo "Weekly done!" ; \ + fi; \ + fi; \ + echo -n "Make monthly backup DBs: $$DBS" ; \ + dest=$$DCAPE_DB_DUMP_DEST/$${d%%.*}-$${dt}-monthly.tgz ; \ + echo -n "$${dest}..." ; \ + [ -f $$dest ] && { echo Exist ; } ; \ + if [[ $$DAY_OF_MONTH == "03" ]]; then \ + pg_dump -d $$d -U postgres -Ft | gzip > $$dest || echo "error" ; \ + echo "Monthly done!" ; \ + fi; \ done endef export EXP_SCRIPT @@ -93,10 +129,15 @@ docker-wait: # DB operations ## Setup host system cron +## If need change the target "cron", you need delete file /etc/cron.d/backup +## otherwise: make cron -> make: nothing to do for cron cron: /etc/cron.d/backup /etc/cron.d/backup: - echo "$$BACKUP_CRON op cd $$PWD && make backup" > $@ + echo "# Set the email to wich error message will be sent" > $@ + echo "MAILTO=$$EMAIL_ADMIN" >> $@ + echo "# Set cron command with disable STDOUT for cron sent mail only if error exist" >> $@ + echo "$$BACKUP_CRON op cd $$PWD && make backup > /dev/null" >> $@ ## dump all databases or named database backup: docker-wait diff --git a/README.md b/README.md index 6832e58..f73e859 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ [5]: LICENSE Postgresql database backup application package for [dcape](https://github.com/dopos/dcape). +The service uses a cron to backup all or selected databases deployed to dcape and sends a report by e-mail in cases of an error. ## Docker image used