Skip to content

Commit 452b518

Browse files
committed
- added tmp directory & docker-compose .env file
1 parent fc85a2e commit 452b518

File tree

6 files changed

+72
-64
lines changed

6 files changed

+72
-64
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ publisher/sanitized
22
database/test/actual
33
backups/backups
44
sanitizer/files
5+
.env

backups/.env.example

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# DB to dump
2+
DB_NAME: test_db
3+
4+
# DBs to dump
5+
DB_CONFIG_1: db-1:5432:*:postgres:password
6+
DB_CONFIG_2: db-2:5432:*:postgres:password
7+
8+
# This dir will be created if it doesn't exist. This must be writable by the user the script is
9+
# running as.
10+
BACKUP_DIR: /backups/
11+
BACKUP_TMP_DIR: /tmp/
12+
13+
# SETTINGS FOR ROTATED BACKUPS
14+
15+
# Which day to take the weekly backup from (1-7 = Monday-Sunday)
16+
DAY_OF_WEEK_TO_KEEP: 5
17+
18+
# Number of hours to keep daily backups
19+
DAYS_TO_KEEP_HOURLY: 1
20+
21+
# Number of days to keep daily backups
22+
DAYS_TO_KEEP_DAILY: 7
23+
24+
# How many weeks to keep weekly backups
25+
WEEKS_TO_KEEP_WEEKLY: 4
26+
27+
# How many months to keep monthly backups
28+
MONTHS_TO_KEEP_MONTHLY: 6
29+
30+
# Avoid dumping of existent roles to prevent password rewriting
31+
IGNORE_DUMP_ROLES: \(admin\|app\|postgres\)
32+
33+
# Copy roles in a separate file
34+
PERFORM_BACKUP_ROLES: "1"
35+
36+
#S3_BUCKET=***
37+
#S3_KEY: ***
38+
#S3_SECRET_KEY: ***
39+
#S3_BUCKET_URL: https://fra1.digitaloceanspaces.com

backups/docker-compose.yml

+2-38
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,8 @@ version: '3.4'
1111
services:
1212
backups:
1313
build: .
14-
environment:
15-
# DB to dump
16-
DB_NAME: test_db
17-
18-
# DBs to dump
19-
DB_CONFIG_1: db-1:5432:*:postgres:password
20-
DB_CONFIG_2: db-2:5432:*:postgres:password
21-
22-
# This dir will be created if it doesn't exist. This must be writable by the user the script is
23-
# running as.
24-
BACKUP_DIR: /backups/
25-
26-
# SETTINGS FOR ROTATED BACKUPS
27-
28-
# Which day to take the weekly backup from (1-7 = Monday-Sunday)
29-
DAY_OF_WEEK_TO_KEEP: 5
30-
31-
# Number of hours to keep daily backups
32-
DAYS_TO_KEEP_HOURLY: 1
33-
34-
# Number of days to keep daily backups
35-
DAYS_TO_KEEP_DAILY: 7
36-
37-
# How many weeks to keep weekly backups
38-
WEEKS_TO_KEEP_WEEKLY: 4
39-
40-
# How many months to keep monthly backups
41-
MONTHS_TO_KEEP_MONTHLY: 6
42-
43-
# Avoid dumping of existent roles to prevent password rewriting
44-
IGNORE_DUMP_ROLES: \(admin\|app\|postgres\)
45-
46-
PERFORM_BACKUP_ROLES: "1"
47-
48-
S3_KEY: '*'
49-
S3_SECRET_KEY: '*'
50-
S3_BUCKET: plyo-db-backups-prod
51-
S3_BUCKET_URL: https://fra1.digitaloceanspaces.com
14+
env_file:
15+
- .env
5216
volumes:
5317
- ./backups/:/backups/
5418
- ./src:/usr/src/app/

backups/src/pg_backup_rotated.sh

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/bash
22

33
# see https://wiki.postgresql.org/wiki/Automated_Backup_on_Linux
4+
BACKUP_TMP_DIR=${BACKUP_TMP_DIR:-/tmp}
45

56
log () {
67
echo "[pg_backup_rotated.sh]> $@"
@@ -17,7 +18,9 @@ function perform_backups()
1718

1819
backup_date=`date +%Y-%m-%d`
1920
backup_file_path="${BACKUP_DIR}${backup_date}${suffix}".backup
21+
backup_progress_file_path="${BACKUP_TMP_DIR}${backup_date}${suffix}".backup
2022
backup_roles_file_path="${backup_file_path}_roles.out"
23+
backup_progress_roles_file_path="${backup_progress_file_path}_roles.out"
2124

2225
if [[ -e ${backup_file_path} ]]; then
2326
log "${backup_file_path} already exists, skipping dump"
@@ -26,19 +29,19 @@ function perform_backups()
2629

2730
log "Dumping custom backup for ${DB_NAME} database to ${backup_file_path}"
2831

29-
if ! pg_dump -Fc -h "$db_host" -p "$db_port" -U "$db_user" ${DB_NAME} -f ${backup_file_path}.in_progress; then
32+
if ! pg_dump -Fc -h "$db_host" -p "$db_port" -U "$db_user" ${DB_NAME} -f ${backup_progress_file_path}.in_progress; then
3033
log "[ERROR] Failed to produce custom backup database ${DB_NAME}"
3134
exit 1
3235
else
3336
# finalize database backup
34-
mv ${backup_file_path}.in_progress ${backup_file_path}
37+
mv ${backup_progress_file_path}.in_progress ${backup_file_path}
3538

3639
# perform backup of database roles
3740
if [[ "${PERFORM_BACKUP_ROLES}" == "1" ]]; then
3841
log "Dumping roles backup for ${DB_NAME} database to ${backup_roles_file_path}"
39-
pg_dumpall -r -h "$db_host" -p "$db_port" -U postgres -f ${backup_roles_file_path}.in_progress
40-
cat ${backup_roles_file_path}.in_progress | grep -v ${IGNORE_DUMP_ROLES} > "${backup_roles_file_path}"
41-
rm -f ${backup_roles_file_path}.in_progress
42+
pg_dumpall -r -h "$db_host" -p "$db_port" -U postgres -f ${backup_progress_roles_file_path}.in_progress
43+
cat ${backup_progress_roles_file_path}.in_progress | grep -v ${IGNORE_DUMP_ROLES} > "${backup_roles_file_path}"
44+
rm -f ${backup_progress_roles_file_path}.in_progress
4245
fi
4346

4447
# perform copy backup to S3-compatible storage

sanitizer/.env.example

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
ADMIN_ROLE_NAME: plyo
2+
EXTENSIONS: pgcrypto,ltree
3+
PRIVATE_SCHEMA_NAME: public
4+
SCHEMA_NAME: public
5+
POSTGRES_PASSWORD: password
6+
#ROLES_FILE_PATH: /roles/roles.out
7+
8+
DB_NAME: database
9+
DB_HOST: postgres
10+
11+
#S3_KEY: *
12+
#S3_SECRET_KEY: *
13+
#
14+
#S3_BUCKET: *
15+
#S3_BUCKET_URL: https://fra1.digitaloceanspaces.com
16+
#S3_BACKUP_MNT_POINT: /backups
17+
#
18+
#S3_SANITIZED_BUCKET: *
19+
#S3_SANITIZED_BUCKET_URL: https://fra1.digitaloceanspaces.com
20+
#S3_SANITIZED_BACKUP_MNT_POINT: /sanitized

sanitizer/docker-compose.yml

+2-21
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,8 @@ services:
44
build:
55
context: .
66
working_dir: /usr/src/app
7-
environment:
8-
- ADMIN_ROLE_NAME=plyo
9-
- EXTENSIONS=pgcrypto,ltree
10-
- PRIVATE_SCHEMA_NAME=public
11-
- SCHEMA_NAME=public
12-
- POSTGRES_PASSWORD=password
13-
# - ROLES_FILE_PATH=/roles/roles.out
14-
15-
- DB_NAME=plyo
16-
- DB_HOST=plyo-db
17-
18-
- S3_KEY=*
19-
- S3_SECRET_KEY=*
20-
21-
- S3_BUCKET=plyo-db-backups-prod
22-
- S3_BUCKET_URL=https://fra1.digitaloceanspaces.com
23-
- S3_BACKUP_MNT_POINT=/backups
24-
25-
- S3_SANITIZED_BUCKET=plyo-db-sanitized-prod
26-
- S3_SANITIZED_BUCKET_URL=https://fra1.digitaloceanspaces.com
27-
- S3_SANITIZED_BACKUP_MNT_POINT=/sanitized
7+
env_file:
8+
- .env
289
volumes:
2910
- ./src/start.sh:/usr/src/app/start.sh
3011
- ./src/mount-s3.sh:/usr/src/app/mount-s3.sh

0 commit comments

Comments
 (0)