|
| 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