Skip to content

Instantly share code, notes, and snippets.

@israelst
Last active May 20, 2025 17:16
Show Gist options
  • Save israelst/ce2677c85b6b951411a427b98d56d9df to your computer and use it in GitHub Desktop.
Save israelst/ce2677c85b6b951411a427b98d56d9df to your computer and use it in GitHub Desktop.
This script loops through all Dokku Postgres services, exports each database, compresses the output, and saves it to a timestamped file in a dedicated backup directory.
#!/bin/bash
# ----------------------------------------
# Dokku PostgreSQL Backup Script
# ----------------------------------------
# This script loops through all Dokku Postgres services,
# exports each database, compresses the output, and saves it
# to a timestamped file in a dedicated backup directory.
#
# Author: Israel Teixeira <[email protected]>
# ----------------------------------------
# Exit on any error
set -e
# Set backup directory
BACKUP_DIR="$HOME/dokku-pg-backups"
# Create the backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"
# Get the current date
DATE=$(date +%Y-%m-%d-%H-%M)
# List all Dokku Postgres services quietly (just the names)
SERVICES=$(dokku postgres:list --quiet)
echo "Starting backup of Dokku PostgreSQL databases..."
echo "Backup directory: $BACKUP_DIR"
echo "Date: $DATE"
echo
# Loop over each Postgres service and back it up
for SERVICE in $SERVICES; do
echo "Backing up service: $SERVICE"
# Export the database and compress the output
dokku postgres:export "$SERVICE" | gzip > "$BACKUP_DIR/${SERVICE}-${DATE}.sql.gz"
echo "Saved: $BACKUP_DIR/${SERVICE}-${DATE}.sql.gz"
echo
done
echo "✅ All backups completed successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment