Skip to content

Instantly share code, notes, and snippets.

@uglyrobot
Last active May 12, 2022 16:02
Show Gist options
  • Save uglyrobot/81cd9243f8cdef4f6d609ab6f4ae1269 to your computer and use it in GitHub Desktop.
Save uglyrobot/81cd9243f8cdef4f6d609ab6f4ae1269 to your computer and use it in GitHub Desktop.
A bash script using the B2 CLI to restore Backblaze cloud storage deleted files for a given prefix in a bucket with parallel processing for speed.
#!/bin/sh
if [ -z "$1" ]
then
echo "No bucketname supplied."
exit 1
fi
if [ -z "$2" ]
then
echo "No prefix supplied (example 360/jkloxsuc/)."
exit 1
fi
recover_file(){
FILE_ID=$(awk '{ print $1 }' <<<$1)
FILE_KEY=$(awk '{ print $6 }' <<<$1)
b2 delete-file-version "$FILE_KEY" "$FILE_ID"
}
LIST=$(b2 ls --recursive --long --versions "$1" "$2" | grep " hide ")
if [ ! -z "$LIST" ]; then
N=10 #number of parallel deletes
while IFS= read -r line; do
((i=i%N)); ((i++==0)) && wait
recover_file "$line" &
done <<< "$LIST"
echo "Restore complete!"
else
echo "No files to restore."
fi
@uglyrobot
Copy link
Author

When you have versioning turned on in a Backblaze B2 cloud storage bucket (always on for 1 day minimum) you can restore deleted objects by finding and deleting the "delete/hide markers" to restore the old version. This script makes that easy and fast.

Usage:

  1. Install the B2 CLI
  2. Login to the B2 CLI: b2 authorize-account
  3. Restore files: bash b2-restore.sh <bucket> <prefix>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment