M23: automate verified database backups
This commit is contained in:
Executable
+41
@@ -0,0 +1,41 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
destination="${BACKUP_DESTINATION:-/backups}"
|
||||
secondary="${BACKUP_SECONDARY_DESTINATION:-}"
|
||||
interval="${BACKUP_INTERVAL_SECONDS:-86400}"
|
||||
retention_days="${BACKUP_RETENTION_DAYS:-30}"
|
||||
minimum_copies="${BACKUP_MINIMUM_COPIES:-7}"
|
||||
|
||||
case "$destination" in ""|"/"|".") echo "Unsafe backup destination: $destination" >&2; exit 1;; esac
|
||||
case "$interval:$retention_days:$minimum_copies" in *[!0-9:]*|:*|*:) echo "Backup settings must be integers" >&2; exit 1;; esac
|
||||
mkdir -p "$destination"
|
||||
[ -z "$secondary" ] || mkdir -p "$secondary"
|
||||
|
||||
while true; do
|
||||
timestamp="$(date -u +%Y%m%dT%H%M%SZ)"
|
||||
target="$destination/mobilityops-$timestamp.dump"
|
||||
temporary="$target.partial"
|
||||
rm -f "$temporary"
|
||||
if pg_dump --format=custom --no-owner --no-acl \
|
||||
--host=db --username="$POSTGRES_USER" "$POSTGRES_DB" > "$temporary" \
|
||||
&& pg_restore --list "$temporary" > /dev/null; then
|
||||
mv "$temporary" "$target"
|
||||
sha256sum "$target" > "$target.sha256"
|
||||
if [ -n "$secondary" ]; then
|
||||
cp "$target" "$target.sha256" "$secondary/"
|
||||
fi
|
||||
date -u +%Y-%m-%dT%H:%M:%SZ > "$destination/latest-success"
|
||||
BACKUP_RETENTION_DAYS="$retention_days" BACKUP_MINIMUM_COPIES="$minimum_copies" \
|
||||
/opt/mobilityops/prune-postgres-backups.sh "$destination"
|
||||
if [ -n "$secondary" ]; then
|
||||
BACKUP_RETENTION_DAYS="$retention_days" BACKUP_MINIMUM_COPIES="$minimum_copies" \
|
||||
/opt/mobilityops/prune-postgres-backups.sh "$secondary"
|
||||
fi
|
||||
echo "Verified database backup: $target"
|
||||
else
|
||||
rm -f "$temporary"
|
||||
echo "Database backup failed at $timestamp" >&2
|
||||
fi
|
||||
sleep "$interval"
|
||||
done
|
||||
Reference in New Issue
Block a user