Created
October 4, 2021 15:13
-
-
Save antoniosmgatto/5aeda317736d5b520ceb6a89b5cbd4f4 to your computer and use it in GitHub Desktop.
Postgres backup with gzip support and upload to S3/Digital Ocean Spaces
This file contains 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 | |
WORKSPACE_DIR="/tmp/postgres_bkp" | |
DB_URL="$1" | |
BACKUP_FILENAME="$2" | |
S3_CMD=$(which s3cmd) | |
SPACES_BUCKET="your-bucket-here" | |
if [ -z "$DB_URL" ] || [ -z "$BACKUP_FILENAME" ]; then | |
echo "You must set database connection and output filename !!!"; | |
echo "eg: ./postgres-bkp.sh DATABASE_URL backup_filename"; | |
exit 1; | |
fi | |
if [ ! -d "$WORKSPACE_DIR" ]; then | |
echo "creating workspace at $WORKSPACE_DIR" | |
mkdir -p $WORKSPACE_DIR | |
fi | |
echo "Running backup for $DB_URL." | |
DUMP_SUFFIX="$(date +'%F_%H_%M')" | |
OUTPUT_FILENAME="$BACKUP_FILENAME-$DUMP_SUFFIX.sql.gz" | |
OUTPUT_PATH="$WORKSPACE_DIR/$OUTPUT_FILENAME" | |
pg_dump $DB_URL --no-owner | gzip > "$OUTPUT_PATH" | |
echo "Sending backup to S3/Digital Ocean Spaces." | |
SPACES_PATH="$(date +"%Y")/$(date +"%m")/$OUTPUT_FILENAME" | |
$S3_CMD put "$OUTPUT_PATH" s3://"$SPACES_BUCKET/$SPACES_PATH" | |
echo "Cleaning up workspace" | |
find "$WORKSPACE_DIR" -type f -name "*.sql.gz" -exec rm -f {} \; | |
echo "Backup finished !!!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment