Skip to content

Instantly share code, notes, and snippets.

@rockerBOO
Last active October 4, 2023 22:43
Show Gist options
  • Save rockerBOO/48b777868f169042818ae7f66bc62502 to your computer and use it in GitHub Desktop.
Save rockerBOO/48b777868f169042818ae7f66bc62502 to your computer and use it in GitHub Desktop.
How to backup a Docker volume using Volumerize

How to backup a Docker Volume

Docker supports volumes for storing data across containers. This also allows us to mount the volume in a docker container to backup this volume. Using Volumerize we can mount our volume and back it up to a separate volume or many backend options.

Volumerize is a docker container that can attach your docker volumes and backup the files of the volume. It does this with a cronjob in the container or runs manually.

Volumerize uses Duplicity to make the actual backup. Supports many backup backend options like S3, Dropbox, and the local file system.

Backup

Backing up works by moving the files from a volume to a volume or service in the container.

  • -v data_volume:/source Attach your volume to a folder in the container.

  • -v /home/you/backup:/backup Attach your backup volume to the docker instance

  • If using Windows docker you will need to make sure your drive is attached -v F:/backups:/backup

Then we apply the VOLUMERIZE environmental variables to define the options.

  • -e "VOLUMERIZE_SOURCE=/source"
  • -e "VOLUMERIZE_TARGET=file:///backup"

VOLUMERIZE_TARGET is where you can define the different backend options.

All containers who mount the volume should be turned off to not have data corruption issues. Volumerize can handle stopping and starting containers for you. You will also need to attach the docker socket so it can call the docker commands inside the container.

  • -e "VOLUMERIZE_CONTAINERS=db_container"
  • -v /var/run/docker.sock:/var/run/docker.sock

The full docker run statement for generating a backup. This version just creates a backup and self-destructs.

docker run -it --rm  --name volumerize -v /home/you/backup:/backup -v data_volume:/source:ro  -e "VOLUMERIZE_SOURCE=/source" -e "VOLUMERIZE_TARGET=file:///backup" blacklabelops/volumerize backup

You should then see 3 files in your backup volume. These files are defined by the Duplicity process. They can be used for restoring a backup to a volume.

Restore

You should always test restore to make sure your backup is working properly.

docker run --rm -v data_volume:/source -v /home/you/backup:/backup:ro -e "VOLUMERIZE_SOURCE=/source" -e "VOLUMERIZE_TARGET=file:///backup" blacklabelops/volumerize restore

Automated

By default, the Volmerize will run as an automated script running on the crontab.

docker run -d -v data_volume:/source -v /home/you/backup:/backup:ro -e "VOLUMERIZE_SOURCE=/source" -e "VOLUMERIZE_TARGET=file:///backup" blacklabelops/volumerize

The cron will run at the default time of 4 am.

- name: VolumerizeBackupJob
  cmd: /etc/volumerize/periodicBackup
  time: '0 0 4 * * *'
  onError: Continue
  notifyOnError: false
  notifyOnFailure: false

More details about what is possible is available on the Volumerize Github Page

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment