Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add email notify support #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 48 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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 \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space?

-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" ; \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не стоит нагружать БД дублированием дампа. Файл уже есть, можно его cp, а лучше - ln

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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down