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 Linux 386 pre-compiled binary.

(Author's Note: The developers of Restic contacted me following the publication of this article and recommends choosing the AMD64 binary, saying it will be much faster.)

screen1.png

Download it to your system.

screen2.png

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

screen3.png

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

$ chmod u+x restic_0.7.3_linux_386

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

Change the name of the executable binary to restic.

$ mv restic_0.7.3_linux_386 restic

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

screen4.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

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 =""
$ export B2_ACCOUNT_KEY=""

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.)

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

8 Comments

  • 1
    Avatar
    rnicksic

    Restic Release Page link is broken. Missing ":" after https https//github.com/restic/restic/releases/tag/v0.7.3

  • 0
    Avatar
    roderick

    Thanks. Fixed.

  • 0
    Avatar
    eplant

    Your guide is helpful, but why are the snapshots I create via the command line restic backup not visible online under the "snapshots" section? I was hoping that online I could see the filenames, directory structure, modification times, etc.

    Thanks for your assistance.

  • 0
    Avatar
    roderick

    The data is encrypted on B2 and not directly available. You'll have to use restic to view the information you are looking for. 

  • 0
    Avatar
    eplant

    OK, thank you. I thought that may be the case. 

  • 0
    Avatar
    upssnowman

    Unfortunately restic doesn't work with B2 :-(

    I have gotten restic to work on Google Cloud, Azure, and various sftp servers but it does not work with B2 unfortunately

  • 0
    Avatar
    rnicksic

    @upssnowman, I recently uploaded almost 500GB of files to B2 via restic. I set env variables in the head of my backup script, e.g.

    #!/bin/bash

    export B2_ACCOUNT_ID="<account_id>"

    export B2_ACCOUNT_KEY="<account_key>"

    export RESTIC_REPOSITORY="b2:<bucket_name>"

    export RESTIC_PASSWORD="<password>"

    RESTIC=/usr/local/sbin/restic

    # Initialize the repo, comment out or remove after first run or you will re-initialize it and lose your backup

    $RESTIC init 

     

    # Backup files

    $RESTIC backup <path1>

    $RESTIC backup <path2>

    ...

     

    I hope that helps. I am very pleased with B2, especially the low cost.

  • 0
    Avatar
    roderick

    upssnowman Yes, restic works with B2. That's what this article is about. What's the problem you're seeing with restic and B2?

Please sign in to leave a comment.
Powered by Zendesk