Skip to content

Commit bf0704b

Browse files
committed
Initial commit
0 parents  commit bf0704b

12 files changed

+560
-0
lines changed

.env.example

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Postgres
2+
POSTGRES_DATABASE=database
3+
POSTGRES_USER=user
4+
POSTGRES_PASSWORD=password
5+
6+
# Backup
7+
SCHEDULE=@weekly
8+
BACKUP_KEEP_DAYS=7
9+
PASSPHRASE=wxHw26GJZQBDenA8
10+
S3_REGION=us-east-1
11+
S3_BUCKET=my-s3-bucket
12+
S3_PREFIX=prefix
13+
S3_ACCESS_KEY_ID=AKIA3M3ZKBJPQUBT2UK6
14+
S3_SECRET_ACCESS_KEY=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
15+
POSTGRES_HOST=postgres

.gitattributes

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
* text=auto
2+
*.sh text eol=lf
3+
4+
/.gitattributes export-ignore
5+
/.github export-ignore
6+
/.gitignore export-ignore
7+
/CHANGELOG.md export-ignore
8+
/README.md export-ignore
9+
/tests export-ignore

.github/workflows/.github-ci.yml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Docker Image CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
docker-build-release:
10+
runs-on: ubuntu-latest
11+
12+
strategy:
13+
matrix:
14+
include:
15+
- { postgres: 14, alpine: '3.16' }
16+
- { postgres: 15, alpine: '3.17' }
17+
- { postgres: 16, alpine: '3.19' }
18+
19+
steps:
20+
- name: Checkout code
21+
uses: actions/checkout@v4
22+
23+
- name: Setup QEMU
24+
uses: docker/setup-qemu-action@v3
25+
26+
- name: Setup Docker Buildx
27+
uses: docker/setup-buildx-action@v3
28+
29+
- name: Login to Dockerhub
30+
uses: docker/login-action@v3
31+
with:
32+
username: ${{ secrets.CI_REGISTRY_USER }}
33+
password: ${{ secrets.CI_REGISTRY_TOKEN }}
34+
35+
- name: Builds and pushes Docker image
36+
uses: docker/build-push-action@v5
37+
with:
38+
context: .
39+
platforms: linux/amd64,linux/arm64
40+
push: true
41+
tags: ${{ secrets.DOCKER_IMAGE }}:${{ matrix.postgres }}
42+
build-args: |
43+
ALPINE_VERSION=${{ matrix.alpine }}
44+
cache-from: type=gha
45+
cache-to: type=gha,mode=max

.gitignore

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
### PhpStorm ###
2+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
3+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
4+
5+
# User-specific stuff:
6+
.idea/**/tasks.xml
7+
.idea/**/workspace.xml
8+
9+
# Sensitive or high-churn files:
10+
.idea/**/dataSources.ids
11+
.idea/**/dataSources.local.xml
12+
.idea/**/dataSources.xml
13+
.idea/**/dataSources/
14+
.idea/**/dynamic.xml
15+
.idea/**/sqlDataSources.xml
16+
.idea/**/uiDesigner.xml
17+
18+
# Gradle:
19+
.idea/**/gradle.xml
20+
.idea/**/libraries
21+
22+
# Mongo Explorer plugin:
23+
.idea/**/mongoSettings.xml
24+
25+
## File-based project format:
26+
*.iws
27+
28+
# mpeltonen/sbt-idea plugin
29+
.idea_modules/
30+
31+
# JIRA plugin
32+
atlassian-ide-plugin.xml
33+
34+
# Crashlytics plugin (for Android Studio and IntelliJ)
35+
com_crashlytics_export_strings.xml
36+
crashlytics-build.properties
37+
crashlytics.properties
38+
fabric.properties
39+
40+
### MacOS System Files ###
41+
# Covers: MacOS
42+
# Reference: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore
43+
44+
# General
45+
.AppleDouble
46+
.DS_Store
47+
.idea
48+
.LSOverride
49+
50+
# Icon must end with two \r
51+
Icon
52+
53+
# Thumbnails
54+
._*
55+
56+
# Files that might appear in the root of a volume
57+
.com.apple.timemachine.donotpresent
58+
.DocumentRevisions-V100
59+
.fseventsd
60+
.Spotlight-V100
61+
.TemporaryItems
62+
.Trashes
63+
.VolumeIcon.icns
64+
65+
# Directories potentially created on remote AFP share
66+
.apdisk
67+
.AppleDB
68+
.AppleDesktop
69+
Network Trash Folder
70+
Temporary Items
71+
72+
# Directories SSL
73+
.well-known
74+
75+
# Environment's params
76+
.env

Dockerfile

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
FROM alpine:3
2+
LABEL maintainer="Hi-Technix, Inc. <[email protected]>"
3+
4+
ARG TARGETARCH
5+
6+
COPY ./src/install.sh install.sh
7+
RUN sh install.sh && rm install.sh
8+
9+
ENV POSTGRES_DATABASE ''
10+
ENV POSTGRES_HOST ''
11+
ENV POSTGRES_PORT 5432
12+
ENV POSTGRES_USER ''
13+
ENV POSTGRES_PASSWORD ''
14+
ENV PGDUMP_EXTRA_OPTS ''
15+
ENV S3_ACCESS_KEY_ID ''
16+
ENV S3_SECRET_ACCESS_KEY ''
17+
ENV S3_BUCKET ''
18+
ENV S3_REGION 'us-west-1'
19+
ENV S3_PATH 'backup'
20+
ENV S3_ENDPOINT ''
21+
ENV S3_S3V4 'no'
22+
ENV SCHEDULE ''
23+
ENV PASSPHRASE ''
24+
ENV BACKUP_KEEP_DAYS ''
25+
26+
COPY ./src/run.sh run.sh
27+
COPY ./src/env.sh env.sh
28+
COPY ./src/backup.sh backup.sh
29+
COPY ./src/restore.sh restore.sh
30+
31+
CMD ["sh", "run.sh"]

LICENSE

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
The BSD 3-Clause License
2+
Copyright (c) 2023, Hi-Technix, Inc.
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
6+
following conditions are met:
7+
8+
Redistributions of source code must retain the above copyright notice, this list of conditions and the following
9+
disclaimer.
10+
11+
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
12+
disclaimer in the documentation and/or other materials provided with the distribution.
13+
14+
Neither the name of Hi-Technix, Inc. and its libraries nor the names of its contributors may be used to endorse or
15+
promote products derived from this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# PostgreSQL backup to AWS S3
2+
3+
This project provides Docker images to periodically back up a PostgreSQL database to AWS S3, and to restore from the
4+
backup as needed.
5+
6+
### 📦 Requirements
7+
8+
The Docker & Docker Compose system requirements are Linux Ubuntu as the OS (other operating systems are supported as
9+
well), an absolute minimum 512MB RAM (2GB recommended)
10+
11+
In order to install docker Ubuntu, you will need to meet the following requirements:
12+
13+
- OS: Linux Ubuntu
14+
- Memory: 512MB RAM (2GB Recommended)
15+
- Disk: Sufficient amount to run the Docker containers you wish to use
16+
- CPU: Dependant on the applications you wish to run in the containers
17+
18+
### 📋 Features
19+
20+
- Images are tagged by the major Postgres version supported: `14`, `15` or `16`.
21+
- The `SCHEDULE` variable determines backup frequency. See go-cron schedules
22+
documentation [here](http://godoc.org/github.com/robfig/cron#hdr-Predefined_schedules). Omit to run the backup
23+
immediately and then exit.
24+
- If `PASSPHRASE` is provided, the backup will be encrypted using GPG.
25+
- Run `docker exec <container name> sh backup.sh` to trigger a backup ad-hoc.
26+
- If `BACKUP_KEEP_DAYS` is set, backups older than this many days will be deleted from S3.
27+
- Set `S3_ENDPOINT` if you're using a non-AWS S3-compatible storage provider.
28+
29+
### 🔧 Installation
30+
31+
1. Install Docker and Docker-Compose
32+
33+
- [Docker Install documentation](https://docs.docker.com/install/)
34+
- [Docker-Compose Install documentation](https://docs.docker.com/compose/install/)
35+
36+
2. Bring up your stack by running
37+
38+
```shell
39+
git clone https://github.com/hitechnix/postgres-backup.git \
40+
&& cd postgres-backup \
41+
&& cp .env.example .env
42+
```
43+
44+
3. Edit environment variable
45+
46+
```dotenv
47+
# Postgres
48+
POSTGRES_DATABASE=database
49+
POSTGRES_USER=user
50+
POSTGRES_PASSWORD=password
51+
52+
# Backup
53+
SCHEDULE=@weekly
54+
BACKUP_KEEP_DAYS=7
55+
PASSPHRASE=wxHw26GJZQBDenA8
56+
S3_REGION=us-east-1
57+
S3_BUCKET=my-s3-bucket
58+
S3_PREFIX=prefix
59+
S3_ACCESS_KEY_ID=AKIA3M3ZKBJPQUBT2UK6
60+
S3_SECRET_ACCESS_KEY=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
61+
POSTGRES_HOST=postgres
62+
```
63+
64+
4. Start PostgreSQL backup to AWS S3
65+
66+
```shell
67+
docker-compose up -d
68+
```
69+
70+
### 📝 Usage
71+
72+
Example `docker-compose.yml`
73+
74+
```yaml
75+
version: "3.9"
76+
77+
services:
78+
postgres:
79+
container_name: postgres
80+
restart: unless-stopped
81+
image: postgres:16
82+
environment:
83+
- POSTGRES_DATABASE=${POSTGRES_DATABASE:-database}
84+
- POSTGRES_USER=${POSTGRES_USER:-user}
85+
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-password}
86+
networks:
87+
- network-bridge
88+
89+
backup:
90+
container_name: postgres-backup
91+
restart: unless-stopped
92+
image: hitechnix/postgres-backup:16
93+
environment:
94+
- SCHEDULE=${SCHEDULE:-@weekly}
95+
- BACKUP_KEEP_DAYS=${BACKUP_KEEP_DAYS:-7}
96+
- PASSPHRASE=${PASSPHRASE:-passphrase}
97+
- S3_REGION=${S3_REGION:-region}
98+
- S3_BUCKET=${S3_BUCKET:-bucket}
99+
- S3_PREFIX=${S3_PREFIX:-prefix}
100+
- S3_ACCESS_KEY_ID=${S3_ACCESS_KEY_ID:-key}
101+
- S3_SECRET_ACCESS_KEY=${S3_SECRET_ACCESS_KEY:-secret}
102+
- POSTGRES_HOST=${POSTGRES_HOST:-postgres}
103+
depends_on:
104+
- postgres
105+
networks:
106+
- network-bridge
107+
108+
networks:
109+
network-bridge:
110+
driver: bridge
111+
name: network-bridge
112+
```
113+
114+
#### Restore
115+
116+
> **WARNING:** DATA LOSS! All database objects will be dropped and re-created.
117+
118+
### ... from latest backup
119+
120+
```sh
121+
docker exec <container name> sh restore.sh
122+
```
123+
124+
> **NOTE:** If your bucket has more than 1000 files, the latest may not be restored -- only one S3 `ls` command is used
125+
126+
### ... from specific backup
127+
128+
```sh
129+
docker exec <container name> sh restore.sh <timestamp>
130+
```
131+
132+
### 📨 Message
133+
134+
I hope you find this useful. If you have any questions, please create an issue.
135+
136+
### 🔐 Security
137+
138+
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
139+
140+
### 📖 License
141+
142+
This software is released under the [BSD 3-Clause][link-license] License. Please see the [LICENSE](LICENSE) file
143+
or https://opensource.hitechnix.com/LICENSE.txt for more information.
144+
145+
### ✨ Contributors
146+
147+
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
148+
149+
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
150+
<!-- prettier-ignore-start -->
151+
<!-- markdownlint-disable -->
152+
<table>
153+
<td align="center" valign="top" width="14.28%">
154+
<a href="https://trants.me">
155+
<img src="https://avatars.githubusercontent.com/u/40693126?v=4?s=100" width="100px;" alt="Son Tran Thanh" />
156+
<br />
157+
<sub>
158+
<b>Son Tran Thanh</b>
159+
</sub>
160+
</a>
161+
<br />
162+
<a href="#tool-trants" title="Tools">🔧</a>
163+
<a href="#infra-trants" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a>
164+
<a href="#maintenance-trants" title="Maintenance">🚧</a>
165+
<a href="https://github.com/hitechnix/postgres-backup/commits?author=trants" title="Code">💻</a>
166+
<a href="https://github.com/hitechnix/postgres-backup/commits?author=trants" title="Documentation">📖</a>
167+
<a href="https://github.com/hitechnix/postgres-backup/pulls?q=is%3Apr+reviewed-by%3Atrants" title="Reviewed Pull Requests">👀</a>
168+
</td>
169+
</table>
170+
171+
<!-- markdownlint-restore -->
172+
<!-- prettier-ignore-end -->
173+
174+
<!-- ALL-CONTRIBUTORS-LIST:END -->
175+
176+
This project follows the [all-contributors](https://allcontributors.org) specification.
177+
Contributions of any kind welcome!
178+
179+
[link-license]: https://opensource.org/license/bsd-3-clause

0 commit comments

Comments
 (0)