forked from cnobile2012/dcolumn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump-db.sh
executable file
·65 lines (58 loc) · 1.41 KB
/
dump-db.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
#!/bin/bash
#
# Usage: -u -- DB user (default is "inventory")
# -h -- host (default is localhost)
# -e -- Prefixed environment tag (ex. dev, stg, prod, but can be longer)
# -D -- Database name (defaults to "inventory")
# -d -- Directory to put dump files (default is "db-snapshots").
#
# You will be prompted for the DB user's password.
#
# Reload data
#
# zcat db-snapshots/<env>-<date>.sql.gz | ./manage.py dbshell
#
set -e
set -u
set -o pipefail
USER="dcolumn"
HOST="localhost"
ENV=""
DB="dcolumn"
DIR="db-snapshots"
while getopts 'u:h:e:D:d:' opt; do
case ${opt} in
u )
USER="$OPTARG"
;;
h )
HOST="$OPTARG"
;;
e )
ENV="$OPTARG"
;;
D )
DB="$OPTARG"
;;
d )
DIR="$OPTARG"
;;
\? )
printf "Usage: $(basename $0) [-u <db user>] [-h <hostname>] [-e <environment tag>] [-d <directory>]\n"
exit 1
;;
esac
done
shift $((OPTIND -1))
printf "Options--user: %s, host: %s, env: %s, dir: %s\n" "$USER" "$HOST" \
"$ENV" "$DIR"
if [ "$ENV" == "" ]; then
printf "Must provide an environment i.e. dev, stg, prod\n"
exit 1
fi
if [ ! -d "$DIR" ]; then
mkdir "$DIR"
fi
pg_dump --username="$USER" -c --compress=9 -h "$HOST" "$DB" > \
"$DIR"/"$ENV"-$(date +"%Y%m%d%H%M-%s").sql.gz
exit 0