Skip to content

Instantly share code, notes, and snippets.

@soffes
Created October 27, 2023 01:15
Show Gist options
  • Save soffes/e447e929c5ebf95198af6ee73e3a7a51 to your computer and use it in GitHub Desktop.
Save soffes/e447e929c5ebf95198af6ee73e3a7a51 to your computer and use it in GitHub Desktop.
Some messy scripts for deleting AWS Glacier vaults
require 'json'
ACCOUNT_ID = ''
data = JSON.load(File.read('jobs.json'))
data.each do |job|
unless job[['completed']]
result = JSON.load(`aws glacier list-jobs --account-id="#{ACCOUNT_ID}" --region="#{job['region']}" --vault-name="#{job['vault']}"`)['JobList'].detect { |i| i['JobId'] == job['job'] }
job['completed'] = result['Completed']
end
end
puts JSON.pretty_generate(data)
require 'json'
ACCOUNT_ID = ''
JOB_ID = ARGV[0]
unless JOB_ID
puts 'Missing JOB_ID.'
puts 'Usage:'
puts ' $ ruby delete-archives.rb JOB_ID'
exit 1
end
jobs = JSON.load(File.read('jobs.json'))
job = jobs.detect { |j| j['job'] == JOB_ID }
puts "Vault: #{job['vault']}"
puts "Region: #{job['region']}"
puts "Job: #{job['job']}"
filename = "job-#{job['job']}.json"
unless File.exist?(filename)
system(%(aws glacier get-job-output --account-id="#{ACCOUNT_ID}" --region="#{job['region']}" --vault-name="#{job['vault']}" --job-id="#{job['job']}" #{filename}), exception: true)
end
archives = JSON.load(File.read(filename))['ArchiveList']
total = archives.count
puts "Archives: #{total}"
archives.each_with_index do |archive, index|
id = archive['ArchiveId']
puts "Deleting archive #{index + 1}/#{total} #{id}"
system(%(aws glacier delete-archive --account-id="#{ACCOUNT_ID}" --region="#{job['region']}" --vault-name="#{job['vault']}" --archive-id="#{id}"), exception: true)
end
puts
puts "Finished deleting all archives in #{job['vault']}"
puts
puts "To delete vault:"
puts %( $ aws glacier delete-vault --account-id="#{ACCOUNT_ID}" --region="#{job['region']}" --vault-name="#{job['vault']}")
puts
require 'json'
ACCOUNT_ID = ''
REGIONS = %w[
us-east-1
us-west-1
]
vaults = {}
REGIONS.each do |region|
vaults[region] = JSON.load(`aws glacier list-vaults --account-id="#{ACCOUNT_ID}" --region="#{region}"`)['VaultList']
end
puts JSON.pretty_generate(vaults)
require 'json'
ACCOUNT_ID = ''
manifest = JSON.load(File.read('vaults.json'))
jobs = []
manifest.each do |region, vaults|
vaults.each do |vault|
name = vault['VaultName']
job = JSON.load(`aws glacier initiate-job --account-id="#{ACCOUNT_ID}" --region="#{region}" --vault-name="#{name}" --job-parameters '{"Type": "inventory-retrieval"}'`)
jobs << {
region: region,
vault: name,
job: job['jobId']
}
end
end
puts JSON.pretty_generate(jobs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment