Last active
March 27, 2025 16:59
-
-
Save garyhtou/fa3cbee4a09a8869dbd743b1d2b962ce to your computer and use it in GitHub Desktop.
Bash script to download all Postgres backups from Heroku
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 1. Make sure you're signed into Heroku: | |
# heroku login | |
# 2. You can estimate the number of backups that will be downloaded by running: | |
# heroku pg:backups --app "your app name here" --verbose | grep -E 'b[0-9]+' | wc -l | |
# 3. Download this script and run: | |
# chmod +x download_backups.sh | |
# ./download_backups.sh | |
# Set the app name | |
APP_NAME="your app name here" | |
# List all backups and extract the backup ID and created_at timestamp, process them | |
heroku pg:backups --app "$APP_NAME" --verbose | grep -E 'b[0-9]+' | while read -r line; do | |
# Extract backup ID (e.g., b736) and timestamp (e.g., 2025-03-26 07:05:43) | |
BACKUP_ID=$(echo "$line" | awk '{print $1}') | |
CREATED_AT=$(echo "$line" | awk '{print $2, $3}') | |
# Convert the timestamp to a filename-friendly format (e.g., 2025-03-26-070543) | |
TIMESTAMP=$(echo "$CREATED_AT" | sed 's/ /-/g' | sed 's/://g' | sed 's/\+0000//') | |
# Construct the filename (e.g., backup_b736-2025-03-26-070543.dump) | |
FILENAME="backup_${BACKUP_ID}_${TIMESTAMP}.dump" | |
# Check if the file already exists | |
if [ -f "$FILENAME" ]; then | |
echo "Skipping existing backup: $FILENAME" | |
continue | |
fi | |
echo "Downloading backup: $BACKUP_ID" | |
echo "Saving as: $FILENAME" | |
# Download the backup with the custom filename | |
heroku pg:backups:download "$BACKUP_ID" --app "$APP_NAME" -o "$FILENAME" | |
done | |
echo "All backups have been downloaded." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment