Backing up the volume
Copy a server's data volume into a .tar.gz file and put it back into a volume later. Use it before an upgrade, before moving to another host, or as a simple nightly copy where a few seconds of downtime are acceptable. For backups without downtime and point-in-time restore, use S3 backups instead.
What you get
backup.sh/backup.ps1: stops the container, archives the whole volume with owners and modes kept, checks that the archive reads back, and starts the container again (also when the copy fails).restore.sh/restore.ps1: unpacks an archive into a new or empty volume, owned by uid 10001 as the server expects. It refuses a volume that already holds anything.
The server is stopped for the copy because its files are only consistent with each other when it is not running. A copy of a running server's files can miss writes or hold half of one. Stopping it is a clean shutdown, so the copy needs no recovery when it is started.
Prerequisites
- Docker. The scripts run
tarinside the RasterizedDB image you already have (milendenev/rasterizeddb:1.1.0; setIMAGE, or-Image, for another tag). Use an Ubuntu-based tag: the scripts expect GNUtar, and thealpinetags have BusyBox's. - Free disk space for the compressed copy.
What to edit
Nothing in the files. Each script takes its targets as arguments:
| Script | Arguments |
|---|---|
backup.sh, backup.ps1 | the container name, the volume name, and the output folder (default: the current folder) |
restore.sh, restore.ps1 | the archive, and the volume to restore into |
Find the names with:
docker ps --format '{{.Names}}'
docker volume lsFor the single-container recipe they are rasterizeddb and rasterizeddb-data. A Compose project names them after the project: Compose basic has the container rasterizeddb-rasterizeddb-1 and the volume rasterizeddb_data.
Back up
Linux and macOS:
./backup.sh rasterizeddb rasterizeddb-data ./backupsWindows:
.\backup.ps1 rasterizeddb rasterizeddb-data .\backupsStopping rasterizeddb for a consistent copy...
Started rasterizeddb again.
Wrote /home/you/backups/rasterizeddb-data-20261127T101500Z.tar.gz (12M).The file name carries the time of the copy in UTC. Keep the copies somewhere other than the Docker host.
To run it every night, add a cron entry (Linux) or a scheduled task (Windows) that calls the script with the same arguments.
Restore
Restore into a new volume, then start a server on it.
./restore.sh backups/rasterizeddb-data-20261127T101500Z.tar.gz rasterizeddb-restored.\restore.ps1 .\backups\rasterizeddb-data-20261127T101500Z.tar.gz rasterizeddb-restoredThen:
-
Single container: remove the old container and start one on the restored volume, with
run.shfrom the single-container recipe:shdocker rm -f rasterizeddb VOLUME=rasterizeddb-restored ./run.sh -
Compose: to keep the project's own volume name, remove the old volume first and restore into that name:
shdocker compose down docker volume rm rasterizeddb_data ./restore.sh backups/rasterizeddb-data-20261127T101500Z.tar.gz rasterizeddb_data docker compose up -dCompose may warn that the volume was not created by Compose. It uses it all the same.
A restored volume keeps the roles and passwords of the copy. RASTERIZEDDB_PG_PASSWORD does not change them, because it is only used when a server starts with no roles at all.
Check that it works
List what an archive holds without restoring it:
tar -tzf backups/rasterizeddb-data-20261127T101500Z.tar.gz | headAfter a restore, connect and read a table you know:
psql "host=127.0.0.1 port=5432 user=rasterized dbname=postgres sslmode=require" -c "SELECT COUNT(*) FROM greetings"Clean up
The archives are ordinary files: delete old ones yourself. A restored volume you no longer need:
docker volume rm rasterizeddb-restoredLimits
- The server is down while the copy runs, for as long as
tartakes over the whole volume. - A copy restores the database exactly as it was at the copy, nothing later.
- A restored copy is the same installation as the original. Never run both at once with backup or replication flags: they would write into the same S3 backup prefix, or appear to the cluster as the same node. To run a copy beside the original for testing, start it without those flags.
- A database restored to an earlier point than its replication peers have seen reports
needs reseedfor those peers. See Replication.
The files
#!/usr/bin/env bash
# Copies a RasterizedDB data volume into a .tar.gz file, consistently: the
# container using the volume is stopped for the copy and started again after.
#
# usage: ./backup.sh <container> <volume> [output directory]
# ./backup.sh rasterizeddb rasterizeddb-data ./backups
# ./backup.sh rasterizeddb-rasterizeddb-1 rasterizeddb_data ./backups # Compose
#
# The copy is made by `tar` inside the RasterizedDB image (set IMAGE to use
# another image with GNU tar), as root, so it can read the server's files.
set -euo pipefail
CONTAINER="${1:?usage: ./backup.sh <container> <volume> [output directory]}"
VOLUME="${2:?usage: ./backup.sh <container> <volume> [output directory]}"
OUT="${3:-.}"
IMAGE="${IMAGE:-milendenev/rasterizeddb:1.1.0}"
docker volume inspect "$VOLUME" >/dev/null
mkdir -p "$OUT"
OUT="$(cd "$OUT" && pwd)"
FILE="$VOLUME-$(date -u +%Y%m%dT%H%M%SZ).tar.gz"
running="$(docker container inspect -f '{{.State.Running}}' "$CONTAINER")"
start_again() {
if [ "$running" = true ]; then
docker start "$CONTAINER" >/dev/null
echo "Started $CONTAINER again."
fi
}
if [ "$running" = true ]; then
echo "Stopping $CONTAINER for a consistent copy..."
docker stop -t 60 "$CONTAINER" >/dev/null
trap start_again EXIT
fi
docker run --rm --user 0 --entrypoint tar \
-v "$VOLUME:/data:ro" \
--mount "type=bind,source=$OUT,target=/backup" \
"$IMAGE" --numeric-owner -czf "/backup/$FILE" -C /data .
# Readable back, and owned by you rather than root.
docker run --rm --user 0 --entrypoint sh \
--mount "type=bind,source=$OUT,target=/backup" \
"$IMAGE" -c "tar -tzf '/backup/$FILE' >/dev/null && chown $(id -u):$(id -g) '/backup/$FILE'"
echo "Wrote $OUT/$FILE ($(du -h "$OUT/$FILE" | cut -f1))."#!/usr/bin/env bash
# Unpacks an archive made by backup.sh into a data volume. The volume must be
# new or empty: this never writes over existing data.
#
# usage: ./restore.sh <archive.tar.gz> <volume>
# ./restore.sh backups/rasterizeddb-data-20261127T101500Z.tar.gz rasterizeddb-restored
set -euo pipefail
ARCHIVE="${1:?usage: ./restore.sh <archive.tar.gz> <volume>}"
VOLUME="${2:?usage: ./restore.sh <archive.tar.gz> <volume>}"
IMAGE="${IMAGE:-milendenev/rasterizeddb:1.1.0}"
[ -f "$ARCHIVE" ] || { echo "No such file: $ARCHIVE" >&2; exit 1; }
DIR="$(cd "$(dirname "$ARCHIVE")" && pwd)"
NAME="$(basename "$ARCHIVE")"
if docker volume inspect "$VOLUME" >/dev/null 2>&1; then
if ! docker run --rm --user 0 --entrypoint sh -v "$VOLUME:/data" "$IMAGE" \
-c '[ $(ls -A /data | wc -l) -eq 0 ]'; then
echo "Volume $VOLUME is not empty. Restore into a new volume, or remove this one first." >&2
exit 1
fi
else
docker volume create "$VOLUME" >/dev/null
fi
# As root, so the files keep their owner (uid 10001) and modes.
docker run --rm --user 0 --entrypoint sh \
-v "$VOLUME:/data" \
--mount "type=bind,source=$DIR,target=/backup,readonly" \
"$IMAGE" -c "tar --numeric-owner -xzpf '/backup/$NAME' -C /data && chown 10001:10001 /data && chmod 700 /data && sync"
echo "Restored $ARCHIVE into volume $VOLUME."# Copies a RasterizedDB data volume into a .tar.gz file, consistently: the
# container using the volume is stopped for the copy and started again after.
#
# usage: .\backup.ps1 <container> <volume> [output directory]
# .\backup.ps1 rasterizeddb rasterizeddb-data .\backups
# .\backup.ps1 rasterizeddb-rasterizeddb-1 rasterizeddb_data .\backups # Compose
#
# The copy is made by `tar` inside the RasterizedDB image (-Image to use
# another image with GNU tar), as root, so it can read the server's files.
param(
[Parameter(Mandatory = $true, Position = 0)] [string] $Container,
[Parameter(Mandatory = $true, Position = 1)] [string] $Volume,
[Parameter(Position = 2)] [string] $OutDir = '.',
[string] $Image = 'milendenev/rasterizeddb:1.1.0'
)
$ErrorActionPreference = 'Stop'
docker volume inspect $Volume | Out-Null
if ($LASTEXITCODE -ne 0) { throw "There is no volume named $Volume." }
New-Item -ItemType Directory -Force -Path $OutDir | Out-Null
$out = (Resolve-Path $OutDir).Path
$file = "$Volume-$((Get-Date).ToUniversalTime().ToString('yyyyMMddTHHmmssZ')).tar.gz"
$running = (docker container inspect -f '{{.State.Running}}' $Container)
if ($LASTEXITCODE -ne 0) { throw "There is no container named $Container." }
if ($running -eq 'true') {
Write-Host "Stopping $Container for a consistent copy..."
docker stop -t 60 $Container | Out-Null
if ($LASTEXITCODE -ne 0) { throw "docker stop $Container failed" }
}
try {
docker run --rm --user 0 --entrypoint tar `
-v "${Volume}:/data:ro" `
--mount "type=bind,source=$out,target=/backup" `
$Image --numeric-owner -czf "/backup/$file" -C /data .
if ($LASTEXITCODE -ne 0) { throw 'tar failed; no complete backup was written' }
docker run --rm --user 0 --entrypoint tar `
--mount "type=bind,source=$out,target=/backup" `
$Image -tzf "/backup/$file" | Out-Null
if ($LASTEXITCODE -ne 0) { throw "the archive $file does not read back" }
}
finally {
if ($running -eq 'true') {
docker start $Container | Out-Null
Write-Host "Started $Container again."
}
}
Write-Host "Wrote $(Join-Path $out $file)."# Unpacks an archive made by backup.ps1 (or backup.sh) into a data volume.
# The volume must be new or empty: this never writes over existing data.
#
# usage: .\restore.ps1 <archive.tar.gz> <volume>
# .\restore.ps1 .\backups\rasterizeddb-data-20261127T101500Z.tar.gz rasterizeddb-restored
param(
[Parameter(Mandatory = $true, Position = 0)] [string] $Archive,
[Parameter(Mandatory = $true, Position = 1)] [string] $Volume,
[string] $Image = 'milendenev/rasterizeddb:1.1.0'
)
$ErrorActionPreference = 'Stop'
if (-not (Test-Path -PathType Leaf $Archive)) { throw "No such file: $Archive" }
$full = (Resolve-Path $Archive).Path
$dir = Split-Path -Parent $full
$name = Split-Path -Leaf $full
$existing = docker volume ls -q --filter "name=^${Volume}$"
if ($LASTEXITCODE -ne 0) { throw 'docker volume ls failed' }
if ($existing -contains $Volume) {
docker run --rm --user 0 --entrypoint sh -v "${Volume}:/data" $Image -c '[ $(ls -A /data | wc -l) -eq 0 ]'
if ($LASTEXITCODE -ne 0) { throw "Volume $Volume is not empty. Restore into a new volume, or remove this one first." }
}
else {
docker volume create $Volume | Out-Null
if ($LASTEXITCODE -ne 0) { throw "docker volume create $Volume failed" }
}
# As root, so the files keep their owner (uid 10001) and modes.
docker run --rm --user 0 --entrypoint sh `
-v "${Volume}:/data" `
--mount "type=bind,source=$dir,target=/backup,readonly" `
$Image -c "tar --numeric-owner -xzpf '/backup/$name' -C /data && chown 10001:10001 /data && chmod 700 /data && sync"
if ($LASTEXITCODE -ne 0) { throw 'the restore failed' }
Write-Host "Restored $Archive into volume $Volume."