Last active
May 12, 2022 16:02
-
-
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.
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/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
b2 authorize-account
bash b2-restore.sh <bucket> <prefix>