forked from kartoza/docker-postgis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore.sh
56 lines (46 loc) · 1.68 KB
/
restore.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
#!/bin/bash
# stop on errors
set -e
# we might run into trouble when using the default `postgres` user, e.g. when dropping the postgres
# database in restore.sh. Check that something else is used here
if [ "$POSTGRES_USER" == "postgres" ]
then
echo "restoring as the postgres user is not supported, make sure to set the POSTGRES_USER environment variable"
exit 1
fi
# export the postgres password so that subsequent commands don't ask for it
export PGPASSWORD=$POSTGRES_PASSWORD
# check that we have an argument for a filename candidate
if [[ $# -eq 0 ]] ; then
echo 'usage:'
echo ' docker-compose run postgres restore <backup-file>'
echo ''
echo 'to get a list of available backups, run:'
echo ' docker-compose run postgres list-backups'
exit 1
fi
# set the backupfile variable
BACKUPFILE=/backups/$1
# check that the file exists
if ! [ -f $BACKUPFILE ]; then
echo "backup file not found"
echo 'to get a list of available backups, run:'
echo ' docker-compose run postgres list-backups'
exit 1
fi
echo "beginning restore from $1"
echo "-------------------------"
# delete the db
# deleting the db can fail. Spit out a comment if this happens but continue since the db
# is created in the next step
echo "deleting old database $POSTGRES_DB"
if dropdb -h postgres -U $POSTGRES_USER $POSTGRES_DB
then echo "deleted $POSTGRES_DB database"
else echo "database $POSTGRES_DB does not exist, continue"
fi
# create a new database
echo "creating new database $POSTGRES_DB"
createdb -h postgres -U $POSTGRES_USER $POSTGRES_DB -O $POSTGRES_USER
# restore the database
echo "restoring database $POSTGRES_DB"
gunzip -c $BACKUPFILE | psql -h postgres -U $POSTGRES_USER