Skip to content

Commit c2fcb4a

Browse files
authored
Merge pull request #2 from silinternational/develop
Release 1.0.0
2 parents 459b9aa + 62d9b5f commit c2fcb4a

File tree

7 files changed

+73
-37
lines changed

7 files changed

+73
-37
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM alpine:3.6
1+
FROM alpine:3.8
22

33
RUN apk update \
44
&& apk add --no-cache rsyslog rsyslog-tls \

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ backup: db
77
docker-compose up -d backup
88

99
db:
10-
docker-compose up -d db adminer
10+
docker-compose up -d db
1111

1212
clean:
1313
docker-compose kill

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# postgresql-backup-restore
2-
Service to backup and/or restore PostgreSQL databases using S3
2+
Service to backup and/or restore a PostgreSQL database using S3
33

44
## How to use it
55
1. Create an S3 bucket to hold your backups
@@ -14,16 +14,20 @@ Service to backup and/or restore PostgreSQL databases using S3
1414

1515
`CRON_SCHEDULE="0 2 * * *"` _defaults to every day at 2:00 AM_ [syntax reference](https://en.wikipedia.org/wiki/Cron)
1616

17-
`DB_NAMES=name1 name2 name3 ...`
18-
1917
`DB_HOST=` hostname of the database server
2018

21-
`DB_USER=` user that accesses the database
22-
23-
`DB_PASSWORD=` password for the `DB_USER`
19+
`DB_NAME=` name of the database
2420

2521
`DB_OPTIONS=opt1 opt2 opt3 ...` optional arguments to supply to the backup or restore commands
2622

23+
`DB_ROOTPASSWORD=` password for the `DB_ROOTUSER`
24+
25+
`DB_ROOTUSER=` database administrative user, typically "postgres" for PostgreSQL databases
26+
27+
`DB_USERPASSWORD=` password for the `DB_USER`
28+
29+
`DB_USER=` user that accesses the database (PostgreSQL "role")
30+
2731
`AWS_ACCESS_KEY=` used for S3 interactions
2832

2933
`AWS_SECRET_KEY=` used for S3 interactions

application/backup.sh

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
#!/usr/bin/env sh
22

3-
for dbName in ${DB_NAMES}; do
4-
logger -p user.info "backing up ${dbName}..."
3+
logger -p user.info "backing up ${DB_NAME}..."
54

65
start=$(date +%s)
7-
runny $(PGPASSWORD=${DB_PASSWORD} pg_dump --host=${DB_HOST} --username=${DB_USER} --create --clean ${DB_OPTIONS} --dbname=${dbName} > /tmp/${dbName}.sql)
6+
runny $(PGPASSWORD=${DB_USERPASSWORD} pg_dump --host=${DB_HOST} --username=${DB_USER} --create --clean ${DB_OPTIONS} --dbname=${DB_NAME} > /tmp/${DB_NAME}.sql)
87
end=$(date +%s)
98

10-
logger -p user.info "${dbName} backed up ($(stat -c %s /tmp/${dbName}.sql) bytes) in $(expr ${end} - ${start}) seconds."
9+
logger -p user.info "${DB_NAME} backed up ($(stat -c %s /tmp/${DB_NAME}.sql) bytes) in $(expr ${end} - ${start}) seconds."
1110

12-
runny gzip -f /tmp/${dbName}.sql
13-
runny s3cmd put /tmp/${dbName}.sql.gz ${S3_BUCKET}
14-
# runny aws s3 cp /tmp/${dbName}.sql.gz ${S3_BUCKET}
11+
runny gzip -f /tmp/${DB_NAME}.sql
12+
runny s3cmd put /tmp/${DB_NAME}.sql.gz ${S3_BUCKET}
13+
# runny aws s3 cp /tmp/${DB_NAME}.sql.gz ${S3_BUCKET}
1514

16-
logger -p user.info "${dbName} backup stored in ${S3_BUCKET}."
17-
done
15+
logger -p user.info "${DB_NAME} backup stored in ${S3_BUCKET}."

application/entrypoint.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env sh
22

33
# hostname:port:database:username:password
4-
echo ${DB_HOST}:*:*:${DB_USER}:${DB_PASSWORD} > /root/.pgpass
4+
echo ${DB_HOST}:*:*:${DB_USER}:${DB_USERPASSWORD} > /root/.pgpass
5+
echo ${DB_HOST}:*:*:${DB_ROOTUSER}:${DB_ROOTPASSWORD} >> /root/.pgpass
56
chmod 600 /root/.pgpass
67

78
if [ "${LOGENTRIES_KEY}" ]; then

application/restore.sh

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
11
#!/usr/bin/env sh
22

3-
for dbName in ${DB_NAMES}; do
4-
logger -p user.info "restoring ${dbName}..."
3+
# Does the database exist?
4+
logger -p user.info "checking for DB ${DB_NAME}..."
5+
result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --list | grep ${DB_NAME})
6+
if [ -z "${result}" ]; then
7+
message="Database "${DB_NAME}" on host "${DB_HOST}" does not exist."
8+
logger -p 1 -t application.crit "${message}"
9+
exit 1
10+
fi
511

6-
runny s3cmd get -f ${S3_BUCKET}/${dbName}.sql.gz /tmp/${dbName}.sql.gz
7-
runny gunzip -f /tmp/${dbName}.sql.gz
12+
# Ensure the database user exists.
13+
logger -p user.info "checking for DB user ${DB_USER}..."
14+
result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --command='\du' | grep ${DB_USER})
15+
if [ -z "${result}" ]; then
16+
result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --command="create role ${DB_USER} with login password '${DB_USERPASSWORD}' inherit;")
17+
if [ "${result}" != "CREATE ROLE" ]; then
18+
message="Create role command failed: ${result}"
19+
logger -p 1 -t application.crit "${message}"
20+
exit 1
21+
fi
22+
fi
823

9-
start=$(date +%s)
10-
runny psql --host=${DB_HOST} --username=${DB_USER} ${DB_OPTIONS} < /tmp/${dbName}.sql
11-
end=$(date +%s)
24+
logger -p user.info "changing DB ownership to ${DB_USER}..."
25+
result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --command="alter database ${DB_NAME} owner to ${DB_USER};")
26+
if [ "${result}" != "ALTER DATABASE" ]; then
27+
message="Alter database command failed: ${result}"
28+
logger -p 1 -t application.crit "${message}"
29+
exit 1
30+
fi
1231

13-
logger -p user.info "${dbName} restored in $(expr ${end} - ${start}) seconds."
14-
done
32+
logger -p user.info "restoring ${DB_NAME}..."
33+
34+
runny s3cmd get -f ${S3_BUCKET}/${DB_NAME}.sql.gz /tmp/${DB_NAME}.sql.gz
35+
runny gunzip -f /tmp/${DB_NAME}.sql.gz
36+
37+
start=$(date +%s)
38+
runny psql --host=${DB_HOST} --username=${DB_USER} --dbname=${DB_NAME} ${DB_OPTIONS} < /tmp/${DB_NAME}.sql
39+
end=$(date +%s)
40+
41+
logger -p user.info "${DB_NAME} restored in $(expr ${end} - ${start}) seconds."

docker-compose.yml

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ services:
1818
environment:
1919
POSTGRES_PASSWORD: r00tp@ss!
2020

21-
adminer:
22-
image: adminer:4.6.3
23-
ports:
24-
- "8080:8080"
21+
# adminer:
22+
# image: adminer:4.6.3
23+
# ports:
24+
# - "8080:8080"
2525

2626
# DB_HOST - hostname of the database server
27+
# DB_ROOTUSER - administrative user for the database server
28+
# DB_ROOTPASSWORD - password for the DB_ROOTUSER
2729
# DB_USER - user that accesses the database
28-
# DB_PASSWORD - password for the DB_USER
29-
# DB_NAMES - list of databases to back up/restore
30+
# DB_USERPASSWORD - password for the DB_USER
31+
# DB_NAME - name of database to back up/restore
3032
restore:
3133
build: ./
3234
volumes_from:
@@ -35,9 +37,11 @@ services:
3537
- ./local.env
3638
environment:
3739
DB_HOST: db
40+
DB_ROOTUSER: postgres
41+
DB_ROOTPASSWORD: r00tp@ss!
3842
DB_USER: postgres
39-
DB_PASSWORD: r00tp@ss!
40-
DB_NAMES: world
43+
DB_USERPASSWORD: r00tp@ss!
44+
DB_NAME: world
4145
MODE: restore
4246
CRON_SCHEDULE: "25 * * * *"
4347

@@ -49,8 +53,10 @@ services:
4953
- ./local.env
5054
environment:
5155
DB_HOST: db
56+
DB_ROOTUSER: postgres
57+
DB_ROOTPASSWORD: r00tp@ss!
5258
DB_USER: postgres
53-
DB_PASSWORD: r00tp@ss!
54-
DB_NAMES: world
59+
DB_USERPASSWORD: r00tp@ss!
60+
DB_NAME: world
5561
MODE: backup
5662
CRON_SCHEDULE: "20 * * * *"

0 commit comments

Comments
 (0)