Database Backups - YourDeveloperFriend/choochoo GitHub Wiki

Right now, the entire database is on the same EC2 server as the API server. I have a simple set up for creating database backups. I created an S3 bucket for backups called choochoo-db-dumps and wrote a script that can be found at the root of the postgres user's directory. The entire content of the file can be found below:

filename="choochoo-$(date +"%Y_%m_%d_%I_%M_%p").dump"
file="/home/postgres/dumps/${filename}"
folder="$1"

set -e

if [ "$folder" = "hourly" ]; then
  expires="+2 days"
elif [ "$folder" = "daily" ]; then
  expires="+14 days"
else
  echo "must set hourly or daily"
  exit 1
fi

remote_file="s3://choochoo-db-dumps/dumps/$folder/$filename"

pg_dump -F c aos > "$file"
aws s3 cp   --expires "$(date -d "$expires" --utc +'%Y-%m-%dT%H:%M:%SZ')" "$file" "$remote_file"
rm "$file"
echo "success"

I set up a crontab to run this daily (with a 14 day expiration) and hourly (with a 2 day expiration).