Linuxpad

Got Linux?

Automatically Backup Docker Volumes using restic

Automatically Backup Docker Volumes using restic

This blog post is about restic, a simple, beautiful, and extremely versatile file backup solution written in Go. It is very easy to install and configure and just works pretty neat out of the box. Even though restic backup is not for everyone, I quickly fell in love with its simplicity and enjoy the peace of mind it gives me when using it for backing up my Linux servers.

Restic is a very simple and powerful file-level backup solution that was written in go and is completely open-source. It is available for Linux, BSD, macOS, and Windows and can be used with a variety of different storage backends ranging from local storage, to SFTP, REST, S3 as well as Azure blob storage and Google cloud storage. Even though restic is awesome, it is also different from a lot of well-known backup software players such as Veeam, Acronis, or Backup Exec.

Installation:

Now copy this code snippet to the docker-compose.yaml file:

version: "3.3"

services:
  backup:
    image: mazzolino/restic
    container_name: restic
    hostname: nana
    environment:
      RUN_ON_STARTUP: "true" #change as you wish
      BACKUP_CRON: "0 */12 * * *" #this is twice daily, i.e., every 12 hours
      RESTIC_REPOSITORY: /restic
      RESTIC_PASSWORD: Afiaanimah*123
      RESTIC_BACKUP_SOURCES: /mnt/docker-volumes  /home/nana/docker
      RESTIC_COMPRESSION: auto 
      RESTIC_BACKUP_ARGS: >-
        --tag docker-volumes
        --exclude some-folder/cache
        --exclude another-folder/with\ space
        --exclude *.tmp
        --verbose
      RESTIC_FORGET_ARGS: >- #change as required
        --keep-last 10
        --keep-daily 7
        --keep-weekly 5
        --keep-monthly 12
      TZ: Europe/London
    volumes:
      - /home/nana/docker/nas/backup:/restic #change the left hand side to where you want to store the backups. As you can see I'm storing it on my NAS that is mounted to the host /home/truenas
      - /home/nana/docker/nas/tmp-for-restore:/tmp-for-restore #USE THIS FOLDER FOR RESTORE - CAN VIEW EACH CONTAINER
      - /var/lib/docker/volumes:/mnt/docker-volumes:ro
      - /home/nana/docker:/home/nana/docker:ro
    security_opt:
      - no-new-privileges:true

  prune:
    image: mazzolino/restic
    container_name: restic-prune
    hostname: nana
    environment:
      RUN_ON_STARTUP: "true"
      PRUNE_CRON: "0 0 4 * * *"
      RESTIC_REPOSITORY: /restic
      RESTIC_PASSWORD: Afiaanimah*123
      TZ: Europe/London
    security_opt:
      - no-new-privileges:true

  check:
    image: mazzolino/restic
    container_name: restic-check
    hostname: nana
    environment:
      RUN_ON_STARTUP: "false"
      CHECK_CRON: "0 15 5 * * *"
      RESTIC_CHECK_ARGS: >-
        --read-data-subset=10%
      RESTIC_REPOSITORY: /restic
      RESTIC_PASSWORD: Afiaanimah*123
      TZ: Europe/London
    security_opt:
      - no-new-privileges:true

snapshots:



❯ docker exec restic restic snapshots
ID        Time                 Host        Tags            Paths                Size
-----------------------------------------------------------------------------------------
5e086208  2025-02-21 17:16:22  nana        restic-proxmox  /mnt/docker-volumes  2.539 GiB

162717e9  2025-02-21 17:27:51  nana        restic-docker   /home/nana           3.973 GiB
                                                           /mnt/docker-volumes
-----------------------------------------------------------------------------------------
2 snapshots