|
| 1 | +# Docker PG Backup |
| 2 | + |
| 3 | +Inspired by: https://github.com/kartoza/docker-pg-backup |
| 4 | + |
| 5 | +A Docker container that runs automated scheduled PostgreSQL/PostGIS backups for all |
| 6 | +PostgreSQL-based Docker Containers in its network that have the Label `"pgbackup.enable=true"`. |
| 7 | + |
| 8 | +It should work with |
| 9 | +the following PostgreSQL/PostGIS Docker images: |
| 10 | + |
| 11 | +* [mdillon/postgis](https://hub.docker.com/r/mdillon/postgis/) |
| 12 | +* [Kartoza docker postgis](https://github.com/kartoza//docker-postgis) |
| 13 | +* [Standard PostgreSQL Docker image](https://hub.docker.com/_/postgres/) |
| 14 | + |
| 15 | +Any other PostgreSQL/PostGIS image may work as long as its Container has the `POSTGRES_` environment |
| 16 | +variables set (see below). |
| 17 | + |
| 18 | +By default it will create a backup once per night (at 23:00) in a |
| 19 | +nicely ordered directory by container-name/year/month, but you can specify your own schedule. |
| 20 | + |
| 21 | +* Docker hub at: https://registry.hub.docker.com/u/justb4/pgbackup/ |
| 22 | +* Github at: https://github.com/justb4/docker-pgbackup |
| 23 | + |
| 24 | +## Getting the image |
| 25 | + |
| 26 | +There are various ways to get the image onto your system: |
| 27 | + |
| 28 | +The preferred way (but using most bandwidth for the initial image) is to |
| 29 | +get our docker trusted build like this: |
| 30 | + |
| 31 | + |
| 32 | +``` |
| 33 | +docker pull justb4/pgbackup:17 |
| 34 | +
|
| 35 | +``` |
| 36 | + |
| 37 | +We highly suggest that you use a tagged image as |
| 38 | +latest may change and may not successfully back up your database. Use the same or |
| 39 | +greater version of postgis as the database you are backing up. |
| 40 | +To build the image yourself: |
| 41 | + |
| 42 | +``` |
| 43 | +docker build -t justb4/pgbackup . |
| 44 | +``` |
| 45 | + |
| 46 | +If you do not wish to do a local checkout first then build directly from github. |
| 47 | + |
| 48 | +``` |
| 49 | +git clone git://github.com/justb4/docker-pgbackup |
| 50 | +``` |
| 51 | + |
| 52 | +## Environment Variables |
| 53 | + |
| 54 | +* `PGB_SCHEDULE`, crontab schedule line, if not set, defaults to : `0 23 * * *` |
| 55 | + |
| 56 | +## Run Backups |
| 57 | + |
| 58 | +To create a running container do: |
| 59 | + |
| 60 | +``` |
| 61 | +docker run --name="pgbackup"\ |
| 62 | + -v backup:/backup -v /var/run/docker.sock:/var/run/docker.sock \ |
| 63 | + -i -d justb4/pgbackup:17 |
| 64 | +``` |
| 65 | + |
| 66 | +In this example a local dir (`./backup`) is mounted inti which the actual backups will be |
| 67 | +stored. |
| 68 | + |
| 69 | +Best is to use docker-compose, below the as used |
| 70 | +for testing, with a schedule that backs up once a minute. |
| 71 | + |
| 72 | + |
| 73 | +``` |
| 74 | +# Test for pgbackup with sample db |
| 75 | +version: "3" |
| 76 | +
|
| 77 | +services: |
| 78 | + db: |
| 79 | + image: mdillon/postgis:17-alpine |
| 80 | + container_name: pg_db_17 |
| 81 | + labels: |
| 82 | + - "pgbackup.enable=true" |
| 83 | + environment: |
| 84 | + - POSTGRES_DB=testdb |
| 85 | + - POSTGRES_USER=testuser |
| 86 | + - POSTGRES_PASSWORD=testpass |
| 87 | +
|
| 88 | + dbbackup: |
| 89 | + image: justb4/pgbackup:17 |
| 90 | + container_name: pg_backup_17 |
| 91 | + volumes: |
| 92 | + - /var/run/docker.sock:/var/run/docker.sock |
| 93 | + - ./backup:/backup |
| 94 | + environment: |
| 95 | + - PGB_SCHEDULE=*/1 * * * * |
| 96 | + |
| 97 | +``` |
| 98 | + |
| 99 | +Then run using: |
| 100 | + |
| 101 | +``` |
| 102 | +docker-compose up -d |
| 103 | +``` |
| 104 | + |
| 105 | +It is advised to use explicit DB-container-naming, as backups will be stored in |
| 106 | +subdirectories (`year/month/<DB-container-name>-ymd-hm.sql.gz`). |
| 107 | + |
| 108 | +## Explicit Backups |
| 109 | + |
| 110 | +You can also run backups (and restores) explicitly, by calling `exec` on the `justb4/pgbackup` |
| 111 | +container, assuming `pgbackup` here. |
| 112 | + |
| 113 | +Backup all DBs containers: |
| 114 | + |
| 115 | +``` |
| 116 | +docker exec -it pgbackup /pgbackup/backup-all.sh |
| 117 | +
|
| 118 | +``` |
| 119 | + |
| 120 | +Or you can backup a single DB container: |
| 121 | + |
| 122 | +``` |
| 123 | +docker exec -it pgbackup /pgbackup/backup.sh <DB container-name> <backup file.sql.gz> |
| 124 | +
|
| 125 | +
|
| 126 | +# example |
| 127 | +docker exec -it pgbackup /pgbackup/backup.sh pgdb /backup/mybackup.sql.gz |
| 128 | +
|
| 129 | +``` |
| 130 | + |
| 131 | +## List Backups |
| 132 | + |
| 133 | +You can list all backups available in the container: |
| 134 | + |
| 135 | +``` |
| 136 | +docker exec -it pgbackup /pgbackup/list-backups.sh |
| 137 | +
|
| 138 | +``` |
| 139 | + |
| 140 | +## Restoring Backups |
| 141 | + |
| 142 | +This Docker Image also provides restore utilities. |
| 143 | + |
| 144 | +You can `bash` into the `justb4/pgbackup` container and run `restore.sh` or other commands |
| 145 | +from there. The following steps are needed: |
| 146 | + |
| 147 | +* if not already present copy your backup file, assuming `/backup/mybackup.sql.gz` here, into the `pgbackup` container mounted volume |
| 148 | +* figure out the name of your `justb4/pgbackup` container, assuming `pgbackup` here |
| 149 | +* figure out the name of your target DB container, assuming `pgdb` here |
| 150 | +* `bash` into the container: `docker exec -it pgbackup /bin/bash` |
| 151 | +* execute restore: `/pgbackup/restore.sh /backup/mybackup.sql.gz pgdb` |
| 152 | + |
| 153 | +You could also `exec` directly. Below an example: |
| 154 | + |
| 155 | +``` |
| 156 | +docker exec -it pgbackup /pgbackup/restore.sh pgdb /backup/2018/10/pgdb-181013-1050.sql.gz |
| 157 | +
|
| 158 | +``` |
| 159 | + |
| 160 | +## Design and diffs with kartoza/pg-backup |
| 161 | + |
| 162 | +Main difference is that `justb4/pgbackup` uses the Docker API to search within its Docker Network for |
| 163 | +Containers that have the Label `"pgbackup.enable=true"`. Using Labels in conjunction with the Docker API |
| 164 | +is found in many modern Docker-based services, like e.g. Traefik and Kubernetes. |
| 165 | + |
| 166 | +Each Container to be backed up is then further inspected to get the PostgreSQL credentials |
| 167 | +needed to connect with PG tools like `psql`. The Container name will be the PG Hostname |
| 168 | +(TODO: figure out IP address via Docker API, |
| 169 | +such that single backup/restores can be run commandline). |
| 170 | + |
| 171 | +This has the following advantages: |
| 172 | + |
| 173 | +* loose coupling, easy to setup |
| 174 | +* one `pgbackup` Container can backup multiple PostgreSQL Containers |
| 175 | +* no need to configure `pgbackup` with all PG credentials |
| 176 | + |
| 177 | +Further changes: |
| 178 | + |
| 179 | +* works with multiple Docker images for both PostgreSQL and PostGIS (mdillon and kartoza) |
| 180 | +* using smaller `postgres:<version>-alpine` as base image (i.s.o. `kartoza/postgis`) |
| 181 | +* schedule via env var `PGB_SCHEDULE` |
| 182 | +* dumps in SQL gzip format (more portable among PG versions) but may become option in futre |
| 183 | +* includes restore command to restore backup file in a named container |
| 184 | + |
| 185 | +## Credits |
| 186 | + |
| 187 | +* Tim Sutton (tim@kartoza.com) for https://github.com/kartoza/docker-pg-backup - Consulted Oct 2018 |
| 188 | +* Just van den Broecke (https://justobjects.nl) - this version - 2018 |
0 commit comments