Migrate data from Amazon to Backblaze B2 Cloud Storage. Read more.Purple Right Arrow Icon

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:

  • sudo mkdir -p /usr/local/bin/
  • sudo chgrp admin /usr/local/bin/
  • sudo chmod 775 /usr/local/bin/
  • sudo mkdir /Library/Logs/b2sync/
  • sudo chgrp admin /Library/Logs/b2sync/
  • sudo chmod 775 /Library/Logs/b2sync/
  • copy the b2sync in this zip b2sync_for_mac to here: /usr/local/bin/b2sync
  • sudo chgrp admin /usr/local/bin/b2sync

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

  • sudo mkdir -p /usr/local/bin/
  • sudo chgrp staff /usr/local/bin/
  • sudo chmod 775 /usr/local/bin/
  • sudo mkdir /var/log/b2sync/
  • sudo chgrp staff /var/log/b2sync/
  • sudo chmod 775 /var/log/b2sync/
  • copy the b2sync in this zip b2sync_for_linux to here: /usr/local/bin/b2sync
  • sudo chgrp staff /usr/local/bin/b2sync
  • LINUX NOTE 1: this binary will only work on 64 bit Linux distributions.
  • LINUX NOTE 2: this one binary has been compiled on Debian and links statically with as much as it can, and has been tested and found to run on Debian, Ubuntu, and RedHat and might work on others also. Please contact us with your experiences (if it runs or not on your particular 64 bit distribution) so we can update this page.

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

  • create folder C:\Program Files\b2sync
  • copy the b2sync in this zip b2sync_for_win64 to here: C:\Program Files\b2sync\b2sync.exe
  • create folder C:\ProgramData\b2sync for log files
  • add folder C:\Program Files\b2sync to your PATH environment variable. More Info
  • WINDOWS NOTE 1: Some customers report having to install Visual C++ Redistributable for Visual Studio 2015 to get it to run. This was not intended and we will try to fix this, but in the meantime that is a workaround.

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!

  • On Macintosh or Linux: In a Terminal or Shell window, go to the top level folder that contains the file "README_b2sync_by_Backblaze.txt" and type "make RELEASE=1"
  • On Windows: Launch Microsoft Visual Studio Community 2015 (VC14) and open the "WinVisStudio.sln". Under the "Build" menu, select the "Configuration Manager..." and make sure the Active solution configuration is "Release" and the Active solution platform is "x64". Then build it.

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

  • On Macintosh: "build/Release/b2sync"
  • On Linux: "release/b2sync"
  • On Windows: "x64\Release\b2sync.exe"