Skip to content

Instantly share code, notes, and snippets.

@fishuke
Created January 4, 2025 04:04
Show Gist options
  • Select an option

  • Save fishuke/9a555c05c6dbe77694f3b05c44d1a6e1 to your computer and use it in GitHub Desktop.

Select an option

Save fishuke/9a555c05c6dbe77694f3b05c44d1a6e1 to your computer and use it in GitHub Desktop.
postgres-backup
#!/bin/bash
# Install AWS CLI (using apt-get for Debian-based systems)
apt-get update && apt-get install -y awscli
# Set backup name prefix if provided, otherwise use empty string
BACKUP_PREFIX="${BACKUP_NAME:+${BACKUP_NAME}_}"
# Perform the PostgreSQL dump
pg_dump --format=custom --no-acl --no-owner --username=$POSTGRES_USER $POSTGRES_DB > /tmp/backup.dump
[ $? -eq 0 ] || { echo "Error creating backup. Exiting..."; exit 1; }
# Upload to S3
aws s3 cp /tmp/backup.dump "s3://$S3_BUCKET_NAME/${BACKUP_PREFIX}backup_$(date +%F_%H-%M-%S).dump" --endpoint-url=$S3_URL
[ $? -eq 0 ] && rm /tmp/backup.dump || { echo "Error uploading backup. Exiting..."; exit 1; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment