Class: S3StreamBackup

Inherits:
Object
  • Object
show all
Defined in:
lib/s3streambackup.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ S3StreamBackup

Returns a new instance of S3StreamBackup.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/s3streambackup.rb', line 30

def initialize(&block)
	instance_eval &block
	cli_setup = @cli_setup
	cli_verify_setup = @cli_verify_setup
	settings = CLI.new do
			option :key, 
				short: :k,
				description: 'S3 key',
				default: ENV['AWS_ACCESS_KEY_ID']
			option :secret, 
				short: :s,
				description: 'S3 key secret',
				default_label: '<secret>',
				default: ENV['AWS_SECRET_ACCESS_KEY']
			option :prefix,
				short: :p,
				description: 'prefix under which the backup objects are kept',
				default: ''
			option :log_file,
				short: :l,
				description: 'location of log file; if not specifed log to STDERR'
			switch :plain,
				description: 'use plain connections instead of SSL to S3'
			switch :verbose,
				short: :v,
				description: 'log debug messages'
			switch :debug,
				short: :d,
				description: 'log AWS SDK debug messages'
			argument :bucket,
				description: 'name of bucket to upload data to'
			argument :name,
				description: 'name under which the object will be stored'
			instance_eval &cli_setup if cli_setup
	end.parse! do |settings|
		fail 'AWS_ACCESS_KEY_ID environment not set and --key not used' unless settings.key
		fail 'AWS_SECRET_ACCESS_KEY environment not set and --secret not used' unless settings.secret
		instance_eval &cli_verify_setup if cli_verify_setup
	end

	log = Logger.new(settings.log_file ? settings.log_file : STDERR)
	log.formatter = proc do |severity, datetime, progname, msg|
		"[#{datetime.utc.strftime "%Y-%m-%d %H:%M:%S.%6N %Z"}] [#{$$}] #{severity}: #{msg.strip}\n"
	end

	log.level = Logger::INFO
	log.level = Logger::DEBUG if settings.verbose or settings.debug

	begin
		s3 = AWS::S3.new(
			access_key_id: settings.key,
			secret_access_key: settings.secret,
			logger: settings.debug ? log : nil,
			log_level: :debug,
			use_ssl: ! settings.plain
		)
		@main.call(settings, log, s3)
	rescue => error
		msg = "#{error.class.name}: #{error.message}\n#{error.backtrace.join("\n")}"
		log.error msg
		STDERR.write msg 
		exit 10
	end
end

Instance Method Details

#cli(&block) ⇒ Object



95
96
97
# File 'lib/s3streambackup.rb', line 95

def cli(&block)
	@cli_setup = block
end

#cli_verify(&block) ⇒ Object



99
100
101
# File 'lib/s3streambackup.rb', line 99

def cli_verify(&block)
	@cli_verify_setup = block
end

#main(&block) ⇒ Object



103
104
105
# File 'lib/s3streambackup.rb', line 103

def main(&block)
	@main = block
end