-
Notifications
You must be signed in to change notification settings - Fork 1
/
restore_backup.sh
executable file
·50 lines (37 loc) · 1.2 KB
/
restore_backup.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
#!/bin/bash
# Correctly configure aws cli before running this script
# `aws configure`
CPU_COUNT=4
DB_CONTAINER_ID=$(docker-compose ps -q db)
BACKUP_NAME="${BACKUP_NAME:-unknown-backup}"
echo 'The script restores the default state of the' \
'public schema, which will delete all data'
while true; do
echo "Continue? (yes/no) "
read -r yn
case $yn in
yes ) echo 'start of dump recovery';
break;;
no ) echo 'exiting...';
exit 1;;
* ) echo 'invalid response';;
esac
done
# download dump
aws s3 cp s3://palladium-backup/"$BACKUP_NAME" .
# reset the public schema & define access privileges
docker exec \
-u postgres \
"$DB_CONTAINER_ID" \
psql -c \
'DROP SCHEMA public CASCADE; CREATE SCHEMA public; GRANT ALL ON SCHEMA public TO postgres; GRANT ALL ON SCHEMA public TO public;'
# restore pg_backup
docker cp "$BACKUP_NAME" "$DB_CONTAINER_ID":/tmp/
docker exec "$DB_CONTAINER_ID" \
pg_restore \
-j $CPU_COUNT \
-U postgres \
-d postgres \
/tmp/"$BACKUP_NAME"
docker exec "$DB_CONTAINER_ID" rm /tmp/"$BACKUP_NAME"
rm "$BACKUP_NAME"