After having installed the B2 CLI tool , 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 allPrivateThe 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 failwith the following error:
ERROR: Missing account data: 'NoneType' object has no attribute '__getitem__' Use: b2 authorize_accountThis 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 .
b2 authorize_account [acount ID] [Application Key]Now we can create our bucket:
b2 create_bucket programster-test allPrivateAnd finally, we can sync a folder to our new bucket.
mkdir test echo "hello world" > test/test.txt b2 sync --delete test b2://programster-testAlthough 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.
If we wish to list the files in our bucket that we just synced to, execute:
b2 list_file_names programster-testYou 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-folderYou should now see the test file in your restore folder.
ConclusionWe'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