forked from gratipay/gratipay.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
executable file
·83 lines (67 loc) · 1.39 KB
/
backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/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 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
pg_dump `heroku config:get DATABASE_URL -a gittip` > $FILEPATH
fi