Skip to content

Instantly share code, notes, and snippets.

@mvanlonden
Created November 5, 2023 18:55
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 mvanlonden/b612d121be63c543ce2c2d8b835a4c39 to your computer and use it in GitHub Desktop.
Save mvanlonden/b612d121be63c543ce2c2d8b835a4c39 to your computer and use it in GitHub Desktop.
Upload multiple directories to AWS Glacier
#!/bin/bash
# Function to upload directory to Glacier
upload_directory_to_glacier() {
local directory_path=$1
local vault_name=$2
local account_id=$3
local compressed_archive_path="/tmp/$(basename "$directory_path").tar.gz"
local archive_description="Backup of $(basename "$directory_path") on $(date +%Y-%m-%d)"
# Compress the directory
echo "Compressing the directory: $directory_path"
tar -zcvf "$compressed_archive_path" -C "$(dirname "$directory_path")" "$(basename "$directory_path")" || return 1
# Check if the vault exists, if not, create it
if ! aws glacier describe-vault --account-id $account_id --vault-name $vault_name 2>/dev/null; then
echo "Vault $vault_name does not exist, creating it..."
aws glacier create-vault --account-id $account_id --vault-name $vault_name || return 1
fi
# Upload the archive to Glacier
echo "Uploading the archive to Glacier: $directory_path"
local upload_output=$(aws glacier upload-archive --vault-name $vault_name --account-id $account_id --archive-description "$archive_description" --body "$compressed_archive_path") || return 1
# Provide feedback and next steps
echo "Upload complete. Details: $upload_output"
# Extract the Archive ID from the upload output (optional)
local archive_id=$(echo $upload_output | jq -r '.archiveId')
echo "Archive ID for $directory_path: $archive_id"
# Cleanup: Remove the compressed archive if you no longer need it
# Uncomment the following line if you want to remove the archive after uploading.
# rm "$compressed_archive_path"
}
# Main script body
if [ "$#" -lt 2 ]; then
echo "Usage: $0 VAULT_NAME DIRECTORY [DIRECTORY]..."
exit 1
fi
VAULT_NAME=$1
ACCOUNT_ID="-" # use '-' for the current account
shift # Shift all arguments to the left (original $1 gets lost)
# Iterate over all directory arguments
for dir_path in "$@"; do
if [ ! -d "$dir_path" ]; then
echo "Warning: Skipping $dir_path because it is not a directory."
continue
fi
upload_directory_to_glacier "$dir_path" "$VAULT_NAME" "$ACCOUNT_ID"
done
@mvanlonden
Copy link
Author

mvanlonden commented Nov 5, 2023

Usage

chmod +x glacier_multi_upload.sh
./glacier_multi_upload.sh YourVaultName /path/to/your/first/directory /path/to/your/second/directory ...

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