-
Notifications
You must be signed in to change notification settings - Fork 30
/
restore_db.sh
executable file
·131 lines (112 loc) · 3.38 KB
/
restore_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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env bash
# NOTE: set -e cannot be used at the moment because of pg_restore will actually generate
# errors/warnings, which stops the script. A better way to handle errors must be found.
# Usage ./restore_db.sh <db_name> <host> <user_name> <password> <absolute_path_to_backup>
# or the ./restore_db.sh <absolute_path_to_backup> if the default options are ok for you
# the latest production backup can be fetched on "transport-site-postgresql" in clevercloud
#
# With the flag `--skip-extensions`, you can also skip extensions restoration as those might require administrative
# rights your pg user doesn't have. Example:
# ./restore_db.sh --skip-extensions <path_to_backup>
#
# With the flag `--preserve-contacts`, contacts and related tables won't be
# truncated. It is risky. Example:
# ./restore_db.sh --preserve-contacts <path_to_backup>
#
# With the flag `--preserve-user-feedback`, user_feedback table won't be
# truncated. It is risky. Example:
# ./restore_db.sh --preserve-contacts <path_to_backup>
#
# With the flag `--preserve-oban-jobs`, Oban jobs won't be truncated. It is
# risky. Example:
# ./restore_db.sh --preserve-oban-jobs <path_to_backup>
#
# The flags must be the first args.
should_skip_extensions=false
should_preserve_contacts=false
should_preserve_user_feedback=false
should_preserve_oban_jobs=false
function usage() {
echo "Usage:"
echo " $0 (-h|--help)"
echo " $0 [--skip-extensions] [--preserve-contacts] [--preserve-user-feedback] [--preserve-oban-jobs] (--) <absolute_path_to_backup>"
echo " $0 [--skip-extensions] [--preserve-contacts] [--preserve-user-feedback] [--preserve-oban-jobs] (--) <db_name> <host> <user_name> <password> <absolute_path_to_backup>"
exit 1
}
while true; do
case "$1" in
-h|--help)
usage
;;
--skip-extensions)
should_skip_extensions=true
shift 1
;;
--preserve-contacts)
should_preserve_contacts=true
shift 1
;;
--preserve-user-feedback)
should_preserve_user_feedback=true
shift 1
;;
--preserve-oban-jobs)
should_preserve_oban_jobs=true
shift 1
;;
--) shift;
break
;;
--* | -*)
echo "Unrecognized option \"$1\""
usage
;;
*) break;;
esac
done
if test "$#" -eq 1; then
DB_NAME="transport_repo"
USER_NAME="postgres"
BACKUP_PATH=$1
HOST="localhost"
export PGPASSWORD="postgres"
elif test "$#" -eq 5; then
DB_NAME=$1
HOST=$2
USER_NAME=$3
export PGPASSWORD=$4
BACKUP_PATH=$5
else
usage
fi
function sql() {
psql -h "$HOST" -U "$USER_NAME" -d "$DB_NAME" -c "$1"
}
if [ "$should_skip_extensions" = true ]
then
pg_restore -l "$BACKUP_PATH" -f ./pg.list
pg_restore -h "$HOST" -U "$USER_NAME" -d "$DB_NAME" --format=c --no-owner --clean --use-list ./pg.list --no-acl "$BACKUP_PATH"
else
pg_restore -h "$HOST" -U "$USER_NAME" -d "$DB_NAME" --format=c --no-owner --clean --no-acl "$BACKUP_PATH"
fi
if [ "$should_preserve_contacts" = false ]
then
echo "Truncating contact table"
sql 'TRUNCATE TABLE contact CASCADE'
fi
if [ "$should_preserve_user_feedback" = false ]
then
echo "Truncating user_feedback table"
sql 'TRUNCATE TABLE user_feedback CASCADE'
fi
if [ "$should_preserve_oban_jobs" = false ]
then
echo "Truncating oban_jobs table"
sql 'TRUNCATE TABLE oban_jobs'
fi
# Don't let database files hang around
rm "$BACKUP_PATH"
if [ "$should_skip_extensions" = true ]
then
rm ./pg.list
fi