savior 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 Mateusz Zawisza.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,7 @@
1
+ [![Build Status](https://secure.travis-ci.org/mateuszzawisza/savior.png)](http://travis-ci.org/mateuszzawisza/savior)
2
+
3
+ Savior
4
+ ==
5
+
6
+ Savior is a ruby library (gem) that provides easy to setup MySQL database backups to Amazon AWS S3
7
+
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ require 'rspec/core/rake_task'
9
+
10
+ desc 'Default: run specs.'
11
+ task :default => :spec
12
+
13
+ desc "Run all specs"
14
+ RSpec::Core::RakeTask.new(:spec)
15
+
16
+ #Bundler::GemHelper.install_tasks
data/bin/savior ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
4
+ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
5
+
6
+ require "savior"
7
+ require "yaml"
8
+ require "optparse"
9
+
10
+ options = {}
11
+ OptionParser.new do |opts|
12
+ opts.banner = "Usage: savior [options]"
13
+ opts.on("-c PATH", "--config-file=PATH", String, "specify config file") do |path|
14
+ options[:config_file] = path
15
+ end
16
+ end.parse!
17
+
18
+ @config = YAML.load_file(options[:config_file])
19
+
20
+ if @config
21
+ savior = Savior.new
22
+ savior.database(@config[:database])
23
+ savior.storage(@config[:storage])
24
+ savior.save
25
+ end
@@ -0,0 +1,50 @@
1
+ class Savior
2
+ class Database
3
+ def initialize(options = {})
4
+ default_options = {
5
+ :user => nil,
6
+ :password => nil,
7
+ :host => "localhost",
8
+ :port => "3306",
9
+ :database_name => nil,
10
+ }
11
+ options = default_options.merge(options)
12
+ @user = options[:user]
13
+ @password = options[:password]
14
+ @host = options[:host]
15
+ @port = options[:port]
16
+ @database_name = options[:database_name]
17
+ end
18
+
19
+ def create_snapshot
20
+ db_snapshot_file = set_db_snapshot_file_name
21
+ file = File.open(db_snapshot_file, "w+")
22
+ IO.popen("mysqldump #{mysql_command_line_options}","r+") do |pipe|
23
+ pipe.close_write
24
+ while (line = pipe.gets)
25
+ file.puts line
26
+ end
27
+ end
28
+ file.close
29
+ db_snapshot_file
30
+ end
31
+
32
+ private
33
+ def mysql_command_line_options
34
+ cli_options = []
35
+ cli_options << "-u #{@user}"
36
+ cli_options << "-h #{@host}"
37
+ cli_options << "-p#{@password}" if @password
38
+ cli_options << "-P #{@port}"
39
+ cli_options << "#{@database_name}"
40
+ cli_options << "--single-transaction"
41
+ cli_options.join(" ")
42
+ end
43
+
44
+ def set_db_snapshot_file_name
45
+ "#{@database_name}_" +
46
+ "snapshot_#{Time.now.strftime("%Y%m%d%H%M%S")}.sql"
47
+ end
48
+ #EOF private
49
+ end
50
+ end
@@ -0,0 +1,27 @@
1
+ require "aws"
2
+
3
+ class Savior
4
+ class Storage
5
+ def initialize(options={})
6
+ default_options = {
7
+ :access_key_id => ENV['AMAZON_ACCESS_KEY_ID'],
8
+ :secret_access_key => ENV['AMAZON_SECRET_ACCESS_KEY'],
9
+ :bucket_name => nil
10
+ }
11
+ options = default_options.merge(options)
12
+ @access_key_id = options[:access_key_id]
13
+ @secret_access_key = options[:secret_access_key]
14
+ @bucket_name = options[:bucket_name]
15
+ @s3 = AWS::S3.new(
16
+ :access_key_id => @access_key_id,
17
+ :secret_access_key => @secret_access_key
18
+ )
19
+ end
20
+
21
+ def upload_file(db_snapshot_file)
22
+ s3_snapshot_object = @s3.buckets[@bucket_name].
23
+ objects[db_snapshot_file]
24
+ s3_snapshot_object.write(File.read(db_snapshot_file))
25
+ end
26
+ end
27
+ end
data/lib/savior.rb ADDED
@@ -0,0 +1,27 @@
1
+ require File.join(File.dirname(__FILE__), "savior/database.rb")
2
+ require File.join(File.dirname(__FILE__), "savior/storage.rb")
3
+
4
+ class Savior
5
+ # load db configuration
6
+ def database(options)
7
+ @db = Savior::Database.new(options)
8
+ end
9
+
10
+ # load aws configuration
11
+ def storage(options)
12
+ @storage = Savior::Storage.new(options)
13
+ end
14
+
15
+ def save
16
+ check_configuration
17
+ db_snapshot_file = @db.create_snapshot
18
+ @storage.upload_file(db_snapshot_file)
19
+ end
20
+
21
+ private
22
+ def check_configuration
23
+ raise "Database configuration not set" unless @db
24
+ raise "Storage configuration not set" unless @storage
25
+ end
26
+ #EOF private
27
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: savior
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mateusz Zawisza
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: aws-sdk
16
+ requirement: &70161529224820 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.3.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70161529224820
25
+ description: MySQL database backups to AWS S3
26
+ email: mateusz@applicake.com
27
+ executables:
28
+ - savior
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - lib/savior/database.rb
33
+ - lib/savior/storage.rb
34
+ - lib/savior.rb
35
+ - bin/savior
36
+ - MIT-LICENSE
37
+ - Rakefile
38
+ - README.markdown
39
+ homepage: https://github.com/mateuszzawisza/savior
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.10
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: MySQL database backups to AWS S3
63
+ test_files: []