From 06a955e0425166737a73ab847730d61e48c72be7 Mon Sep 17 00:00:00 2001 From: lapatatedouce Date: Tue, 3 Sep 2024 16:07:23 +0200 Subject: [PATCH] Update code scaleway --- .../scaleway/list-glacier-objects.sh | 42 +++++++++++++++++++ .../scaleway/update_class_standard.sh | 32 +++++++++++--- 2 files changed, 68 insertions(+), 6 deletions(-) create mode 100755 backup_and_restore/scaleway/list-glacier-objects.sh diff --git a/backup_and_restore/scaleway/list-glacier-objects.sh b/backup_and_restore/scaleway/list-glacier-objects.sh new file mode 100755 index 0000000..f24f80b --- /dev/null +++ b/backup_and_restore/scaleway/list-glacier-objects.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +# This script counts the number of objects stored in the Glacier storage class within an S3 bucket. +# It optionally filters the list based on a specified directory within the bucket. + +# Example usage: +# ./count-glacier-objects.sh my-bucket my-directory +# (Counts Glacier objects in the 'my-directory' directory within the 'my-bucket' bucket) +# ./count-glacier-objects.sh my-bucket +# (Counts all Glacier objects in the 'my-bucket' bucket) + +# Check the number of arguments +if [[ $# -lt 1 || $# -gt 2 ]]; then + echo "Usage: $0 [directory_name]" + exit 1 +fi + +# Retrieve the arguments +BUCKET="$1" +DIRECTORY="$2" + +# Function to sanitize names (though not necessary for an output file in this version) +sanitize_name() { + echo "$1" | tr -dc '[:alnum:]-_.' +} + +# Construct the AWS CLI command +COMMAND="aws s3api list-objects-v2 --bucket $BUCKET --query \"Contents[?StorageClass=='GLACIER']\"" + +# Add the prefix if a directory is specified +if [[ -n "$DIRECTORY" ]]; then + COMMAND+=" --prefix $DIRECTORY" +fi + +# Finalize the command to count the objects +COMMAND+=" --output text | wc -l" + +# Execute the command and retrieve the number of Glacier files +NUM_FILES=$(eval $COMMAND) + +# Display the number of Glacier files +echo "Number of files in Glacier storage class to be restored: $NUM_FILES" diff --git a/backup_and_restore/scaleway/update_class_standard.sh b/backup_and_restore/scaleway/update_class_standard.sh index bd55135..29b55bb 100755 --- a/backup_and_restore/scaleway/update_class_standard.sh +++ b/backup_and_restore/scaleway/update_class_standard.sh @@ -1,6 +1,9 @@ #!/bin/bash -# This script initiates restoration requests for objects listed in a specified file. +# This script initiates restoration requests for Glacier objects listed in a specified file, +# but only if they haven't been restored yet, do not have an ongoing restoration request, +# and are still in the Glacier storage class. The script processes these objects in parallel +# using xargs to speed up the execution. # It requires the bucket name, file name, and the number of days for the restoration. # Example usage: @@ -24,9 +27,26 @@ if [[ ! -f "$FILE" ]]; then exit 1 fi -# Iterate through the file and initiate restoration requests -while read -r KEY; do - aws s3api restore-object --restore-request Days=$DAYS --bucket $BUCKET --key "$KEY" -done < "$FILE" +# Function to process each object +process_object() { + KEY="$1" + STORAGE_CLASS=$(aws s3api head-object --bucket "$BUCKET" --key "$KEY" --query 'StorageClass' --output text) + STATUS=$(aws s3api head-object --bucket "$BUCKET" --key "$KEY" --query 'Restore' --output text) -echo "Restoration requests initiated for objects listed in '$FILE'." + if [[ "$STORAGE_CLASS" != "GLACIER" ]]; then + echo "Skipping $KEY: Already in $STORAGE_CLASS storage class." + elif [[ "$STATUS" == *"ongoing-request=\"true\""* ]]; then + # Skip if there's an ongoing restoration + : + else + echo "Initiating restoration for: $KEY" + aws s3api restore-object --restore-request Days=$DAYS --bucket "$BUCKET" --key "$KEY" + fi +} + +export -f process_object +export BUCKET +export DAYS + +# Use xargs to run process_object in parallel for each line in the file +cat "$FILE" | xargs -P 4 -I {} bash -c 'process_object "$@"' _ {}