1
- # nd_postgres_backup
2
-
3
- Docker image for universal postgres backups
1
+ # PostgreSQL Backup to S3 and retention container
4
2
5
3
# Roadmap
6
4
7
5
- [X] Add support for S3
8
6
- [X] Add CI/CD to publish image to DockerHub
9
- - [ ] Add retention policy settings by env vars
7
+ - [X ] Add retention policy settings by env vars
10
8
- [X] Notify about backup status by HTTP-request
11
- - [ ] Add docker-compose example
9
+ - [X] Add docker-compose example
10
+
11
+ ## Description
12
+
13
+ Image created to automate backing up procedure of PostgreSQL databases, store backups to S3 Object storage and implement retention of stored archives with ` Grandfather-father-son ` backup rotation [ scheme] ( https://en.wikipedia.org/wiki/Backup_rotation_scheme ) .
14
+ It is also possible to use this container to create a single backup of specific DB.
12
15
13
- # Usage
16
+ ## Usage
17
+
18
+ Key idea of usage was to add this container as a service to ` docker-compose.yml ` manifest alongside with PostgreSQL database container. See ` compose-example/docker-compose.yml ` .
19
+ To run container as a standalone backupper, to backup cloud SaaS or bare-metal deployed PostgreSQL, for example, use following command:
14
20
15
- ## Backup manually:
16
- Use next command if you need to backup DB manually:
17
21
``` shell
18
- docker run --rm \
19
- --env POSTGRES_HOST=" FQDN-OR-IP" \
20
- --env POSTGRES_DB=" DB-NAME" \
21
- --env POSTGRES_USER=" DB-USER" \
22
- --env POSTGRES_PASSWORD=" PASS" \
23
- --env S3_ENDPOINT=https://YOUR-S3 \
24
- --env S3_ACCESS_KEY=" access-key" \
25
- --env S3_SECRET_KEY=" secret-key" \
26
- --env S3_BUCKET=" BUCKET-NAME" \
27
- numdes/nd_postgres_backup:v0.2.2
22
+ docker run -d --rm \
23
+ --env POSTGRES_HOST=" DB_IP_OR_HOSTNAME" \
24
+ --env POSTGRES_DB=" DB_NAME" \
25
+ --env POSTGRES_USER=" DB_USERNAME" \
26
+ --env POSTGRES_PORT=" NON_DEFAULT_PORT" \
27
+ --env POSTGRES_PASSWORD=" DB_USERNAME_PASSWORD" \
28
+ --env NOTIFICATION_SERVER_URL=" ONLY_SET_IF_PRIVATE_TELEGRAM_BOT_USED" \
29
+ --env TELEGRAM_CHAT_ID=" PRIVATE_OR_TELEGRAM_BOT_ID" \
30
+ --env S3_ENDPOINT=" S3_API_URL" \
31
+ --env S3_ACCESS_KEY=" S3_ACCESS_KEY" \
32
+ --env S3_SECRET_KEY=" S3_SECERT_KEY" \
33
+ --env S3_BUCKET=" S3_BUCKET_NAME(+POSSIBLE_PATH_DEEPER)" \
34
+ --env S3_ALIAS=" S3_CONFIG_SET_ALIAS" \
35
+ numdes/nd_postgres_backup:v0.3.0
28
36
```
29
- It will backup given Postgres DB and upload it to S3 bucket.
30
37
31
- ## Backup using ` go-cron `
38
+ ### Manual one-time backup without schedule
39
+
40
+ Set full S3 path (e.g ` bucket_name/project_name/stage_branch/database_name.tar.gz ` ) as the value of variable ` S3_OBJECT_PATH ` to execute single backup
32
41
33
42
``` shell
34
- docker run --detach \
35
- --env POSTGRES_HOST=" FQDN-OR-IP " \
36
- --env POSTGRES_DB=" DB-NAME " \
37
- --env POSTGRES_USER=" DB-USER " \
38
- --env POSTGRES_PASSWORD= " PASS " \
39
- --env S3_ENDPOINT=http://YOUR-S3 \
40
- --env S3_ACCESS_KEY= " KEY-ID " \
41
- --env S3_SECRET_KEY= " KEY-SECRET " \
42
- --env S3_BUCKET= " BUCKET-NAME " \
43
- --env PRIVATE_NOTIFICATION_URL=http://webhook \
44
- --env TELEGRAM_CHAT_ID=point_to_notify_group \
45
- --env POSTGRES_PORT=if_not_5432 \
46
- --env SCHEDULE=Chosen_schedule \
47
- numdes/nd_postgres_backup:v0.2.1
43
+ docker run -d --rm \
44
+ --env POSTGRES_HOST=" DB_IP_OR_HOSTNAME " \
45
+ --env POSTGRES_DB=" DB_NAME " \
46
+ --env POSTGRES_USER=" DB_USERNAME " \
47
+ --env POSTGRES_PORT= " NON_DEFAULT_PORT " \
48
+ --env POSTGRES_PASSWORD= " DB_USERNAME_PASSWORD " \
49
+ --env NOTIFICATION_SERVER_URL= " ONLY_SET_IF_PRIVATE_TELEGRAM_BOT_USED " \
50
+ --env TELEGRAM_CHAT_ID= " PRIVATE_OR_TELEGRAM_BOT_ID " \
51
+ --env S3_ENDPOINT= " S3_API_URL " \
52
+ --env S3_ACCESS_KEY= " S3_ACCESS_KEY " \
53
+ --env S3_SECRET_KEY= " S3_SECERT_KEY " \
54
+ --env S3_OBJECT_PATH= " FULL_S3_PATH (e.g ` bucket_name/project_name/stage_branch/database_name.tar.gz ` ) " \
55
+ --env S3_ALIAS= " S3_CONFIG_SET_ALIAS " \
56
+ numdes/nd_postgres_backup:v0.3.0
48
57
```
49
58
50
- :wave : By default ` SCHEDULE ` variable is set to ` @daily ` in case if you need other scheduling options, please refer
51
- to ` go-cron ` * [ Documentation] ( https://pkg.go.dev/github.com/robfig/cron?utm_source=godoc#hdr-Predefined_schedules ) * .
59
+ ## Backup strategy
60
+
61
+ By default set to make backup every hour, plus one separate backup a day, plus one separate backup a week
62
+
63
+ Schedule can be tuned or changed by editing of ` crontab ` file
64
+
65
+ ## Retention strategy
66
+
67
+ Maximum depth of storage for each type of backup can be tuned by changing values of these variables:
68
+
69
+ - ` WEEKLY_BACKUP_LIMIT `
70
+ - ` DAILY_BACKUP_LIMIT `
71
+ - ` HOURLY_BACKUP_LIMIT `
72
+
73
+ Schedule of retention script (` retention.sh ` ) execution can be edited in ` crontab ` file
52
74
53
75
## Variables
54
76
@@ -57,58 +79,37 @@ to `go-cron` *[Documentation](https://pkg.go.dev/github.com/robfig/cron?utm_sour
57
79
58
80
| Name | Description |
59
81
| --------------------| -----------------------------|
60
- | DOCKERHUB_LOGIN | ` Actions ` Repository secret |
61
- | DOCKERHUB_PASSWORD | ` Actions ` Repository secret |
82
+ | DOCKERHUB_USERNAME | ` Actions ` Repository secret |
83
+ | DOCKERHUB_TOKEN | ` Actions ` Repository secret |
62
84
63
85
### Notification environmental variables
64
86
65
- | Name | Description |
66
- | --------------------------| -----------------------------------------|
67
- | TELEGRAM_CHAT_ID | Notifying group |
68
- | PRIVATE_NOTIFICATION_URL | Private notifier URL |
69
- | TELEGRAM_BOT_TOKEN | Only used to call Telegram's public API |
87
+ | Name | Description |
88
+ | --------------------------- | ------------------------------ -----------------------------------------|
89
+ | NOTIFICATION_SERVER_URL | URL of private telegram bot |
90
+ | TELEGRAM_CHAT_ID | Custom bot ID or Telegram Bot ID when bot created using ` @botfather ` |
91
+ | TELEGRAM_BOT_TOKEN | Created by ` @botfather ` bot security token |
70
92
71
93
### Environmental variables
72
94
73
- | Name | Default value | Is mandatory | Description |
74
- | ---------------------| :--------------| :------------:| ---------------------------------------------------------------------------|
75
- | POSTGRES_DB | - | YES | Database name |
76
- | POSTGRES_HOST | - | YES | PostgreSQL IP address or hostname |
77
- | POSTGRES_PORT | 5432 | - | Connection TCP port |
78
- | POSTGRES_USER | - | YES | Database user |
79
- | POSTGRES_PASSWORD | - | YES | Database user password |
80
- | POSTGRES_EXTRA_OPTS | --blobs | - | Extra options ` pg_dump ` run |
81
- | SCHEDULE | @daily | - | ` go-cron ` schedule. See [ this] ( #backup-using-go-cron ) |
82
- | HEALTHCHECK_PORT | 8080 | - | Port listening for cron-schedule health check. |
83
- | S3_ACCESS_KEY | - | YES | Key or username with RW access to bucket |
84
- | S3_SECRET_KEY | - | YES | Secret or password for ` S3_ACCESS_KEY ` |
85
- | S3_BUCKET | - | YES | Name of S3 bucket |
86
- | S3_ENDPOINT | - | YES | URL of S3 storage |
87
- | S3_OBJECT_PATH | - | NO | Full path to archive including bucket name and desired file name. If not present will be generated automatically |
88
-
89
- ### Notification selection
90
-
91
- It is possible to use either private Telegram bot if you have it or Telegram public API.
92
-
93
- In scenario with private bot ` PRIVATE_NOTIFICATION_URL ` must be set alongside with ` TELEGRAM_CHAT_ID ` .
94
-
95
- In scenario with Telegram's public API ` TELEGRAM_BOT_TOKEN ` must be set as it is
96
- received (` Use this token to access the HTTP API: ` ) from ` @BotFather ` Telegram Bot. Variable ` TELEGRAM_CHAT_ID ` must be
97
- a proper Telegram ID of bot
98
-
99
- In ` docker ... ` command need to replace:
100
-
101
- ```
102
- --env PRIVATE_NOTIFICATION_URL=http://webhook \
103
- --env TELEGRAM_CHAT_ID=point_to_notify_group \
104
- ```
105
-
106
- to
107
-
108
- ```
109
- --env TELEGRAM_BOT_TOKEN='XXXXXXX:XXXXxxxxXXXXxxx' \
110
- --env TELEGRAM_CHAT_ID=000000000 \
111
- ```
95
+ | Variable Name | Default Value | Is Mandatory? | Description |
96
+ | ---------------------------| :-------------:| :-------------:| ----------------------------------------------------------------------|
97
+ | HOURLY_BACKUP_PATH | ` hourly ` | NO | Path suffix to hourly-made backups storage |
98
+ | DAILY_BACKUP_PATH | ` daily ` | NO | Path suffix to daily-made backups storage |
99
+ | WEEKLY_BACKUP_PATH | ` weekly ` | NO | Path suffix to weekly-made backups storage |
100
+ | HOURLY_BACKUP_LIMIT | ` 25 ` | NO | Max number of weekly backups |
101
+ | DAILY_BACKUP_LIMIT | ` 10 ` | NO | Max number of daily backups |
102
+ | WEEKLY_BACKUP_LIMIT | ` 5 ` | NO | Max number of hourly backups |
103
+ | S3_ACCESS_KEY | - | YES | ${S3_BUCKET} READ-WRITE S3 ACCESS KEY |
104
+ | S3_SECRET_KEY | - | YES | ${S3_BUCKET} READ-WRITE S3 ACCESS SECRET |
105
+ | S3_ENDPOINT | - | YES | S3 API URL |
106
+ | S3_BUCKET | - | YES | Path to hourly, daily, weekly directories. Including bucket name |
107
+ | S3_ALIAS | ` backup ` | NO | Name of config set in ` mcli ` command ` mcli alias set ` |
108
+ | S3_OBJECT_PATH | - | NO | Optional variable to use single backup [ functionality] ( #manual-backup-without-schedule ) |
109
+ | POSTGRES_DB | - | YES | PostgreSQL database name |
110
+ | POSTGRES_HOST | - | YES | PostgreSQL IP or host name |
111
+ | POSTGRES_PORT | ` 5432 ` | NO | TCP connection port |
112
+ | POSTGRES_USER | - | YES | DB usermane |
113
+ | POSTGRES_PASSWORD | - | YES | DB username password |
114
+ | POSTGRES_EXTRA_OPTS | ` --blobs ` | NO | ` pg_dump ` extra options |
112
115
113
- - If ` TELEGRAM_CHAT_ID ` has a proper format (Only digits not less than 5 not more than 32) and ` TELEGRAM_BOT_TOKEN ` is
114
- set, script will try to send notification through Telegram's public API.
0 commit comments