Skip to content

Instantly share code, notes, and snippets.

@nathanl
Last active July 21, 2017 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanl/d774c7f40c5c97f97b397150e214bbf9 to your computer and use it in GitHub Desktop.
Save nathanl/d774c7f40c5c97f97b397150e214bbf9 to your computer and use it in GitHub Desktop.
Heroku backup and local import (assumes PostgreSQL, and uses a couple of Elixir Ecto commands)
#!/bin/sh
set -e
HEROKU_APP_NAME=someapp
DEV_DB_NAME=someapp_dev
LOCAL_BACKUP_FOLDER=tmp
LOCAL_BACKUP_LOCATION=$LOCAL_BACKUP_FOLDER/$HEROKU_APP_NAME-production-dump-$(date +"%Y-%m-%dT%H:%M")
echo
read -p "Make and download a fresh backup of production? [y/n]" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
# Create the backup on heroku
heroku pg:backups:capture --app $HEROKU_APP_NAME
# Download it
curl -o $LOCAL_BACKUP_LOCATION `heroku pg:backups public-url --app $HEROKU_APP_NAME | cat`
else
echo
read -p "Download the latest existing backup of production? [y/n]" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
curl -o $LOCAL_BACKUP_LOCATION `heroku pg:backups public-url --app $HEROKU_APP_NAME | cat`
fi
fi
LATEST_BACKUP=$(ls -r $LOCAL_BACKUP_FOLDER/$HEROKU_APP_NAME-production-dump* | head -1)
echo
read -p "Drop and restore $DEV_DB_NAME to match latest production backup in $LOCAL_BACKUP_FOLDER, which is $LATEST_BACKUP? [y/n]" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
mix ecto.drop
mix ecto.create
echo "\n"
echo "restoring from $LATEST_BACKUP"
#
# # Restore the backup
pg_restore --verbose --no-acl --no-owner -h 127.0.0.1 -U `whoami` -d $DEV_DB_NAME $LATEST_BACKUP
echo "\nimport completed successfully - consider cleaning up backups in $LOCAL_BACKUP_FOLDER "
fi
set +e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment