forked from etalab/transport-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore_db.sh
executable file
·105 lines (88 loc) · 2.61 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
#!/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-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_oban_jobs=false
function usage() {
echo "Usage:"
echo " $0 (-h|--help)"
echo " $0 [--skip-extensions] [--preserve-oban-jobs] (--) <absolute_path_to_backup>"
echo " $0 [--skip-extensions] [--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-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
echo "Truncating contact table"
sql 'TRUNCATE TABLE contact CASCADE'
echo "Truncating user_feedback table"
sql 'TRUNCATE TABLE user_feedback CASCADE'
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