Created
October 14, 2023 22:11
-
-
Save MeowcaTheoRange/c947c0bfc19db0adbe05572e8b75f265 to your computer and use it in GitHub Desktop.
Backup File to Backblaze
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Requires: curl, jq | |
# apt install curl jq | |
# dnf install curl jq | |
# yum install curl jq | |
# pacman -S curl jq | |
AUTH_HEADER="Basic [AUTH KEY]" # https://www.backblaze.com/apidocs/b2-authorize-account | |
FILE_TO_UPLOAD="[FILE PATH]" # /home/mtr/Downloads/test.sql | |
FILENAME_TO_UPLOAD="[FILE WITHOUT PATH]" # test.sql | |
# If the file is being generated, put code here! | |
# mysqldump database > /home/mtr/Downloads/test.sql | |
# First, hash file for verification. | |
SHA_OF_FILE=$(openssl dgst -sha1 $FILE_TO_UPLOAD | awk '{print $2;}') | |
# Authorize with Backblaze | |
RESPONSE=$(curl "https://api.backblazeb2.com/b2api/v2/b2_authorize_account" \ | |
-H "Authorization: ${AUTH_HEADER}") | |
# Clip JSON response | |
AUTH_TOKEN=$(echo $RESPONSE | jq .authorizationToken -r) | |
API_URL=$(echo $RESPONSE | jq .apiUrl -r) | |
ALLOWED_BUCKET=$(echo $RESPONSE | jq .allowed.bucketId -r) | |
echo $API_URL | |
# Get upload URL | |
UPLOAD_URL=$(curl "${API_URL}/b2api/v2/b2_get_upload_url?bucketId=${ALLOWED_BUCKET}" \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: ${AUTH_TOKEN}") | |
# Clip JSON response | |
ALLOWED_UPLOAD_URL=$(echo $UPLOAD_URL | jq .uploadUrl -r) | |
UPLOAD_AUTH_TOKEN=$(echo $UPLOAD_URL | jq .authorizationToken -r) | |
echo $ALLOWED_UPLOAD_URL | |
# Finally, upload file | |
curl "${ALLOWED_UPLOAD_URL}" \ | |
-H "Authorization: ${UPLOAD_AUTH_TOKEN}" \ | |
-H "X-Bz-File-Name: ${FILENAME_TO_UPLOAD}" \ | |
-H "Content-Type: b2/x-auto" \ | |
-H "X-Bz-Content-Sha1: ${SHA_OF_FILE}" \ | |
-H "X-Bz-Info-Author: unknown" \ | |
--data-binary "@${FILE_TO_UPLOAD}" | |
# You can repeat this snippet for how many files you would like to upload. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment