Skip to content

Instantly share code, notes, and snippets.

@cablespaghetti
Last active August 10, 2025 14:12
Show Gist options
  • Save cablespaghetti/01862b9d8252223719cbe2586145f686 to your computer and use it in GitHub Desktop.
Save cablespaghetti/01862b9d8252223719cbe2586145f686 to your computer and use it in GitHub Desktop.
Backup Snac to Backblaze with just Curl and Jq
#!/bin/sh
cd /var/lib/snac
filename=snac-$(date -u +%Y-%m-%dT%H:%M:%SZ).tar.gz
tar czf backups/$filename data
# Upload to Backblaze B2
BUCKET_ID=... # From the Backblaze Console
ACCOUNT_ID=... # Your API Key ID
APPLICATION_KEY=... # API Key Secret
authorize_account_response=$(curl -s https://api.backblazeb2.com/b2api/v1/b2_authorize_account -u "$ACCOUNT_ID:$APPLICATION_KEY")
ACCOUNT_AUTH_TOKEN=$(echo $authorize_account_response | jq -r .authorizationToken)
API_URL=$(echo $authorize_account_response | jq -r .apiUrl)
upload_url_response=$(curl -s \
-H "Authorization: $ACCOUNT_AUTH_TOKEN" \
-d "{\"bucketId\": \"$BUCKET_ID\"}" \
"$API_URL/b2api/v3/b2_get_upload_url")
FILE_TO_UPLOAD="backups/$filename"
MIME_TYPE=b2/x-auto
SHA1_OF_FILE=$(openssl dgst -sha1 $FILE_TO_UPLOAD | awk '{print $2;}') # SHA1 of the file
UPLOAD_URL=$(echo $upload_url_response | jq -r .uploadUrl)
UPLOAD_AUTHORIZATION_TOKEN=$(echo $upload_url_response | jq -r .authorizationToken)
curl \
-H "Authorization: $UPLOAD_AUTHORIZATION_TOKEN" \
-H "X-Bz-File-Name: $filename" \
-H "Content-Type: $MIME_TYPE" \
-H "X-Bz-Content-Sha1: $SHA1_OF_FILE" \
-H "X-Bz-Info-Author: unknown" \
--data-binary "@$FILE_TO_UPLOAD" \
$UPLOAD_URL
# Delete older backups from disk
find ./backups/ -name 'snac-*.tar.xz' -maxdepth 1 -mtime +14 -delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment