Skip to content
masdzub.com
~/articles/ubuntu — one from the notebook

How to Set Up Automated Backups with Restic and Backblaze B2 on Ubuntu

A practical guide to configuring encrypted, incremental backups using Restic and Backblaze B2 on Ubuntu with systemd timers.

masdzub@website:~$ cat ~/articles/how-to-set-up-automated-backups-with-restic-and-backblaze-b2-on-ubuntu.aspx.md
# 1 file · 2 min read · topic: ubuntu · status: published

Introduction

Backing up data to Backblaze B2 using Restic provides an encrypted, deduplicated, and incremental storage solution. This setup avoids vendor lock-in and protects against ransomware through immutable snapshots.

Prerequisites

  1. A Backblaze B2 bucket.
  2. Application Key ID and Application Key with readWrite permissions.
  3. Restic installed (sudo apt install restic).

1. Initialize the Repository

Export your B2 credentials to the environment to avoid manual entry. Do not store these in plain text in your crontab.

Terminal window
export B2_ACCOUNT_ID="your-id"
export B2_ACCOUNT_KEY="your-key"
export RESTIC_PASSWORD="a-strong-random-password"
export B2_BUCKET_NAME="your-bucket-name"
restic -r b2:${B2_BUCKET_NAME}:/ init

2. Create the Backup Script

Create a script at /usr/local/bin/backup-to-b2.sh. This script handles the environment variables and the backup execution.

#!/bin/bash
export B2_ACCOUNT_ID="your-id"
export B2_ACCOUNT_KEY="your-key"
export RESTIC_PASSWORD="your-password"
export RESTIC_REPOSITORY="b2:your-bucket-name:/"
# Backup files
restic backup /home/user/data --tag system-backup
# Prune old snapshots (keep last 7 days, 4 weeks, 6 months)
restic forget --tag system-backup --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune

Set permissions: sudo chmod +x /usr/local/bin/backup-to-b2.sh.

3. Automate with systemd

Instead of cron, use a systemd timer for better logging and failure tracking. Create /etc/systemd/system/restic-backup.service:

[Unit]
Description=Restic Backup Service
[Service]
ExecStart=/usr/local/bin/backup-to-b2.sh

Create the timer at /etc/systemd/system/restic-backup.timer:

[Unit]
Description=Daily Restic Backup
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target

Enable the timer:

Terminal window
sudo systemctl daemon-reload
sudo systemctl enable --now restic-backup.timer

Troubleshooting

  • Error: repository is already locked: If a previous backup was interrupted, run restic unlock to remove stale lock files.
  • Slow performance: If backing up millions of small files, Restic’s metadata scanning can be slow. Use the --files-from flag to target specific directories or increase the GOMAXPROCS environment variable if CPU is not the bottleneck.
  • Verification: Run restic check manually to ensure the integrity of the data in B2. This checks for missing or corrupted blobs.
found this useful? share it →
Written by

Dzubayyan Ahmad

System Administrator, SRE & Technical Support Engineer specialising in web hosting infrastructure — Linux servers, automation, and monitoring. Writing these notes since 2015 so future-me can find the fix again.