diff --git a/Changelog.md b/Changelog.md index 942e0ae..638ad1e 100644 --- a/Changelog.md +++ b/Changelog.md @@ -9,6 +9,7 @@ - added `PG_SSL` parameter to enable/disable SSL support - `DB_LOCALE` config parameter renamed to `PG_LOCALE` - complete rewrite of the README +- add support for creating backups using `pg_basebackup` **9.4-2** - added replication options diff --git a/README.md b/README.md index 323c1e1..89f8a88 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ - [Creating replication user](#creating-replication-user) - [Setting up a replication cluster](#setting-up-a-replication-cluster) - [Creating a snapshot](#creating-a-snapshot) + - [Creating a backup](#creating-a-backup) - [Logs](#logs) - [UID/GID mapping](#uid-gid-mapping) - [Maintenance](#maintenance) @@ -219,7 +220,7 @@ docker run --name postgresql -itd --restart always \ > - No changes will be made if the user already exists > - Only a single user can be created at each launch -*It is a good idea to create a replication user even if you are not going to use it as it will allow you to setup slave nodes and/or generate snapshots when the need arises.* +*It is a good idea to create a replication user even if you are not going to use it as it will allow you to setup slave nodes and/or generate snapshots and backups when the need arises.* ## Setting up a replication cluster @@ -284,6 +285,26 @@ The difference between a slave and a snapshot is that a slave is read-only and u This is useful for developers to quickly snapshot the current state of a live database and use it for development/debugging purposes without altering the database on the live instance. +## Creating a backup + +Just as the case of setting up a slave node or generating a snapshot, you can also create a backup of the data on the master master by specifying `REPLICATION_MODE=backup`. + +> The backups are generated using [pg_basebackup](http://www.postgresql.org/docs/9.4/static/app-pgbasebackup.html) using the replication protocol. + +Once the master node is created as specified in [Setting up a replication cluster](#setting-up-a-replication-cluster), you can create a point-in-time backup using: + +```bash +docker run --name postgresql-backup -it --rm \ + --link postgresql-master:master \ + --env 'REPLICATION_MODE=backup' -e 'REPLICATION_SSLMODE=prefer' \ + --env 'REPLICATION_HOST=master' -e 'REPLICATION_PORT=5432' \ + --env 'REPLICATION_USER=repluser' --env 'REPLICATION_PASS=repluserpass' \ + --volume /srv/docker/backups/postgresql.$(date +%Y%m%d%H$M%S):/var/lib/postgresql \ + sameersbn/postgresql:9.4-8 +``` + +Once the backup is generated, the container will exit and the backup of the master data will be available at `/srv/docker/backups/postgresql.XXXXXXXXXXXX/`. Restoring the backup involves starting a container with the data in `/srv/docker/backups/postgresql.XXXXXXXXXXXX`. + ## Logs By default the PostgreSQL server logs are sent to the standard output. Using the [Command-line arguments](#command-line-arguments) feature you can configure the PostgreSQL server to send the log output to a file using the `-c logging_collector=on` argument: diff --git a/runtime/functions b/runtime/functions index 52c62df..21759ab 100755 --- a/runtime/functions +++ b/runtime/functions @@ -117,7 +117,7 @@ configure_ssl() { configure_hot_standby() { case ${REPLICATION_MODE} in - slave|snapshot) ;; + slave|snapshot|backup) ;; *) echo "Configuring hot standby..." set_postgresql_param "wal_level" "hot_standby" @@ -132,7 +132,7 @@ configure_hot_standby() { initialize_database() { if [[ ! -f ${PG_DATADIR}/PG_VERSION ]]; then case ${REPLICATION_MODE} in - slave|snapshot) + slave|snapshot|backup) # default params REPLICATION_PORT=${REPLICATION_PORT:-5432} REPLICATION_SSLMODE=${REPLICATION_SSLMODE:-prefer} @@ -177,6 +177,12 @@ initialize_database() { exec_as_postgres PGPASSWORD=$REPLICATION_PASS ${PG_BINDIR}/pg_basebackup -D ${PG_DATADIR} \ -h ${REPLICATION_HOST} -p ${REPLICATION_PORT} -U ${REPLICATION_USER} -X fetch -w >/dev/null ;; + backup) + echo "Backing up data on $REPLICATION_HOST..." + exec_as_postgres PGPASSWORD=$REPLICATION_PASS ${PG_BINDIR}/pg_basebackup -D ${PG_DATADIR} \ + -h ${REPLICATION_HOST} -p ${REPLICATION_PORT} -U ${REPLICATION_USER} -X fetch -w >/dev/null + exit 0 + ;; esac ;; *) @@ -289,7 +295,7 @@ create_database() { create_replication_user() { case $REPLICATION_MODE in - slave|snapshot) ;; # replication user can only be created on the master + slave|snapshot|backup) ;; # replication user can only be created on the master *) if [[ -n ${REPLICATION_USER} ]]; then if [[ -z ${REPLICATION_PASS} ]]; then