Restic Backup Docker Container
This is a docker container I use for backups on my QNAP NAS. It is largely based on Lobaro's initial framework you can find here.
This container runs restic backups in regular intervals using cron. It supports some configurability which helps before full-scale deployment.
The container is based on:
- a 64-bit Linux binary, that is downloaded from the restic releases page.
We don't compile it ourselves, but we check its SHA256 hash for conformity.
The binary is installed as
/bin/restic - scripts for setting-up the cron job, called
backup/entrypoint.shand running the backup jobs, calledbackup/run.sh. The filebackup/functions.shcontains shared functions between the two scripts and checks for environment sanity that are run every time each of these two main scripts are called. backup/entrypoint.shis called once per container instantiation, whilebackup/run.shis called everytime we need to perform a scheduled backup.- The backup program in
backup/run.shis based on the environment settings you must pass when running the docker container using-eoptions todocker run. You're supposed to override the variables using this technique. Alternatively, clone this repository, modify the variables inDockerfileand rebuild the image to your liking.
The cron job is set up when the container is started. From this point onwards,
the only thing the container outputs is the equivalent of tail -f
/var/log/cron.log.
You can build the container yourself using the following command:
$ docker build --rm -t anjos/backuper:latest .
I recommend you start running local tests to check your system is sane before going for cloud-based setups.
Edit the file envs/local.env to your liking. Create a data/repo directories
on your working directory and initialize the restic repository:
$ mkdir data #fill-in with toy-data to "backup" $ mkdir repo #don't put anything $ restic -r repo init #use password "test" or change file envs/local.env ...
Test the container with the following command:
$ docker run -t --env-file envs/local.env -v `pwd`/data:/data -v `pwd`/repo:/repo anjos/backuper:latest
If you don't change the values in envs/local.env, the cron job will run every
minute. Use the contents of ./data to add/remove contents simulating your
usage. Backups are stored in ./repo using a simple file backend. The
password is test. The last 3 backups are kept.
Backblaze's B2 backup solution provides a free-tier which is useful to run
quick tests without incurring in cost. Edit the file envs/b2.env to your
liking. Create a directory named data on your working directory that will
contain your test data to backup. Initialize the B2's bucket:
$ mkdir data #fill-in with toy-data to "backup" $ b2 authorized-account <set-account-id> <set-account-key> $ b2 create-bucket test allPrivate $ B2_ACCOUNT_ID=<set-account-id> B2_ACCOUNT_KEY=<set-account-key> restic -r 'b2:test' init ...
Test the container with the following command:
$ docker run -t --env-file envs/b2.env -v `pwd`/data:/data anjos/backuper:latest
If you don't change the values in envs/b2.env, the cron job will run
every minute. Use the contents of ./data to add/remove contents simulating
your usage. Backups are stored in ./backup using a simple file backend. The
password is test. The last 3 backups are kept.
Note
To delete all files in a bucket and then delete the bucket, do:
$ mkdir empty $ cd empty $ b2 authorize-account ... $ b2 sync --delete . b2://mybucketname $ b2 delete-bucket mybucketname
To start a new container, execute:
$ docker run -ti --env-file envs/local.env -v `pwd`/data:/data -v `pwd`/repo:/repo --entrypoint=/bin/bash anjos/backuper:latest '-e'
From this point you can use restic and check things for yourself.