Backing Up a Container’s Data Volume with One Command

Preface

When I first created the miniflux container, I used the official docker-compose.yml configuration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
services:
miniflux:
image: miniflux/miniflux:latest
ports:
- "80:8080"
depends_on:
db:
condition: service_healthy
environment:
- DATABASE_URL=postgres://miniflux:secret@db/miniflux?sslmode=disable
- RUN_MIGRATIONS=1
- CREATE_ADMIN=1
- ADMIN_USERNAME=admin
- ADMIN_PASSWORD=test123
db:
image: postgres:15
environment:
- POSTGRES_USER=miniflux
- POSTGRES_PASSWORD=secret
- POSTGRES_DB=miniflux
volumes:
- miniflux-db:/var/lib/postgresql/data
healthcheck:
test: ["CMD", "pg_isready", "-U", "miniflux"]
interval: 10s
start_period: 30s
volumes:
miniflux-db:

Later, I noticed that the official configuration used a named data volume miniflux-db. This allows the database directory to be shared between multiple containers, although, in this case, sharing and reading the database directory is unnecessary. However, this has a drawback: if a system failure occurs, there might not be enough time to back up the Docker data volume, or backing it up might be overlooked entirely. The database holds a wealth of RSS links and articles accumulated from long-term subscriptions, forming a valuable dataset that could potentially serve as a knowledge base. Losing this data would be a significant loss.

I researched the issue online. The methods I found were all similar, involving creating a temporary container, mounting the directory, using tar to create a compressed archive, and then unpacking it with a container. However, after reviewing the Docker commands, I discovered a single-command solution:

1
docker cp miniflux-db-1:/var/lib/postgresql/data $(pwd)/miniflux-db/

This command backs up the data. Create a directory named miniflux-db in the same location as your docker-compose.yml file. This will serve as the destination for the backup. In this command, miniflux-db-1 represents the name of your Miniflux database container. Executing this command copies the database contents to the miniflux-db directory.

Now, you can modify the docker-compose.yml entry:

1
2
3
4
db:
volumes:
# - miniflux-db:/var/lib/postgresql/data
- ./miniflux-db:/var/lib/postgresql/data

Finally, you can remove the data volume definition from the configuration, as it’s no longer necessary:

1
2
volumes:
miniflux-db: