How to Set Up Off-Site Backups to Backblaze B2 with Restic

A backup that lives on the same server as your data is not a backup. When the disk dies, the server gets hacked, or the provider loses the machine, both copies go at once. Backblaze B2 Restic backups fix that by sending encrypted snapshots to object storage off site, on a schedule, for a small monthly cost. This guide sets up Restic with a B2 bucket, runs the first backup, and adds a cron job that keeps it running.

Restic is a backup tool that encrypts everything before it leaves your server, stores snapshots with deduplication so repeat backups are fast, and can restore a single file or a whole directory. Backblaze B2 is object storage that charges for what you store and download, with no charge for uploads. Together they give you off-site backups without a second server.

What You Need First

You need a Backblaze account, a B2 bucket, and Restic installed on your server. Create a private bucket, then create an application key with access to that bucket only. A key scoped to one bucket limits the damage if the key ever leaks. Save the key ID, the application key, and the bucket name somewhere safe, such as a password manager.

Install Restic from your package manager or the official binary. On Ubuntu, sudo apt install restic works, though the version in the repository can lag behind the latest release. The official binary from the Restic site is a single file and easy to update, so use it if you want the newest version. Pick the directories you want to back up before you start: website files, databases dumps, and config files such as Nginx server blocks.

Create the B2 Bucket and Key

Log in to Backblaze, open B2 Cloud Storage, and create a bucket. Set it to private, since Restic encrypts the data anyway and a private bucket adds a second layer. Turn on the default lifecycle rule only if you want old file versions pruned; Restic manages its own snapshots, so leave lifecycle off unless you know why you need it.

Create an application key, give it a name such as restic-server-1, and limit it to the bucket you just made. Copy the key ID and the key itself right away: Backblaze shows the key only once. If you lose it, you will need to create a new key and update the server.

Set Up Restic for Backblaze B2

Restic needs a handful of environment values: the repository address, the B2 account ID and key, and a strong password for the repository itself. This backblaze b2 backup setup keeps the keys in a file only root can read. The repository password encrypts the backup, so pick a long one and store it in a password manager. If you lose this password, the backups cannot be read by anyone, including you.

export B2_ACCOUNT_ID="your-key-id"
export B2_ACCOUNT_KEY="your-application-key"
export RESTIC_PASSWORD="a-long-random-password"
export RESTIC_REPOSITORY="b2:my-backup-bucket:/server-1"

restic init

Keep these values in a file only root can read, such as /root/.restic-env with permissions 600, and load it with set -a; source /root/.restic-env; set +a in your backup script. Never put the key or password in a world-readable file or in a shell history you share. Readers who back up more than one machine can follow our guide to automating Linux server backups with restic and cron for a repeatable script pattern.

Run the First Backup

Back up the directories that matter, and exclude the ones that do not. Cache directories, log files, and package caches waste space and time, so leave them out with --exclude flags.

restic backup /var/www /etc/nginx /opt 
  --exclude /var/www/*/cache 
  --exclude '*.log' 
  --tag daily

The first run takes the longest because it uploads everything. Later runs only upload changed blocks, so a daily backup of a small site usually finishes in minutes. Watch the output: Restic reports how many files it scanned, how many changed, and how much it uploaded. If the first run stalls, check that the bucket name is right and that the application key has access to it.

Databases need a dump before Restic can back them up well. Restic can back up a live database directory, but restoring from a raw copy of a running database is risky. Dump MySQL or PostgreSQL to a file first, then back up the dump. Our comparison of the best Linux server backup tools explains how Restic, Borg, and Duplicacy differ if you want to compare before you commit.

Automate Backups with Cron

A backup you run by hand is a backup you will forget. Put the backup in a script and run it from cron every night. The script should load the environment file, run the backup, prune old snapshots, and write a log line you can check.

#!/bin/bash
set -e
set -a; source /root/.restic-env; set +a
restic backup /var/www /etc/nginx /opt 
  --exclude /var/www/*/cache 
  --exclude '*.log' 
  --tag daily
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune

Save it as /usr/local/bin/backup-b2.sh, make it executable, and add a cron entry to run it at 2 a.m.: 0 2 * * * /usr/local/bin/backup-b2.sh >> /var/log/restic-backup.log 2>&1. The forget and prune step keeps seven daily, four weekly, and six monthly snapshots, so storage stays bounded. Adjust those numbers to fit how far back you want to restore.

Test the Restore Before You Need It

A backup you never restored is a guess, not a safety net. Test it once a month. List snapshots with restic snapshots, then restore one file to a temporary directory and compare it with the original.

restic snapshots
mkdir /tmp/restore-test
restic restore latest --target /tmp/restore-test --include /var/www/example.com/index.html

Open the restored file and confirm it matches. Then restore a database dump and load it into a test database to confirm the dump is valid. Write down the restore steps while you test them: the person restoring at 2 a.m. during an outage will thank you for a short, tested checklist. If you want the wider context on storage costs, our comparison of object storage pricing across S3, Backblaze B2, and Cloudflare R2 helps you estimate the monthly bill.

Backblaze B2 Restic Backups: Watch the Cost

B2 charges for storage and for downloads, while uploads are free. A small site with a few gigabytes of snapshots costs very little per month, and the prune policy above keeps the total from growing without limit. Prices change over time, so treat any figure you read in a guide as approximate and check official pricing on the Backblaze site before you estimate a budget.

Watch two numbers each month: total stored bytes and download bytes. Restores are the main download cost, and a test restore once a month adds only a small amount. If storage grows faster than expected, check whether a large cache or media directory slipped into the backup set, and add an exclude for it.

Common Problems and Fixes

Most Restic and B2 problems come from credentials or the bucket name. A “repository does not exist” error usually means the bucket name or the key scope is wrong. A failed init with an auth error means the key ID or key has a typo, or the key was deleted. A backup that fails halfway usually means the network dropped; run it again, since Restic resumes from where it stopped.

If cron runs the script but no snapshot appears, read the log file. The most common cause is the environment file not loading because cron runs with a minimal environment. Sourcing the file inside the script, as shown above, avoids that. If prune takes a long time, run it weekly instead of daily; it locks the repository while it works, so keep it away from backup hours.

Keep the Backup Healthy

Run restic check once a month to verify the repository has no damaged data. A restic off-site backup is only as good as its last tested restore, so keep the monthly test on your calendar. Rotate the application key once a year, and update the environment file the same day. Keep a copy of the repository password and the bucket name somewhere outside the server, such as a password manager, so you can restore even if the server is gone entirely. For encrypted cloud backups, the repository password is the one secret you cannot lose, so guard it like the data itself.

Off-site backups are the last line of defence for a small server. With the script in cron and a tested restore, a dead disk turns into a bad afternoon instead of a lost business. For a team setup with encrypted backups across several machines, our guide to automatic encrypted backups for a small team builds on the same ideas.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.