Skip to content

Instantly share code, notes, and snippets.

@hyuki
Last active July 10, 2020 14:00
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 hyuki/fdb264837179514827c51286e7b37138 to your computer and use it in GitHub Desktop.
Save hyuki/fdb264837179514827c51286e7b37138 to your computer and use it in GitHub Desktop.
wasabi - Simple file backup script for Wasabi.com.
#! /usr/bin/env ruby
# Wasabi.com
# $ grep ~/.bash_profile WASABI_
# export WASABI_ACCESS_KEY_ID=1234ABCD1234ABCD1234
# export WASABI_SECRET_ACCESS_KEY=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
# export WASABI_REGION=us-east-1
# export WASABI_BUCKET=bucketname
require 'aws-sdk-s3'
REGION = ENV['WASABI_REGION']
BUCKET = ENV['WASABI_BUCKET']
class Wasabi
def ls
puts "Today's files:"
prefix = Time.now.strftime("%Y-%m-%d")
response = @client.list_objects_v2(bucket: BUCKET, prefix: prefix)
response.contents.each do |obj|
puts obj.key
end
puts "#{response.contents.size} files."
self
end
def backup
@names.each do |name|
id = Time.now.strftime("%Y-%m-%d-%H%M%S")
objectname = "#{id}-#{File.basename(name)}"
obj = @s3.bucket(BUCKET).object(objectname)
puts "#{name} => #{objectname}"
File.open(name, 'rb') do |file|
obj.put(body: file)
end
end
self
end
def initialize(names)
Aws.config.update({
region: REGION,
credentials: Aws::Credentials.new(ENV['WASABI_ACCESS_KEY_ID'], ENV['WASABI_SECRET_ACCESS_KEY']),
endpoint: "https://s3.#{REGION}.wasabisys.com",
})
@names = names
@client = Aws::S3::Client.new # (region: REGION)
@s3 = Aws::S3::Resource.new(region: REGION)
if @names.length == 0
puts "Usage: wasabi filename"
puts "Backup filename to #{BUCKET} as YYYY-MM-DD-hhmmss-filename"
end
end
end
Wasabi.new(ARGV).backup.ls
@hyuki
Copy link
Author

hyuki commented Jul 10, 2020

$ wasabi hello.zip sample.zip
hello.zip => 2020-07-10-224444-hello.zip
sample.zip => 2020-07-10-224445-sample.zip
Today's files:
2020-07-10-224444-hello.zip
2020-07-10-224445-sample.zip
2 files.

@hyuki
Copy link
Author

hyuki commented Jul 10, 2020

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