Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Add a backup script #1357

Merged
merged 2 commits into from
Sep 4, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env bash

# Fail on error.
# ==============

set -e


# Be somewhere predictable.
# =========================

cd "`dirname $0`"


# Helpers
# =======

confirm () {
proceed=""
while [ "$proceed" != "y" ]; do
read -p"$1 (y/N) " proceed
if [ "$proceed" == "n" -o "$proceed" == "N" -o "$proceed" == "" ]
then
return 1
fi
done
return 0
}

require () {
if [ ! `which $1` ]; then
echo "The '$1' command was not found."
return 1
fi
return 0
}

get_filepath () {
TODAY="`date +%F`"
CANDIDATE="$DIRPATH/$TODAY.psql"
if [ -f "$CANDIDATE" ]
then
echo " $CANDIDATE" >&2
for x in {a..z}
do
CANDIDATE="$DIRPATH/$TODAY$x.psql"
if [ ! -f "$CANDIDATE" ]
then
break
fi
echo " $CANDIDATE" >&2
done
if [ -f "$CANDIDATE" ]
then
CANDIDATE=""
else
echo "----> $CANDIDATE" >&2
echo >&2
fi
fi
echo "$CANDIDATE"
}


# Work
# ====

require foreman
require heroku
require pg_dump

DIRPATH="../backups"
FILEPATH=$(get_filepath)

if [ "$FILEPATH" = "" ]
then
exit "Too many backups!"
fi

confirm "Backup the Gittip database to $FILEPATH?"
if [ $? -eq 0 ]; then
export PGSSLMODE=require
heroku config -s -a gittip \
| foreman run -e /dev/stdin ./configure-pg-env.sh \
| foreman run -e /dev/stdin pg_dump > $FILEPATH
fi
42 changes: 42 additions & 0 deletions configure-pg-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bash

# Parse DATABASE_URL
# ==================
# We will export PG* envvars based on the contents of DATABASE_URL. For
# envvars, see http://www.postgresql.org/docs/current/static/libpq-envars.html
# I committed this but @pjz wrote it: https://gist.github.com/pjz/5855367.

if [ "$DATABASE_URL" = "" ]; then
echo "Please set DATABASE_URL, perhaps by sourcing default_tests.env or something.";
exit 1; fi

# remove the protocol
url=`echo $DATABASE_URL | sed -e s,postgres://,,g`

# extract the user (if any)
userpass="`echo $url | grep @ | cut -d@ -f1`"
pass=`echo $userpass | grep : | cut -d: -f2`
if [ -n "$pass" ]; then
user=`echo $userpass | grep : | cut -d: -f1`
else
user=$userpass
fi

# extract the host
hostport=`echo $url | sed -e s,$userpass@,,g | cut -d/ -f1`
port=`echo $hostport | grep : | cut -d: -f2`
if [ -n "$port" ]; then
host=`echo $hostport | grep : | cut -d: -f1`
else
host=$hostport
fi

# extract the path (if any)
path="`echo $url | grep / | cut -d/ -f2-`"

# echo envvars for psql
echo PGUSER=$user
echo PGPASSWORD=$pass
echo PGHOST=$host
echo PGPORT=$port
echo PGDATABASE=$path
48 changes: 5 additions & 43 deletions recreate-schema.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,11 @@ set -e
# usage: DATABASE_URL=postgres://foo:bar@baz:5234/buz recreate-schema.sh


# Parse DATABASE_URL
# ==================
# We will export PG* envvars based on the contents of DATABASE_URL. For
# envvars, see http://www.postgresql.org/docs/current/static/libpq-envars.html
# I committed this but @pjz wrote it: https://gist.github.com/pjz/5855367.

if [ "$DATABASE_URL" = "" ]; then
echo "Please set DATABASE_URL, perhaps by sourcing default_tests.env or something.";
exit 1; fi

# remove the protocol
url=`echo $DATABASE_URL | sed -e s,postgres://,,g`

# extract the user (if any)
userpass="`echo $url | grep @ | cut -d@ -f1`"
pass=`echo $userpass | grep : | cut -d: -f2`
if [ -n "$pass" ]; then
user=`echo $userpass | grep : | cut -d: -f1`
else
user=$userpass
fi

# extract the host
hostport=`echo $url | sed -e s,$userpass@,,g | cut -d/ -f1`
port=`echo $hostport | grep : | cut -d: -f2`
if [ -n "$port" ]; then
host=`echo $hostport | grep : | cut -d: -f1`
else
host=$hostport
fi

# extract the path (if any)
path="`echo $url | grep / | cut -d/ -f2-`"

# export envvars for psql
export PGUSER=$user
export PGPASSWORD=$pass
export PGHOST=$host
export PGPORT=$port
export PGDATABASE=$path

echo 'PG environment variables:'
env | grep ^PG
# Configure the Postgres environment.
# ===================================

export `./configure-pg-env.sh`



echo "=============================================================================="
Expand Down