Implented Google Cloud Backups of the databases, and also encrypying them before being pushed to cloud. Let us hope that I didn't heck it up.

This commit is contained in:
Root User 2026-02-07 22:56:51 +01:00
parent 458d0d865a
commit 20e27f799c
Signed by: root
GPG key ID: 087F0A95E5766D72
7 changed files with 193 additions and 7 deletions

View file

@ -0,0 +1,24 @@
#!/usr/bin/env bash
set -euo pipefail
TIMESTAMP=$(date +%Y%m%d%H%M%S)
BACKUP_DIR=$(mktemp -d)
trap 'rm -rf "$BACKUP_DIR"' EXIT
# Import GPG key for encryption
gpg --batch --import "${GPG_KEY_FILE}"
DATABASES=$(psql -U postgres -t -c "SELECT datname FROM pg_database WHERE datistemplate = false AND datname != 'postgres';" | grep -v '^$')
for DB in $DATABASES; do
DB=$(echo "$DB" | xargs)
echo "Backing up PostgreSQL database: $DB"
FILENAME="pgsql_${DB}_${TIMESTAMP}.sql.gz.gpg"
if pg_dump -U postgres -d "$DB" | gzip | gpg --batch --encrypt --recipient "${GPG_RECIPIENT}" > "$BACKUP_DIR/$FILENAME"; then
gsutil cp "$BACKUP_DIR/$FILENAME" "gs://${GCS_BUCKET}/postgresql/$FILENAME"
echo "Successfully uploaded encrypted $FILENAME"
else
echo "Failed to backup $DB" >&2
exit 1
fi
done