TIL #2 - Configuring Offen docker volume backup

Backup revisited

In my previous post about container backup I planned to come up with a proper volume backup solution for my home environment.

After playing around with various settings and configuration of Offen I decided that I’ll use a dedicated container for each volume I want to back up. I ended up putting the necessary config in a single docker-compose.yml file.

This may be redundant and I’m sure there are better ways to do it, but it works for me.

A snippet of my over 300 lines long docker-compose.yml file:

 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
services:
  cops_backup:
    image: offen/docker-volume-backup:v2
    container_name: cops_backup
    restart: always
    network_mode: "bridge"
    environment:
      # Backup schedule
      BACKUP_CRON_EXPRESSION: "0 23 * * 5"
      # Backup filenames
      BACKUP_FILENAME: backup_cops-%Y-%m-%dT%H-%M-%S.tar.gz
      BACKUP_LATEST_SYMLINK: backup_cops.latest.tar.gz
      # Notifications
      NOTIFICATION_URLS: "discord://<omitted>@<omitted>"
      NOTIFICATION_LEVEL: "error"
      # Stop only the proper container
      BACKUP_STOP_CONTAINER_LABEL: cops
    volumes:
      # Volumes to backup
      - cops_config:/backup/data:ro
      # Docker socket needed for communicating with the docker daemon
      - /var/run/docker.sock:/var/run/docker.sock
      # Backup target directory
      - /volume1/docker/backup/cops:/archive

  linkding_backup:
    image: offen/docker-volume-backup:v2
    container_name: linkding_backup
    restart: always
    network_mode: "bridge"
    environment:
      # Backup schedule
      BACKUP_CRON_EXPRESSION: "05 23 * * 5"
      # Backup filenames
      BACKUP_FILENAME: backup_linkding-%Y-%m-%dT%H-%M-%S.tar.gz
      BACKUP_LATEST_SYMLINK: backup_linkding.latest.tar.gz
      # Notifications
      NOTIFICATION_URLS: "discord://<omitted>@<omitted>"
      NOTIFICATION_LEVEL: "error"
      # Stop only the proper container
      BACKUP_STOP_CONTAINER_LABEL: linkding
    volumes:
      # Volumes to backup
      - linkding_data:/backup/data:ro
      # Docker socket needed for communicating with the docker daemon
      - /var/run/docker.sock:/var/run/docker.sock
      # Backup target directory
      - /volume1/docker/backup/linkding:/archive

volumes:
  cops_config:
    external: true
  linkding_data:
    external: true

A little explanation of the config

Offen image

image: offen/docker-volume-backup:v2

I’m not using the latest tag for the offen image as this is suggested by the offen team.

Scheduling

I did not want to shut down everything at the same time (in the final config I have at least 15 containers) so I configured the cron jobs 5 minutes apart from each other, starting at 23:00 every Friday

1
2
# Backup schedule
BACKUP_CRON_EXPRESSION: "0 23 * * 5"

Selective container shutdown

BACKUP_STOP_CONTAINER_LABEL: cops

This env variable is used to identify the proper container and shut it down before backup. This must be paired with a label in the respective container’s configuration like so:

1
2
labels:
      - docker-volume-backup.stop-during-backup=cops

Accessing volumes outside of the backup containers

We need to make the volumes accessible for the backup containers with the external setting:

1
2
3
volumes:
  cops_config:
    external: true