B2 Command Line Tool

repository·master·Indexed 20 days ago

https://github.com/backblaze/b2_command_line_tool

A command-line interface for interacting with Backblaze B2 Cloud Storage. It allows users to manage accounts, buckets, files, and application keys, and perform data synchronization via the `b2 sync` command. The tool supports multiple installation methods including Homebrew, pip, Docker, and standalone binaries, and provides version-bound interfaces like `b2v4` for long-term script stability.

Tokens
3.4K
Snippets
12
Records
16
Agent score
70%

What's inside b2

  1. How ApiVer CLI versions work (b2 vs b2v4)

    master

    The B2 CLI uses the ApiVer methodology to allow the tool to evolve without breaking existing automation.

    • b2 command: This is the latest stable interface. Use this in your terminal for the best user experience, latest features, and best performance.
    • b2v4 command: This is a version-bound interface. It provides the same API version 3 interface regardless of the b2 tool's semantic version. Use this in long-term support scripts to ensure your automation doesn't break when new major versions of the CLI are released.
    • _b2v5 command: An experimental, unstable interface (indicated by the _ prefix).

    Even if the main b2 command moves to a much higher API version, b2v4 will continue to provide the same commands and parameters with security and bug fixes.

  2. Prepare the B2 CLI by authorizing your account

    master

    To use the B2 CLI, you must first authorize your account using your application key ID and application key. You can obtain these credentials from the Backblaze B2 website.

    Security Note: Providing credentials directly as command-line arguments may expose them to other local users via the process list. It is recommended to use environment variables instead.

    Method 1: Command-line arguments

    Run b2 account authorize followed by your key ID and application key.

    Set the B2_APPLICATION_KEY_ID and B2_APPLICATION_KEY environment variables. The CLI will automatically pick these up, allowing you to run the authorize command without arguments.

    # Method 1: Direct authorization
    $ b2 account authorize 4ab123456789 001aabbccddeeff123456789012345678901234567
    
    # Method 2: Using environment variables (Safer)
    $ export B2_APPLICATION_KEY_ID="your_key_id"
    $ export B2_APPLICATION_KEY="your_application_key"
    $ b2 account authorize
  3. Manually set up B2 bucket replication

    master

    If you cannot use the automatic setup, you must manually configure the source key, the source bucket replication rules, the destination key, and the destination bucket mapping. Follow these steps in order:

    1. Create the Source Key

    Create a key for the source bucket with the following permissions: readFiles, readFileLegalHolds, and readFileRetentions.

    2. Configure Source Replication Rules

    Update the source bucket using b2 bucket update --replication with a JSON object defining asReplicationSource. This includes replicationRules (specifying destinationBucketId, fileNamePrefix, includeExistingFiles, isEnabled, priority, and replicationRuleName) and the sourceApplicationKeyId.

    3. Create the Destination Key

    Create a key for the destination bucket using a specific profile (e.g., --profile myprofile2) with the following permissions: writeFiles, writeFileLegalHolds, writeFileRetentions, and deleteFiles.

    4. Configure Destination Replication Mapping

    Update the destination bucket using b2 bucket update --replication with a JSON object defining asReplicationDestination. This must include a sourceToDestinationKeyMapping that maps the sourceApplicationKeyId to the new destination key ID.

    # 1. Create source key
    $ b2 key create my-bucket-rplsrc readFiles,readFileLegalHolds,readFileRetentions
    
    # 2. Setup source replication
    $ b2 bucket update --replication '{ "asReplicationSource": { "replicationRules": [ { "destinationBucketId": "85644d98debc657d880b0e1e", "fileNamePrefix": "files-to-share/", "includeExistingFiles": false, "isEnabled": true, "priority": 128, "replicationRuleName": "my-replication-rule-name" } ], "sourceApplicationKeyId": "0014ab1234567890000000123" } }' my-bucket
    
    # 3. Create destination key
    $ b2 key create --profile myprofile2 my-bucket-rpldst writeFiles,writeFileLegalHolds,writeFileRetentions,deleteFiles
    
    # 4. Setup destination replication
    $ b2 bucket update --profile myprofile2 --replication '{ "asReplicationDestination": { "sourceToDestinationKeyMapping": { "0014ab1234567890000000123": "0024ab2345678900000000234" } } }' my-bucket
  4. Use the B2 CLI via Docker

    master

    When using the official Docker image backblazeit/b2:latest, it is recommended to use version-bound commands like b2v4 for long-term stability in scripts.

    Authorization

    You can authorize by passing credentials as environment variables for each command, or by persisting credentials in a volume.

    File Operations

    • Streaming uploads: Use a pipe to pass data to the container.
    • Mounted uploads: Mount local directories to the container to use the file upload command.
    # Authorize once and persist credentials in a volume named 'b2'
    docker run --rm -it -v b2:/root backblazeit/b2:latest b2v4 account authorize
    
    # Use persisted credentials to list buckets
    docker run --rm -v b2:/root backblazeit/b2:latest b2v4 bucket list
    
    # Upload via pipe (streaming)
    cat source_file.txt | docker run -i --rm -v b2:/root backblazeit/b2:latest b2v4 upload-unbound-stream bucket_name - target_file_name
    
    # Upload via mounted local directory
    docker run --rm -v b2:/root -v /home/user/path/to/data:/data backblazeit/b2:latest b2v4 file upload bucket_name /data/source_file.txt target_file_name
  5. Enable autocomplete for shells other than Bash

    master

    The B2 CLI uses argcomplete for its completion logic. If you are using a shell other than Bash (such as Zsh or Fish), you can find instructions for activating global completion via the argcomplete project documentation.

    https://pypi.org/project/argcomplete/#activating-global-completion
  6. Install the B2 CLI

    master

    The B2 Command Line Tool can be installed via several methods depending on your operating system and environment:

    • macOS (Homebrew): The recommended method for Mac users.
    • Linux and Windows (Binaries): Download stand-alone binaries from the GitHub Releases page.
    • Python (pip): Install into a Python environment (virtualenv recommended). You can install with extra dependencies for improved debugging and performance using b2[full], or a minimal version with b2.
    • Docker: Use the official backblazeit/b2 image for a platform-independent solution.
    • Source: Install directly from the GitHub repository using pip.
    # Homebrew
    brew install b2-tools
    
    # Python (with extra dependencies)
    pip install b2[full]
    
    # Python (minimal)
    pip install b2
    
    # From source
    pip install git+https://github.com/Backblaze/B2_Command_Line_Tool.git
  7. Set up replication automatically

    master

    If you have access to accounts hosting both the source and destination buckets (even if they are in the same account), you can use the b2 replication setup command to automate the process. This command will automatically provision or reuse a source key (with no prefix and full reading capabilities) and a destination key (with no prefix and full writing capabilities).

    To use this, run:

    $ b2 replication setup --destination-profile myprofile2 my-bucket my-bucket2

    You can optionally specify a source rule priority and a source rule name using additional flags.

  8. Enable verbose or debug logging

    master

    For troubleshooting, you can enable different levels of logging:

    • --verbose: Enables verbose logs to stdout.
    • --debug-logs: A hidden flag that enables logging to a b2_cli.log file (with rotation at midnight) in the current working directory. Warning: Avoid running this in a directory you are actively syncing to prevent the log file from being uploaded to B2.
    • --log-config <filename.ini>: A hidden option to use a custom log configuration file.
  9. Reference: Manual replication JSON schemas

    master

    When performing manual replication setup via b2 bucket update --replication, use the following JSON structures:

    Source Configuration (asReplicationSource) Used on the source bucket. Requires sourceApplicationKeyId and an array of replicationRules.

    Destination Configuration (asReplicationDestination) Used on the destination bucket. Requires sourceToDestinationKeyMapping, which is a dictionary mapping the source key ID to the destination key ID.

    // Source Configuration
    {
        "asReplicationSource": {
            "replicationRules": [
                {
                    "destinationBucketId": "string",
                    "fileNamePrefix": "string",
                    "includeExistingFiles": boolean,
                    "isEnabled": boolean,
                    "priority": integer,
                    "replicationRuleName": "string"
                }
            ],
            "sourceApplicationKeyId": "string"
        }
    }
    
    // Destination Configuration
    {
        "asReplicationDestination": {
            "sourceToDestinationKeyMapping": {
                "SOURCE_KEY_ID": "DESTINATION_KEY_ID"
            }
        }
    }