-
Notifications
You must be signed in to change notification settings - Fork 132
/
copyapp
executable file
·100 lines (84 loc) · 2.33 KB
/
copyapp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/sh
#
# copyapp <src> <dst>
#
# This script is used to copy the database and assets from one Heroku
# application to another. It is run nightly to sync the staging instance with
# the production database and media.
#
# In addition to the command line arguments, which should be the names of Heroku
# applications, the command also modifies its behaviour in response to the
# setting of two environment variables:
#
# DRYRUN - If DRYRUN is set to a nonblank value, then the command won't
# perform any actions, but instead will print details of what it
# would do.
#
# QUIET - If QUIET is set to a nonblank value, the command will not print
# any output except in the event that an error is encountered. This is
# suitable for use when running the script as a cronjob.
#
: ${DRYRUN:=}
: ${QUIET:=}
set -e
usage () {
echo "Usage: $0 <src> <dst>" >&2
}
status () {
if [ -z "$QUIET" ]; then
echo "---> ${@}" >&2
fi
}
maybe () {
if [ -n "$DRYRUN" ]; then
echo "$@"
else
"$@"
fi
}
quietly () {
local out
if [ -n "$QUIET" ]; then
if ! out=$("$@" 2>&1); then
echo "ERROR while running '$@':" >&2
echo "$out"
exit 1
fi
else
"$@"
fi
}
syncassets () {
local srcapp=$1
local dstapp=$2
local srcbucket=$(heroku config:get -a "$srcapp" AWS_STORAGE_BUCKET_NAME)
local dstbucket=$(heroku config:get -a "$dstapp" AWS_STORAGE_BUCKET_NAME)
local args="--acl public-read"
if [ -n "$DRYRUN" ]; then
args="$args --dryrun"
fi
if [ -n "$QUIET" ]; then
args="$args --quiet"
fi
aws s3 sync $args "s3://$srcbucket" "s3://$dstbucket"
}
SRC_APP=$1
DST_APP=$2
set -u
if [ "$#" -ne 2 ]; then
usage
exit 1
fi
status "making backup of source database"
maybe quietly heroku pgbackups:capture -e -a "${SRC_APP}"
status "making backup of destination database"
maybe quietly heroku pgbackups:capture -e -a "$DST_APP"
status "enabling maintenance mode on destination app"
maybe quietly heroku maintenance:on -a "$DST_APP"
status "transferring data from source to destination"
maybe quietly heroku pgbackups:transfer -a "$SRC_APP" --confirm "$SRC_APP" \
DATABASE_URL "$DST_APP"::DATABASE_URL
status "syncing asset buckets"
syncassets "$SRC_APP" "$DST_APP"
status "disabling maintenance mode on destination app"
maybe quietly heroku maintenance:off -a "$DST_APP"