Skip to content

Commit b41486a

Browse files
authored
Merge pull request #4 from silinternational/develop
fix DB restore error
2 parents 5d6aac7 + 29e083e commit b41486a

File tree

5 files changed

+38
-48
lines changed

5 files changed

+38
-48
lines changed

Dockerfile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
FROM alpine:3.11
1+
FROM alpine:3.15
22

33
RUN apk update \
44
&& apk add --no-cache \
55
bash \
6-
postgresql \
7-
postgresql-client \
8-
python py-pip \
6+
postgresql12-client \
7+
python3 py-pip \
98
&& pip install s3cmd python-magic
109

1110
COPY application/ /data/

README.md

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@ Service to backup and/or restore a PostgreSQL database using S3
88
4. Run a backup and check your bucket for that backup
99

1010
### Environment variables
11-
`MODE=[backup|restore]`
11+
`MODE` Valid values: `backup`, `restore`
1212

13-
`DB_HOST=` hostname of the database server
13+
`DB_HOST` hostname of the database server
1414

15-
`DB_NAME=` name of the database
15+
`DB_NAME` name of the database
1616

17-
`DB_OPTIONS=opt1 opt2 opt3 ...` optional arguments to supply to the backup or restore commands
17+
`DB_OPTIONS` optional arguments to supply to the backup or restore commands
1818

19-
`DB_ROOTPASSWORD=` password for the `DB_ROOTUSER`
19+
`DB_ROOTPASSWORD` password for the `DB_ROOTUSER`
2020

21-
`DB_ROOTUSER=` database administrative user, typically "postgres" for PostgreSQL databases
21+
`DB_ROOTUSER` database administrative user, typically "postgres" for PostgreSQL databases
2222

23-
`DB_USERPASSWORD=` password for the `DB_USER`
23+
`DB_USERPASSWORD` password for the `DB_USER`
2424

25-
`DB_USER=` user that accesses the database (PostgreSQL "role")
25+
`DB_USER` user that accesses the database (PostgreSQL "role")
2626

27-
`AWS_ACCESS_KEY=` used for S3 interactions
27+
`AWS_ACCESS_KEY` used for S3 interactions
2828

29-
`AWS_SECRET_KEY=` used for S3 interactions
29+
`AWS_SECRET_KEY` used for S3 interactions
3030

31-
`S3_BUCKET=` _e.g., s3://database-backups_ **NOTE: no trailing slash**
31+
`S3_BUCKET` e.g., _s3://database-backups_ **NOTE: no trailing slash**
3232

3333
>**It's recommended that your S3 bucket have versioning turned on.**
3434
@@ -38,9 +38,22 @@ This image is built automatically on Docker Hub as [silintl/postgresql-backup-re
3838
## Playing with it locally
3939
You'll need [Docker](https://www.docker.com/get-docker), [Docker Compose](https://docs.docker.com/compose/install/), and [Make](https://www.gnu.org/software/make/).
4040

41-
1. `cp local.env.dist local.env` and supply variables
42-
2. Ensure you have a `gz` dump in your S3 bucket to be used for testing. A test database is provided as part of this project in the `test` folder. You can copy it to S3 as follows:
41+
1. Copy `local.env.dist` to `local.env`.
42+
2. Edit `local.env` to supply values for the variables.
43+
3. Ensure you have a `gz` dump in your S3 bucket to be used for testing. A test database is provided as part of this project in the `test` folder. You can copy it to S3 as follows:
4344
* `aws s3 cp test/world.sql.gz ${S3_BUCKET}/world.sql.gz`
44-
3. `make`
45-
46-
A UI into the local database will then be running at [http://localhost:8080](http://localhost:8080)
45+
4. `make db` # creates the Postgres DB server
46+
5. `make restore` # restores the DB dump file
47+
6. `docker ps -a` # get the Container ID of the exited restore container
48+
7. `docker logs <containerID>` # review the restoration log messages
49+
8. `make backup` # create a new DB dump file
50+
9. `docker ps -a` # get the Container ID of the exited backup container
51+
10. `docker logs <containerID>` # review the backup log messages
52+
11. `make restore` # restore the DB dump file from the new backup
53+
12. `docker ps -a` # get the Container ID of the exited restore container
54+
13. `docker logs <containerID>` # review the restoration log messages
55+
14. `make clean` # remove containers and network
56+
15. `docker volume ls` # find the volume ID of the Postgres data container
57+
16. `docker volume rm <volumeID>` # remove the data volume
58+
17. `docker images` # list existing images
59+
18. `docker image rm <imageID ...>` # remove images no longer needed

application/restore.sh

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@ STATUS=0
44

55
echo "postgresql-backup-restore: restore: Started"
66

7-
# Does the database exist?
8-
echo "postgresql-backup-restore: checking for DB ${DB_NAME}"
9-
result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --list | grep ${DB_NAME})
10-
if [ -z "${result}" ]; then
11-
message="Database "${DB_NAME}" on host "${DB_HOST}" does not exist."
12-
echo "postgresql-backup-restore: FATAL: ${message}"
13-
exit 1
14-
fi
15-
167
# Ensure the database user exists.
178
echo "postgresql-backup-restore: checking for DB user ${DB_USER}"
189
result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --command='\du' | grep ${DB_USER})
@@ -25,14 +16,6 @@ if [ -z "${result}" ]; then
2516
fi
2617
fi
2718

28-
echo "postgresql-backup-restore: changing DB ownership to ${DB_USER}"
29-
result=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --command="alter database ${DB_NAME} owner to ${DB_USER};")
30-
if [ "${result}" != "ALTER DATABASE" ]; then
31-
message="Alter database command failed: ${result}"
32-
echo "postgresql-backup-restore: FATAL: ${message}"
33-
exit 1
34-
fi
35-
3619
echo "postgresql-backup-restore: restoring ${DB_NAME}"
3720

3821
start=$(date +%s)
@@ -58,7 +41,7 @@ else
5841
fi
5942

6043
start=$(date +%s)
61-
psql --host=${DB_HOST} --username=${DB_USER} --dbname=${DB_NAME} ${DB_OPTIONS} < /tmp/${DB_NAME}.sql || STATUS=$?
44+
psql --host=${DB_HOST} --username=${DB_ROOTUSER} --dbname=postgres ${DB_OPTIONS} < /tmp/${DB_NAME}.sql || STATUS=$?
6245
end=$(date +%s)
6346

6447
if [ $STATUS -ne 0 ]; then

docker-compose.yml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,14 @@ services:
1010
# POSTGRES_USER - superuser (default is 'postgres')
1111
# POSTGRES_DB - name of default database (default is value of POSTGRES_USER)
1212
db:
13-
image: postgres:9.6-alpine
13+
image: postgres:11.15-alpine3.15
1414
volumes_from:
1515
- data
1616
ports:
1717
- "5432"
1818
environment:
1919
POSTGRES_PASSWORD: r00tp@ss!
2020

21-
# adminer:
22-
# image: adminer:4.6.3
23-
# ports:
24-
# - "8080:8080"
25-
2621
# DB_HOST - hostname of the database server
2722
# DB_ROOTUSER - administrative user for the database server
2823
# DB_ROOTPASSWORD - password for the DB_ROOTUSER
@@ -39,8 +34,8 @@ services:
3934
DB_HOST: db
4035
DB_ROOTUSER: postgres
4136
DB_ROOTPASSWORD: r00tp@ss!
42-
DB_USER: postgres
43-
DB_USERPASSWORD: r00tp@ss!
37+
DB_USER: dbuser
38+
DB_USERPASSWORD: dbuserpass
4439
DB_NAME: world
4540
MODE: restore
4641
CRON_SCHEDULE: "25 * * * *"
@@ -55,8 +50,8 @@ services:
5550
DB_HOST: db
5651
DB_ROOTUSER: postgres
5752
DB_ROOTPASSWORD: r00tp@ss!
58-
DB_USER: postgres
59-
DB_USERPASSWORD: r00tp@ss!
53+
DB_USER: dbuser
54+
DB_USERPASSWORD: dbuserpass
6055
DB_NAME: world
6156
MODE: backup
6257
CRON_SCHEDULE: "20 * * * *"

test/world.sql.gz

-32 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)