Skip to content

Instantly share code, notes, and snippets.

@XPlantefeve
Last active September 4, 2019 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XPlantefeve/c451e4ff55eaf6f0310996faff4d5461 to your computer and use it in GitHub Desktop.
Save XPlantefeve/c451e4ff55eaf6f0310996faff4d5461 to your computer and use it in GitHub Desktop.
Restore files from OVH Cloud Archive
#!/bin/bash
dirname=$(dirname $0)
basename=$(basename $0)
conffile="${basename%.*}.conf"
if [ -f "$dirname/$conffile" ]
then
conffile="$dirname/$conffile"
. "$conffile"
else
if [ -f "~/$conffile" ]
then
conffile="~/$conffile"
. "$conffile"
fi
fi
verbose=false
usage() {
echo "Usage: $basename [options...]"
echo 'Options:'
echo ' -u, --user USERNAME Name of the user'
echo ' -p, --password PASSWORD Password of the created user.'
echo ' -i, --project PROJECTID Project ID'
echo ' -b, --bucket BUCKETNAME Bucket name'
echo ' -m, --mask MASK Mask to select files : a string to match any part of the folder of file name'
echo ' -v, --verbose Make the operation more talkative'
echo ''
echo 'The project ID is displayed on top of your left menu, it''s a 32 chars hexadecimal number.'
echo 'The user has to be created in "Users & Roles". It needs the ObjectStore operator role.'
}
while [ "$1" != "" ]; do
case $1 in
-u | --user ) shift
username=$1
;;
-p | --password ) shift
password=$1
;;
-i | --project ) shift
projectid=$1
;;
-b | --bucket ) shift
bucket=$1
;;
-m | --mask ) shift
mask=$1
;;
-v | --verbose ) verbose=true
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
while [ -z "$username" ]
do
echo "Username?"
read username
done
while [ -z "$password" ]
do
echo "Password?"
read password
done
while [ -z "$projectid" ]
do
echo "project ID?"
read projectid
done
while [ -z "$bucket" ]
do
echo "bucket name?"
read bucket
done
$verbose && echo "configuration read from $conffile"
urlencode() { perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$*"; }
ApiUrl="https://auth.cloud.ovh.net/v3"
BucketUrl="https://storage.de1.cloud.ovh.net/v1/AUTH_$projectid/$bucket"
AuthInfo='{"auth": {"identity": {"methods": ["password"],"password": {"user": {"name": "'
AuthInfo+=$username
AuthInfo+='","domain": {"name": "Default"},"password": "'
AuthInfo+=$password
AuthInfo+='"}}}}}'
token=$(curl -si --header 'Content-Type: application/json' --data "$AuthInfo" $ApiUrl/auth/tokens -o /dev/null -D - | tr -d '\r' | sed -En 's/^X-Subject-Token: (.*)/\1/p')
if [ -z "$token" ]
then
echo ERROR: token has not been found.
else
IFS=$'\n'
filelist=($(curl -s --header "X-Auth-Token: $token" $BucketUrl))
$verbose && echo "Found ${#filelist[@]} total files"
globalretryafter=0
for file in "${filelist[@]}"
do
if [[ $file == *"$mask"* ]]
then
if [ ! -e "$file" ]
then
$verbose && echo "processing $file"
mkdir -p $(dirname $file)
uri=$BucketUrl/$(urlencode $file)
response=($(curl -s -D - --header "X-Auth-Token: $token" $uri -o "$file"))
httpcode="$(echo "${response[0]}" | sed -e 's/.* \([0-9]*\) .*/\1/')"
if [ $httpcode -eq 429 ]
then
retryafter=$(for h in "${response[@]}"; do if [[ $h == "Retry-After: "* ]] ; then printf ${h#Retry-After: } | tr -d '\r' ; fi ; done)
globalretryafter=$(( globalretryafter > retryafter ? globalretryafter : retryafter ))
rm "$file"
fi
fi
fi
done
if [ $globalretryafter -gt 0 ]
then
# secs="$(if [ $((globalretryafter % 60)) -ne 0 ] ; then printf " and $((globalretryafter % 60)) seconds" ; fi)"
# echo "One or more file are still being defrozen. You might want to retry in $((globalretryafter / 60)) minutes$secs."
echo "One or more file are still being defrozen. You might want to retry after $(date -d "+$globalretryafter seconds" +'%R (%B %d)')"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment