Skip to content
Snippets Groups Projects
Commit 76ccf5de authored by wu-lee's avatar wu-lee
Browse files

mastodon_db_backup.sh - cron script for nightly backup of mastodon DB

A stopgap until we get a proper backup
parent b326f13e
No related branches found
No related tags found
No related merge requests found
#!/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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment