From cf008cd284312b884ba94e18d273fca7e4d5dd25 Mon Sep 17 00:00:00 2001 From: bel Date: Sun, 24 May 2020 03:16:02 -0600 Subject: [PATCH] Try restoring many --- Dockerfile | 4 ++-- backup.sh | 12 ++++++++++++ restore.sh | 9 +++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100755 backup.sh create mode 100755 restore.sh diff --git a/Dockerfile b/Dockerfile index 0786460..35a520c 100755 --- a/Dockerfile +++ b/Dockerfile @@ -39,12 +39,12 @@ ENV RUN_MIGRATIONS=0 ENV CREATE_ADMIN=0 COPY --from=builder /pause/pauser /pauser +COPY backup.sh / +COPY restore.sh / USER root ### CRON BACKUPS RUN true \ - && echo 'b=$(date +%Y%m%d%H%M%S); mkdir -p /mnt/save/$b; pg_dump $DATABASE_URL --clean > /mnt/save/$b/pg.dump || (rm -rf /mnt/save/$b; echo "backup failed; deleting $b"; exit 1); if du -sh /mnt/save/$b | grep -i "^[ ]*4.0K"; then rm -rf /mnt/save/$b; echo rm empty backup $b; fi' > /backup.sh \ - && echo 'b=$(find /mnt/save -type f | sort | tail -n 1); if [ -n "$b" ]; then echo restoring $b; psql $DATABASE_URL < "$b"; fi && service postgresql start;' > /restore.sh \ && echo '0 4,8,12,16,20 * * * bash /backup.sh >> /var/log/cronj.log 2>&1' > /etc/cron.d/backups \ && echo '' >> /etc/cron.d/backups \ && chmod 0644 /etc/cron.d/backups \ diff --git a/backup.sh b/backup.sh new file mode 100755 index 0000000..976f7f8 --- /dev/null +++ b/backup.sh @@ -0,0 +1,12 @@ +b=$(date +%Y%m%d%H%M%S) +mkdir -p /mnt/save/$b +pg_dump $DATABASE_URL --clean > /mnt/save/$b/pg.dump \ + || ( + rm -rf /mnt/save/$b + echo "backup failed; deleting $b" + exit 1 + ) +if du -sh /mnt/save/$b | grep -Ei "^[ ]*(4|20).0K"; then + rm -rf /mnt/save/$b + echo rm empty backup $b +fi diff --git a/restore.sh b/restore.sh new file mode 100755 index 0000000..ff330fe --- /dev/null +++ b/restore.sh @@ -0,0 +1,9 @@ +for b in $(find /mnt/save -type f | sort | tail -n 1); do + if [ -n "$b" ]; then + echo restoring $b + if psql $DATABASE_URL < "$b"; then + break + fi + fi +done +service postgresql start