/ Guides

B2 Backups

Backups a great. Break a server and no problem, you got all the files. Set up a new one. Accidentally delete something? No worries, you got that too.

But sadly, humans are lazy, and therefore not good at taking backups. Which is why we automate them.

Scripts and crontab are magic. They make the world go around. And in case your server breaks, they make it possible for you to start spinning it again.

Our partner-in-crime for this sample will be Backblaze B2. They give us storage for $0.005 per GB per month and provides free uploads. (That means backing up 50 GB of a mail server would only cost you $3 a year) - The first 10 GB are also free, so if you have a small server to back up, you might not even have to pay anything.

Setting up

I'm of course assuming you actually signed up for Backblaze, you can do that here.

Start by simply making a bucket via their WebUI. You can name it anything you want. You probably don't need to share your backups. So you can leave the bucket set to private.

Bucket creation screenshot

For this script we'll need the b2 application. Installing this is quite simple. If you're on Ubuntu, simply run sudo apt install python3-pip to get pip installed on your system. Then run sudo pip3 install b2 to install the application via pip.

Now run b2 authorize_account <accountId> <applicationKey> - You'll find both the accountId and your applicationKey listed on your account's page.

The script

Writing scripts to back up with b2 is quite simple. For this sample I'll be backing up an imaginary /var/vmail folder, which is quite commonly used to store mails on a mail server.

The following script should do the job for us:


# Mail server backup script

folder=$(date +%Y-%m-%d)

tar -czf $folder.tar.gz /var/vmail
/usr/local/bin/b2 upload_file my-new-bucket $folder.tar.gz $folder.tar.gz

This is a basic script. Let's walk through what the three lines do:

  • folder=$(date +%Y-%m-%d) - This makes a folder name for us to use. In this case based off the current date. If I ran it the same day I wrote this blog post, I would get 2016-11-21
  • tar -czf $folder.tar.gz /var/vmail - This creates a new .tar.gz archive with all our mails, using the earlier generated name (So we'd end up with "2016-11-21.tar.gz")
  • /usr/local/bin/b2 upload_file... - This uploads our newly created archive. Note the bucket name in the command, which is "my-new-bucket" in the above sample. Change this to reflect your bucket name.

That's it. If you save that script and run it, you will back up your mail directory to Backblaze B2. (Note that the script does not delete the local .tar.gz file. You can add rm -rf $folder.tar.gz if you want it to do that)


A script is all fine and dandy, but it doesn't help much unless it runs automagically. Which is very easy thanks to crontab. I will assume you saved the script to /bin/backup-mail.sh

First you need to put this into the crontab of some user who has access to the mail folder. Many people like creating a separate user that runs backup scripts. For this sample we'll be lazy and add it to root's crontab however (Don't do this at home kids)

Please note that you must also authorize the b2 account for the user who will be running the script. If you don't run b2 authorize_account <accountId> <applicationKey> for the backup user as well (In this case, root) the script will fail.

Run sudo su to switch users. Then run crontab -e to edit the crontab for the current user. Add the following line to the crontab file:

13   4   *   *   *   /bin/bash /var/vmail/backup/backup_mysql.sh

The general format of crontab entries are as follows:

 ┌───────────── min (0 - 59)
 │ ┌────────────── hour (0 - 23)
 │ │ ┌─────────────── day of month (1 - 31)
 │ │ │ ┌──────────────── month (1 - 12)
 │ │ │ │ ┌───────────────── day of week (0 - 6) (Sunday to Saturday;
 │ │ │ │ │                                         7 is also Sunday)
 │ │ │ │ │
 │ │ │ │ │
 * * * * *  command to execute

The sample line I used above will run at 4:13 every day. (I recommend setting the minutes to something random, that way Backblaze doesn't get bombed by backups from everyone around the world at every full hour)

And... that's it. Your script should now be running automatically at the time you specified in crontab. Sit back and relax. Then check B2 the next day to make sure it works. You now have zero worries, your files are safely and automagically uploaded to Backblaze B2. You can repeat this with any other folders or on any other servers where you have something you wish to back up.

Bucket list