How to configure Backblaze B2 with Restic on Linux

Restic is a multi-platform command line backup software program that is designed to be fast, efficient, and secure. Restic supports a variety of backends for storing backups, including a local server, SFTP server, HTTP Rest server, and a number of cloud storage providers, including Backblaze B2.

Files are uploaded to a B2 bucket as de-duplicated, encrypted chunks. Each time a backup runs, only changed data is backed up. On each backup run, a snapshot is created enabling easy restores to a specific date or time.

The location where the backup data is stored is assumed to not be a trusted environment, such as a shared space where others could possibly access the backups. To guarantee the confidentiality and integrity of the backed up data, Restic uses cryptography to protect it.

Restic is open source and free software and licensed under the BSD 2-Clause License and actively developed on GitHub.

Getting Started with Restic and Backblaze B2

This guide is intended to get you started with Restic and begin using the basic commands to do a full backup and restore of a specific folder to Backblaze B2.

There are many more options than we cover here and opportunities to streamline and automate the process through shell scripts and other means. You can find more information on using Restic at https://restic.readthedocs.io.

Installation

Being a relatively new program, Restic is not yet widely available from Linux distribution repositories. It is becoming more popular, so likely that will change. You should check whether it is possible to install Restic using your standard distribution package management tools, such as apt, yum, and dnf.

In any case, you can download a number of the latest pre-compiled binaries from the Restic release page.

Our examples use the Ubuntu Linux distribution. On that platform we needed to download the binary from the release page and install it.

As we will be installing Restic on Ubuntu 16.04, we will select the AMD64 pre-compiled binary.

Screen_Shot_2018-08-21_at_10.32.06_AM.png

Download it to your system.

Screen_Shot_2018-08-21_at_10.37.13_AM.png

Once in our downloads folder, we can extract the binary using the Archive Manager.

The binary file can be placed anywhere in the path for the current system or user. In this example, it is placed in /home/roderick/bin. Update the binary to make it executable by the user with the chmod command, such as:

$ chmod u+x restic_0.9.2_linux_amd64

(using the exact name of the binary you downloaded and extracted).

Change the name of the executable binary to restic.

$ mv restic_0.9.2_linux_amd64 restic

Verify that Restic is available by typing “restic version” in the terminal.

Screen_Shot_2018-08-21_at_10.57.21_AM.png

Create Backblaze account with B2 enabled

If you don’t have a Backblaze account, you need to create a Backblaze account and enable B2. If you already have a Backblaze account, login, visit "My Settings" and enable "B2 Cloud Storage" on your account under "Enabled Products.” 

screen5.png

Second, create an Private bucket in your B2 account. (Or, you can do this directly from Restic. See Initialize the Restic Repository below.)

screen6.png

Note that the bucket name must be unique across all of B2, not just your account.

Third, retrieve your account id and application key. You will need to specify these in Restic to perform backups and restores. 

Performing a backup of your home directory

Restic can use environment variables to make it easier to use the command line. Use the account ID and application key you obtained from your B2 account. If you wish, you can set these as environment variables so you don't have to enter them with every command. You can export them in your .bashrc file or other place of your choosing so they’re available whenever you type a restic command. 

$ export B2_ACCOUNT_ID="<MY_ACCOUNT_ID>"
$ export B2_ACCOUNT_KEY="<MY_SECRET_ACCOUNT_KEY>"

Note:
In case you want to use Backblaze Application keys, replace <MY_ACCOUNT_ID>
and <MY_SECRET_ACCOUNT_KEY> with <applicationKeyId> and <applicationKey>
respectively. See Application Keys for more information.

You also can specify the name of the B2 bucket in an environment variable. In these examples, the bucket is called "my-restic-backup". As it is located on B2, it has a b2: prefix.

$ export RESTIC_REPOSITORY="b2:my-restic-backup"

If you do that, you don't need to enter the "-r <repo string>" parameter every time you enter a Restic command. In the examples below, we'll assume the account ID and application key are in environment variables, but we'll specify the -r repository parameter each time for clarity.

Each time you enter a Restic command, you will be asked for your repository password. You will need to enter that or store the password in a file and retrieve it with the -p parameter. Or, you can use an environment variable to store the name of the password file and it will be used automatically.

$ export RESTIC_PASSWORD_FILE="restic-pw.txt"

Initialize the Restic repository

Initialize the repository stored at Backblaze B2 with the bucket name of your choice. If the bucket does not exist yet, it will be created.

$ restic -r b2:my-restic-backup init

1.png

You will be asked to create a repository password.

To back up your home directory using the home environment variable ($HOME or ~) and the restic environment variables set as above, you would enter the following:

$ restic -r b2:my-restic-backup backup ~

2.png

The contents of a directory at a specific point in time is called a “snapshot” in Restic. The specific snapshot just created is identified by a sequence of hexadecimal characters.

To view the snapshot you now have stored on B2, enter

$ restic -r b2:my-restic-backup snapshots

3.png

If you run the backup command again, Restic will create another snapshot of your data, but this time it will be faster due to de-duplication. When you back up the same directory again (maybe with new or changed files) Restic will find the old snapshot in the repo and by default only reads those files that are new or have been modified since the last snapshot. This is decided based on the modify date of the file in the file system.

For subsequent backups (snapshots), you would enter the same command.

$ restic -r b2:my-restic-backup backup ~

4.png

 You now have two snapshots in your repository.

$ restic -r b2:my-restic-backup snapshots

5.png

Restoring your backup

To restore your backup, you specify which snapshot you wish to restore and where you want it to be put with the restore and the -t parameters.

$ restic -r b2:my-restic-backup restore  9a83a611 -t /tmp/restic-restore 

6.png

(Open image above in new window/tab to view.)

The snapshot will be restored to your system at /tmp/restic-restore.

You can save to and restore from different systems. The repository in the example above was saved from an Ubuntu Linux system and a snapshot from that repository was restored to a Debian Linux system.

On Debian:

$ restic -r b2:my-restic-backup snapshots

7.png

$ restic -r b2:my-restic-backup restore 9a83a611 -t /tmp/restic-restore

8.png

(Open image above in new window/tab to view.)

Note: The number of concurrent connections to the B2 service can be set with the

-o b2.connections=10

switch. By default, at most five parallel connections are established.

To Learn More

There's a lot you can do with Restic, including adding tags, mounting a repository locally, and scripting. To learn more, you can read the documentation at https://restic.readthedocs.io.

 

 

Have more questions? Submit a request

34 Comments

  • 0
    Avatar
    b2

    @florian: I think you have to run an unlock command:

    Restic -r <path> unlock

     

  • 0
    Avatar
    be.0

    Is there any way to have Backblaze run `restic prune` on the server periodically so the user doesn't have to incur the cost of the downloading required for this?

  • 0
    Avatar
    roderick

    Florian. The lock should be released if it's a process that has completed. If you continue to experience this or other delays, check with our support team at https://help.backblaze.com/hc/en-us/requests/new

  • 0
    Avatar
    michael.a.gainey

    I wish you would have mentioned excluding directories.  I didn't think about it.  I'm doing my first backup now and it has been on the ~/.cache directory for 53 minutes (12146 files, 868 MiB) ... and ncdu says my cache directory is 2.2GB ... so it will be working on my cache directory for more than another hour!

Article is closed for comments.
Powered by Zendesk