B2 C++ Example

PLEASE READ: Backblaze highly recommends you use "b2" command line tool with the "sync" command line option. Click here to get the official b2 command line tool.

This page is NOT THAT TOOL!!

This page is about a C++ language example with the name "b2sync" (all one word) which is NOT the same as the official recommended Backblaze "b2" command line tool with the "sync" option.

WARNING: the C++ example below called b2sync is in ALPHA (it is not even Beta quality). b2sync shows your B2 application key in the command line. This means you should only run it on a secure computer with no untrusted users logged in.

B2 C++ Example Source Code kind of like rsync but for B2

b2sync is a multi-threaded command line tool that works on Linux, Windows, and Macintosh. It uploads files to B2 in a fashion inspired by rsync. What that means is that if the lastModified date on a file on your local system has not changed, it won't upload a new copy. It is idempotent, in that you can run it multiple times or restart it in the middle and the results will turn out the same.

b2sync is only available for 64 bit systems. It runs on Windows Vista 64 bit or later, and all Macintosh computers running 10.7 or later, and various Linux distributions like Debian 6 Linux or later, Ubuntu 14 and later, and RedHat 6.7 or later.

Instructions to hand install on Macintosh - open Terminal (command line) and:

Instructions to hand install on Linux - open a shell (command line) and:

Instructions to hand install on 64 bit Windows Vista or later:

Running b2sync

Try b2sync -h and also b2sync -longhelp to see (example below is on Windows!):

Tutorial on b2sync

 b2sync <-FLAGS>    

    where <-FLAGS> are listed below (combine like "-rvn"):
    -h this top message
    -longhelp this tutorial and examples
    -z no options (cannot be combined with any other SYNCOPTION)
    -r recursive
    -v verbose (produce a lot of output telling you what is going on)
    -n dry run (will not do anything, just tell you what will occur)
    -tN use "N" threads (example: -rvt73x will use 73 threads)
    -m make the dstBucket if it does not already exist
    -p make the dstBucket allPublic if we make it (always use with -m)
    -f forcefully send every file even if lastModified time has not changed
    -s suppress putting the lastModified in the filename (this is very slow)
    -x eXciting mode - do not keep previous versions, delete missing files

Example A: b2sync -mrspv 123456 78912 C:\dogs\ animals:pics/doggies

  This makes a copy all of the contents of the folder C:\dogs\ found on the
  computer running the command.  The copy will appear in B2 in a bucket
  named "animals" inside the folder pics/doggies.  Because the -r flag was
  specified, if the source folder contains subfolders those are recursively
  copied also.  Nothing will be deleted from the animals bucket. However, if
  animals:pics/doggies contained a file not present in /dogs/ before this
  command was run, that file will be hidden in B2 by the time this command
  completes.  Finally, notice the trailing "\" on C:\dogs\.  That is
  very important.  If you leave the trailing slash off, the destination paths
  on B2 would look like animals:/pics/doggies/dogs/...  It would put the dogs
  folder INSIDE the B2 folder animals:/pics/doggies/ instead of copying the
  contents.   Since the flag -p was set, if the animals bucket is created it
  will be created allPublic (files served by HTTPS without authorization).

Source Code

b2sync is in alpha (it is not even beta quality) but we are releasing the source code as a C++ code example to help developers understand how to communicate with B2. This is only an example and not everything will work. Download b2sync C++ src code here.

Compiling the Code

b2sync is written in C++. You will need these tools:

b2sync is ready to compile when you unzip the source code, there should be no configuration required. If you have to change something, please contact Backblaze support and let us know what you had to change!

When successfully compiled, the b2sync binary appears in different locations depending on platform as follows: