Ongoing physical backups with Docker & WAL-E
deprecation
This section describes a feature that is deprecated on TimescaleDB. We strongly recommend that you do not use this feature in a production environment. If you need more information, contact the support team.
To make TimescaleDB use the WAL-E sidecar for archiving, the two containers need to share a network. To do this, you need to create a Docker network and then launch TimescaleDB with archiving turned on, using the newly created network. When you launch TimescaleDB, you need to explicitly set the location of the write-ahead log (POSTGRES_INITDB_WALDIR
) and data directory (PGDATA
) so that you can share them with the WAL-E sidecar. Both must reside in a Docker volume, by default a volume is created for /var/lib/postgresql/data
. When you have started TimescaleDB, you can log in and create tables and data.
Create the docker container:
Launch TimescaleDB, with archiving turned on:
docker run \
--name timescaledb \
--network timescaledb-net \
-e POSTGRES_PASSWORD=insecure \
-e POSTGRES_INITDB_WALDIR=/var/lib/postgresql/data/pg_wal \
-e PGDATA=/var/lib/postgresql/data/pg_data \
timescale/timescaledb:latest-pg10 postgres \
-cwal_level=archive \
-carchive_mode=on \
-carchive_command="/usr/bin/wget wale/wal-push/%f -O -" \
-carchive_timeout=600 \
-ccheckpoint_timeout=700 \
-cmax_wal_senders=1
Run TimescaleDB within Docker:
docker exec -it timescaledb psql -U postgres
The WAL-E Docker image runs a web endpoint that accepts WAL-E commands across an HTTP API. This allows PostgreSQL to communicate with the WAL-E sidecar over the internal network to trigger archiving. You can also use the container to invoke WAL-E directly. The Docker image accepts standard WAL-E environment variables to configure the archiving backend, so you can issue commands from services such as AWS S3. See for more details.
Start the WAL-E container with the required information about the container. In this example, the container is called
timescaledb-wale
:docker run \
--name wale \
--network timescaledb-net \
-v ~/backups:/backups \
-e WALE_LOG_DESTINATION=stderr \
-e PGWAL=/var/lib/postgresql/data/pg_wal \
-e PGDATA=/var/lib/postgresql/data/pg_data \
-e PGHOST=timescaledb \
-e PGPASSWORD=insecure \
-e PGUSER=postgres \
-e WALE_FILE_PREFIX=file://localhost/backups \
timescale/timescaledb-wale:latest
Start the backup:
Alternatively, you can start the backup using the sidecar’s HTTP endpoint. This requires exposing the sidecar’s port 80 on the Docker host by mapping it to an open port. In this example, we map it to port 8080:
curl http://localhost:8080/backup-push
You should do base backups at regular intervals, we recommend daily, to minimize the amount of WAL-E replay, and to make recoveries faster. To make new base backups, re-trigger a base backup as shown here, either manually or on a schedule. If you run TimescaleDB on Kubernetes, there is built-in support for scheduling cron jobs that can invoke base backups using the WAL-E container’s HTTP API.
To recover the database instance from the backup archive, create a new TimescaleDB container, and restore the database and configuration files from the base backup. Then you can relaunch the sidecar and the database.
Create the docker container:
docker create \
--name timescaledb-recovered \
--network timescaledb-net \
-e POSTGRES_PASSWORD=insecure \
-e POSTGRES_INITDB_WALDIR=/var/lib/postgresql/data/pg_wal \
-e PGDATA=/var/lib/postgresql/data/pg_data \
timescale/timescaledb:latest-pg10 postgres
-
docker run -it --rm \
-v ~/backups:/backups \
--volumes-from timescaledb-recovered \
-e WALE_LOG_DESTINATION=stderr \
-e WALE_FILE_PREFIX=file://localhost/backups \
timescale/timescaledb-wale:latest \wal-e \
backup-fetch /var/lib/postgresql/data/pg_data LATEST
Recreate the configuration files. These are backed up from the original database instance:
Create a
recovery.conf
file that tells PostgreSQL how to recover:docker run -it --rm \
--volumes-from timescaledb-recovered \
timescale/timescaledb:latest-pg10 \
sh -c 'echo "restore_command='\''/usr/bin/wget wale/wal-fetch/%f -O -'\''" > /var/lib/postgresql/data/pg_data/recovery.conf'
When you have recovered the data and the configuration files, and have created a recovery configuration file, you can relaunch the sidecar. You might need to remove the old one first. When you relaunch the sidecar, it replays the last WAL segments that might be missing from the base backup. The you can relaunch the database, and check that recovery was successful.
Relaunch the WAL-E sidecar:
docker run \
--name wale \
--network timescaledb-net \
-v ~/backups:/backups \
--volumes-from timescaledb-recovered \
-e WALE_LOG_DESTINATION=stderr \
-e PGWAL=/var/lib/postgresql/data/pg_wal \
-e PGDATA=/var/lib/postgresql/data/pg_data \
-e PGHOST=timescaledb \
-e PGPASSWORD=insecure \
-e PGUSER=postgres \
-e WALE_FILE_PREFIX=file://localhost/backups \
timescale/timescaledb-wale:latest
Relaunch the TimescaleDB docker container:
docker start timescaledb-recovered
Verify that the database started up and recovered successfully: