From 76ccf5deaf24c0e2fdc42da1473a3188ad4c53dd Mon Sep 17 00:00:00 2001
From: wu-lee <gitcoop.wu-lee@noodlefactory.co.uk>
Date: Tue, 28 Aug 2018 16:37:09 +0100
Subject: [PATCH] mastodon_db_backup.sh - cron script for nightly backup of
 mastodon DB

A stopgap until we get a proper backup
---
 backups/mastodon_db_backup.sh | 36 +++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 backups/mastodon_db_backup.sh

diff --git a/backups/mastodon_db_backup.sh b/backups/mastodon_db_backup.sh
new file mode 100644
index 0000000..5b4fd71
--- /dev/null
+++ b/backups/mastodon_db_backup.sh
@@ -0,0 +1,36 @@
+#!/bin/bash
+
+# A script which backs up the mastodon database(s)
+
+container=mastodon_db
+backupdir=/var/vol2/postgres
+
+# This performs a cyclic dump to files with the container and date in the name
+# It also creates a link to a file containing the backup sequence number 0 or 1
+# which alternates (computed by modular division from the days since the epoch).
+# Thus by deleting the linked file we can maintain just two backup files.
+backup() {
+  local cname=$1
+  local seqno="$(( $(date +%s) / 86400 % 2 ))"
+  local link="db-backup.$seqno.sql.gz"
+  local file="db-backup.(date +%F_%R)-${cname}.sql.gz"
+
+  echo "Backing up $cname -> $file @ $(date +%F_%R)"
+  
+  # delete previous backup with same seqno, don't complain if missing
+  rm -f $(readlink -f "$backupdir/$link")
+
+  # create new backup file
+  docker exec pg_dumpall -U postgres -c -v | gzip >"$backupdir/$file"
+  status=$?
+
+  # link the sequence number to it
+  ln -sf "$backupdir/"{"$file","$link"}
+  echo "Done. (Status $status @$(date +%F_%R))"
+}
+
+# Iterate over all matching container names
+for cname in $(docker container ls -f name=$container --format '{{.Names}}')
+do 
+  echo backup $cname
+done
-- 
GitLab