-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathpg_restore-from-s3.sh
executable file
·49 lines (39 loc) · 1.08 KB
/
pg_restore-from-s3.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
#!/usr/bin/env bash
set -e
# Set current directory
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Import config file
if [ -f "${HOME}/.pg_dump-to-s3.conf" ]; then
source ${HOME}/.pg_dump-to-s3.conf
else
source $DIR/.conf
fi
# Usage
__usage="
USAGE:
$(basename $0) [db target] [s3 object]
EXAMPLE
$(basename $0) service 2023-06-28-at-10-29-44_service.dump
"
if [[ -z "$@" ]]; then
echo "$__usage"
exit 0
fi
# Download backup from s3
aws s3 cp s3://$S3_PATH/$2 /tmp/$2
# Create database if not exists
DB_EXISTS=$(psql -h $PG_HOST -p $PG_PORT -U $PG_USER -d postgres -tAc "SELECT 1 FROM pg_database WHERE datname='$1'")
if [ "$DB_EXISTS" = "1" ]
then
echo "Database $1 already exists, skipping creation"
# Restore database
pg_restore -h $PG_HOST -U $PG_USER -p $PG_PORT -d $1 -Fc --clean /tmp/$2
else
echo "Creating database $1"
createdb -h $PG_HOST -p $PG_PORT -U $PG_USER -T template0 $1
# Restore database
pg_restore -h $PG_HOST -U $PG_USER -p $PG_PORT -d $1 -Fc /tmp/$2
fi
# Remove backup file
rm /tmp/$2
echo "$2 restored to database $1"