Created
September 11, 2013 16:29
-
-
Save david-vo/6526199 to your computer and use it in GitHub Desktop.
Script to set/change the lifecycle of an Amazon S3 bucket. Todo: add options using opt-parse. I am actually not sure if this works. There are no errors but I can't figure out a way to check the lifecycle of an altered bucket.
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
#!/usr/bin/env ruby | |
# Script to backup the Discourse postgres db and upload it to Amazon S3 | |
require 'rubygems' | |
require 'yaml' | |
require 'fog' | |
require 'time' | |
require 'date' | |
#Add date/time enhancements | |
#Inspired by the activesupport gem: http://rubydoc.info/gems/activesupport/Time | |
class Fixnum | |
def seconds | |
self | |
end | |
alias_method :second, :seconds | |
def minutes | |
seconds * 60 | |
end | |
alias_method :minute, :minutes | |
def hours | |
minutes * 60 | |
end | |
alias_method :hour, :hours | |
def days | |
hours * 24 | |
end | |
alias_method :day, :days | |
def weeks | |
days * 7 | |
end | |
alias_method :week, :weeks | |
def months | |
days * 30 | |
end | |
alias_method :month, :months | |
def years | |
days * 365 | |
end | |
alias_method :year, :years | |
def ago | |
Time.now - self | |
end | |
def from_now | |
Time.now + self | |
end | |
end | |
bucket = "qa-discourse-backup" | |
# use yaml to open the .fog file and extract the credentials we need | |
creds = YAML::load(File.read('.fog')) | |
aws_access_key_id_test = creds[:default][:aws_access_key_id_test] | |
aws_secret_access_key_test = creds[:default][:aws_secret_access_key_test] | |
# create a connection | |
connection = Fog::Storage.new({ | |
:provider => 'AWS', | |
:aws_access_key_id => aws_access_key_id_test, | |
:aws_secret_access_key => aws_secret_access_key_test | |
}) | |
expDays = 30 | |
startDate = Time.now.utc | |
ruleName = "your rule name" | |
rulePrefix = "your-prefix" | |
ruleEnabled = true | |
bucket = "my-bucket-name" | |
exp = { | |
"Days" => expDays, | |
"Date" => startDate | |
} | |
life_cycle = { | |
"Rules" => [ | |
{ | |
'ID' => ruleName, | |
'Prefix' => rulePrefix, | |
'Enabled' => ruleEnabled, | |
'Expiration' => { "Days" => expDays, "Date" => startDate } | |
} | |
] | |
} | |
connection.put_bucket_lifecycle(bucket, life_cycle) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment