Last active
July 10, 2020 14:00
-
-
Save hyuki/fdb264837179514827c51286e7b37138 to your computer and use it in GitHub Desktop.
wasabi - Simple file backup script for Wasabi.com.
This file contains 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 | |
# 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 | |
Author
hyuki
commented
Jul 10, 2020
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment