Featured image of post Backup Linux Directories to Backblaze B2

Backup Linux Directories to Backblaze B2

Automate the deployment of servers in minutes using Terraform, feat. Minecraft

I recently set off to find a cheap (and hopefully free) cloud backup solution for some crucial files in my home infrastructure. I stumbled upon Backblaze’s B2 object storage solution (similar to Amazon S3 or Azure Blob). Backblaze is pretty awesome, and provides up to 10GB free storage across multiple buckets to try it out!

Β 

Requirements

  • A Linux machine (virtual, container, or bare metal)
  • Super sudoer powers
  • A Backblaze account

Β 


Β 

🐧 Prepare Linux

⬆️ Make sure you have the latest updates

1
2
sudo apt update
sudo apt upgrade

✨ Install curl (if not already)

1
sudo apt install curl

✨ Install Rclone

Rclone is a tool written in Go that allows the movement of files between local and remote directories. It can be installed by following the tutorial on their docs.

To check if Rclone is installed properly, just type rclone in your CLI. As long as you don’t get the “Unrecognized command” error, you’re good!

Β 

πŸ”₯ Prepare Backblaze

Make sure you have a Backblaze account created, and log in.

πŸͺ£ Create a Bucket

  • Make sure you are in your buckets list
  • Create a bucket
    • Give it a unique name
    • Private
    • Encryption disabled
    • Object lock disabled
    • Click create

πŸ”‘ Create App key

  • Click App keys on the left
  • Scroll down, click Add a New Application Key
    • Give it a friendly name
    • If you want to restrict the key to a specific bucket, choose it from the dropdown
    • Keep it set to read/write
    • Click create
  • Make sure to save the keyID and applicationKey for later

Β 

🎚️ Set up Rclone

In Linux, enter the command rclone config

  • Type n for a new remote

  • Give it a name (such as backblaze-bucketname)

  • This will list a bunch of supported remotes, find Backblaze B2 in the list, and enter the corresponding number into the prompt

  • For Account, use the keyID received from generating the App key

  • For Key, use the `applicationKey1 received from generating the App key

  • Set the following values as you see fit. The defaults should be good for most cases.

  • Once the list of current remotes shows, you can quit the config CLI with q

  • Remotes can be checked by using rclone listremotes

    • This will return a list of all of your remotes

Β 

πŸ“ƒ Copy a file (or directory) to your B2 bucket

The proper syntax to copy a file is

1
rclone copy [directoryOrFileName] [nameOfRemote]:[bucket]/[directoryOrFileToCopyTo]
  • cd into a directory that contains the file you want to copy up. Let’s say this file is named file.txt
  • Use rclone copy to copy up the file
    • Keep in mind, my remote’s name is backblaze-testing and my bucket’s name is quib-testing
1
rclone copy file.txt backblaze-testing:quib-testing/file.txt

  • For a directory, this is very similar. Let’s say this directory is named directory/
1
rclone copy directory backblaze-testing:quib-testing/directory

If this was successful, nothing is returned.

Β 

βœ… Check if your file is uploaded

Back in Backblaze, click Browse Files, and select the bucket you would like to browse.

If the file uploaded properly, it should show up there!

Β 

♾️ Automate it

Now, you’re probably here because you want to automate this. To do so, you’ll need to create a cron job for each backup interval you want.

To edit cron tasks:

1
sudo crontab -e
  • And select your desired text editor
  • Cron jobs are formatted by the desired time interval, followed by the command to run
  • Ex. * * * * * /home/user/script.sh will run the file /home/user/script.sh every minute
    • You can generate times to your liking easily with this website

Example cron job:

1
2
# Run Backblaze automation every day at 12:00 AM
0 12 * * * /home/user/backblaze-backup.sh
comments powered by Disqus
i do tech.
Built with Hugo
Theme Stack designed by Jimmy