Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Getting Started with B2 CLI

Installation

Install pip, the python package management tool.

Once you have done that, use pip to install the package with either:

sudo pip3 install --upgrade b2

...or on older systems:

sudo pip install --upgrade b2

Usage

Create Bucket Using CLI

For the sake of this tutorial, we want to start trying it out by backing up a folder to the cloud.

The first thing we want to do is create a bucket to sync to. A bucket is like a top level directory that must have a unique name across all users that use B2. Hence I tend to prefix my buckets with the name of my organization. For example, I want to create a test bucket for this tutorial, so I will create a bucket called programster-test.

b2 create_bucket programster-test allPrivate

The allPrivate makes the bucket and all of its contents private so strangers on the internet cant access my files. If you are just wanting to host images for your sites, then you will want to use allPublic. If some files need to be public and others private, don't specify anything.

This command should fail with the following error:

ERROR: Missing account data: 'NoneType' object has no attribute '__getitem__'  Use: b2 authorize_account

This is because the tool does not know our security credentials. If you haven't already created an API key-pair, go ahead and create one by clicking on the "Show Account ID and Application Key" link on the page that lists your storage buckets.. Your account ID will always remain the same, but if you create a new key, any previous keys expire.

Authorize Account / Credentials

b2 authorize_account $keyID $applicationKey

Alternatively, you can just run:

b2 authorize_account

... and then fill in the key ID and application key when prompted (interactive).

Now we can create our bucket:

b2 create_bucket programster-test allPrivate

Sync Folder

And finally, we can sync a folder to our new bucket.

mkdir test
echo "hello world" > test/test.txt

b2 sync \
  --delete \
  test \
  b2://programster-test/test

Although not necessary for the very first sync up to the cloud, the --delete option is useful for ensuring that files you deleted locally are removed from the cloud with later syncs.

Do not do something like b2 sync test b2://programster-test/. as this will result in the files syncing up to a folder called . instead of the intention of uploading a folder called test within the programster-test bucket.

Upload File

The sync parameter won't work when trying to upload just one file. In such a scenario, you would use the following command format:

b2 upload-file \
  $BUCKET_NAME \
  $LOCAL_FILEPATH \
  $BUCKET_FILEPATH

e.g.

b2 upload-file \
  stuart-kvm-images \
  disk1.qcow2 \
  my-vm-name/disk1.qcow2

List Files

If we wish to list the files in our bucket that we just synced to, execute:

b2 ls programster-test

You should get a JSON response like so:

{
  "files": [
    {
      "action": "upload", 
      "contentLength": 12, 
      "contentSha1": "22596363b3de40b06f981fb85d82312e8c0ed511", 
      "contentType": "text/plain", 
      "fileId": "4_za381aabbe098ff72545d091e_f111131423a69ffb2_d20160717_m081705_c001_v0001003_t0025", 
      "fileInfo": {
        "src_last_modified_millis": "1468743252956"
      }, 
      "fileName": "test.txt", 
      "size": 12, 
      "uploadTimestamp": 1468743425000
    }
  ], 
  "nextFileName": null
}

To restore from that backup, we can simply sync our bucket down to another local folder.

mkdir restore-folder
b2 sync b2://programster-test restore-folder

You should now see the test file in your restore folder.

Conclusion

We've only scratched the surface of what you can do with the B2 CLI tool, but there was enough here to allow you to create simple backups. If your interested in learning more about more powerful things you can do with the tool, I recommend you use the b2 command on its own to list the available commands, and then investigate each command in depth by using it without any parameters. For example, if I wish to learn about the make_url command, I would enter:

b2 make_url
Last updated: 28th October 2021
First published: 16th August 2018