- Print
- DarkLight
Integrate Restic with Backblaze B2
- Print
- DarkLight
Restic is an open-source backup tool that works with local storage, network-attached storage (NAS) devices, and Backblaze B2 Cloud Storage. Restic uses snapshots and block-level deduplication, along with complex indexing to allow for quick restores. All this while taking minimal storage space. You can use one Restic repository to contain the snapshots from one or multiple hosts because the host information is stored in the snapshot index.
You can install Restic from source code or binaries that you can download from GitHub. You can also install Restic using a local package manager on most operating systems. However, this article focuses on the Linux and Mac operating systems. Restic is available on Windows, but some features may not be available.
For more information about how to use Restic backups including options, creating schedules, and retention periods, see this video.
For a list of all of the Restic environment variables, click here.
Install Restic
Filesystem in the userspace (FUSE) is required to mount the snapshot backups, which is one of the restore options.
On Mac, use OSXFuse 3.10.6 if you run Catalina (10.15). Otherwise, use the current version of OSXFuse (now called MacFuse) if you are on BigSur.
On Linux, s3fuse works just as well as regular fuse, but it has components that allow you to mount Backblaze B2, which is compatible with S3.
- Install Restic for your operating system.
- For Redhat/CentOS, run the following command:
sudo yum install restic fuse
- For Ubuntu/Debian, run the following command:
sudo apt install restic fuse
- For macOS, run the following command:
brew install restic
- For Redhat/CentOS, run the following command:
- Run the following command to ensure that Restic is on the latest version:
restic self-update
Configure Restic
To simplify the use of Restic, Backblaze recommends that you define the Restic environment variables that you need in a file, such as /etc/restic-env
. This eliminates the need to pass every parameter each time you run Restic.
- Run the following command:
export B2_ACCOUNT_ID="012x11f5584568299998888zz" export B2_ACCOUNT_KEY="K012eFG6971Sshi/qrSSS897QC1dBfd" export RESTIC_REPOSITORY="b2:restic-west02" export RESTIC_PASSWORD_FILE=/etc/restic-password
The/etc/restic-password
can contain one line with the password. Knowledge of your password is required to access the repository. Losing your password means that your data is irrecoverably lost. - Run the following command:
mYsEcureP@$$word
- Run the following command to secure the Restic files so that only root or a user who you create can see the files:
chown root:root /etc/restic-env chown root:root /etc/restic-password chmod 700 /etc/restic-env chmod 700 /etc/restic-password
- Run the following command to initialize the repository:
source ~/restic-env restic -r b2:restic-west02 init created restic repository fd5c6a1116 at b2:restic-west02
Create a Backup
- Run the following command to load the environment variables:
source /etc/restic-env
- Run the following command to back up the
/usr/local directory
:restic -r b2:restic-west02 backup /usr/local repository fd5c6a11 opened successfully, password is correct created new cache in /home/administrator/.cache/restic
The following example shows a possible response:
Files: 2 new, 0 changed, 0 unmodified Dirs: 1 new, 0 changed, 0 unmodified Added to the repo: 19.097 MiB processed 2 files, 19.096 MiB in 0:01 snapshot 46225328 saved
Since the RESTIC_REPOSITORY
variable is defined, you do not need to add -r b2:restic-west02
to the commands.
The second backup backs up only changed files since it uses block-level deduplication. There are no changed files yet, so no files are backed up.
restic backup /usr/local repository fd5c6a11 opened successfully, password is correct Files: 0 new, 0 changed, 2 unmodified Dirs: 0 new, 0 changed, 1 unmodified Added to the repo: 0 B processed 2 files, 19.096 MiB in 0:01 snapshot 2a63bc2c saved
Backing up with tags is a useful way to identify your snapshots, identify which hosts they are from, and later prune unneeded snapshots. In the following example, a Windows server message block (SMB) is backed up on a Linux machine.
restic --tag Windows backup /mnt/Windows/ repository fd5c6a11 opened successfully, password is correct no parent snapshot found, will read all files Files: 165 new, 0 changed, 0 unmodified Dirs: 46 new, 0 changed, 0 unmodified Added to the repo: 6.347 GiB processed 165 files, 10.783 GiB in 3:53 snapshot 1b8e60ee saved
List all Restic Backups
To list of all the snapshots that you have, along with their data and time stamps and optional tags, run the following command:
restic snapshots repository fd5c6a11 opened successfully, password is correct ID Time Host Tags Paths ---------------------------------------------------------------------- 46225328 2021-06-23 07:54:37 restic-mike /usr/local 2a63bc2c 2021-06-23 08:18:30 restic-mike /usr/local 36754856 2021-06-23 12:20:40 restic-mike /usr/local 3d8081cb 2021-06-23 12:21:44 restic-mike /usr/local d0f97fe4 2021-06-23 12:22:16 restic-mike /usr/local/src d1c60d85 2021-06-23 12:28:41 restic-mike /usr/local/src 1b8e60ee 2021-06-27 07:14:24 restic-mike Windows /mnt/Windows ---------------------------------------------------------------------- 7 snapshots
Restore a Restic Backup
- To restore a snapshot to a directory, run the following command specifying the snapshot ID the target directory:
restic restore 1b8e60ee --target /tmp/restore repository fd5c6a11 opened successfully, password is correct restoring
Restic restores all of the files from the backup, with their full paths, starting under that directory.to /tmp/restore - Mount and browse the snapshot.
- To create a mount point for the Restic snapshots, run the following command:
mkdir /mnt/restic
- To begin another SSH or terminal session, run the following mount command in the background by appending a "&:"
restic mount /mnt/restic & ls hosts ids snapshots tags
- To browse backups by the host in which they were backed up, the snapshot ID, the date/time stamp, or the tags, run the following command:
cd /mnt/restic/ids/1b8e60ee ls mnt cd mnt/Windows pwd /mnt/restic/ids/1b8e60ee/mnt/Windows
- To create a mount point for the Restic snapshots, run the following command:
Run Restic in Docker
The following list explains the Docker flags that are used in the commands:
--rm
This command automatically removes the container when it exits.-t
This command allocates a pseudo-TTY (a pair of character special files, a master file, and a corresponding slave file).-i
This command keeps STDIN (standard input) open even if it is not attached.--entrypoint
This command overwrites the default ENTRYPOINT of the image.--env-file
This command defines the environment variable file to use.-v
This command mounts the local directory to a directory in Docker.
- Run the following command to install the official Restic Docker container:
docker pull restic/restic:latest
- Run the following command to create an environment file, such as
/etc/restic_env
, and define the following Restic variables:- Create your data directory in the Docker image that will be mapped to your local directly that you want to back up:
RESTIC_DATA
- Specify your Backblaze B2 bucket:
RESTIC_REPOSITORY=b2:’bucket-name’
- Specify your Restic repository password:
RESTIC_PASSWORD
- Specify your account ID:
B2_ACCOUNT_ID
- Specify your account key:
B2_ACCOUNT_KEY
The following example shows the possible commands to create your data directory in the Docker image:RESTIC_DATA=/data RESTIC_REPOSITORY=b2:restic-002 RESTIC_PASSWORD=test B2_ACCOUNT_ID=e8g010ff983a B2_ACCOUNT_KEY=09108d3ggacedba91823be5a3dd21eb6e6e336604d
- Create your data directory in the Docker image that will be mapped to your local directly that you want to back up:
- Run the following command to create a local data directory to store the data that you want to back up, or use an existing directory in the Docker command by substituting $HOME/restic-data with the directory that you want to back up:
mkdir $HOME/restic-data
- Run the following command to initialize the repository to create the structure that is needed for all of the backup files:
docker run --rm -ti --env-file=/etc/restic-env \ -v $HOME/restic-data:/data restic/restic init
- Run the following command to back up the local $HOME/restic-data repository:
docker run --rm -ti --env-file=/etc/restic-env \ -v $HOME/restic-data:/data restic/restic backup /data repository 15827d78 opened successfully, password is correct created new cache in /root/.cache/restic no parent snapshot found, will read all files Files: 165 new, 0 changed, 0 unmodified Dirs: 46 new, 0 changed, 0 unmodified Added to the repo: 6.346 GiB
- Run the following command to list all of the files in the latest backup (caution: this list can be lengthy):
docker run --rm -ti --env-file=/etc/restic-env \ -v $HOME/restic-data:/data restic/restic ls -l latest
- Run this command to list of all your snapshots with their data and time stamps and optional tags:
docker run --rm -ti --env-file=/etc/restic-env \ -v $HOME/restic-data:/data restic/restic snapshots
Restore a Backup
To restore a snapshot to a directory, specify the snapshot ID and specify the target directory. Restic restores all of the files from the backup, with their full paths, starting under that directory.
- Run the following command:
docker run --rm -ti --env-file=/etc/restic-env \ -v $HOME/restic-data:/data -v $HOME/restic-restore:/restore \ restic/restic restore 5d1487e5 --target /restore repository 15827d78 opened successfully, password is correct created new cache in /root/.cache/restic restoring
to /restore - Run the following command to restore the latest backup:
docker run --rm -ti --env-file=/etc/restic-env \ -v $HOME/restic-data:/data -v $HOME/restic-restore:/restore \ restic/restic restore latest --target /restore
Check Repository Health
You should check your Restic repository for errors. By default, the check
command loads all data directly from the repository, and it does not use a local cache.
docker run --rm -ti --env-file=/etc/restic-env \ -v $HOME/restic-data:/data restic/restic check