snapshooter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA512:
3
+ data.tar.gz: f1d9d9da326c97b443ad449e6bbc008fda31ad54931755a9dd863ac9217937faba8025c0fde0b1188b2ec45d9c1e74509e7364528f05e345baa2ae485c385318
4
+ metadata.gz: 06cf492690e9d4c971717ed0b75c25be8fa7c5b2d946880dff9c23439b8373dff42e7c414e0a97180a02296d1c7cc00134771722a2ffa0f35756e611f463ff12
5
+ SHA1:
6
+ data.tar.gz: 36a0a5174a10f743e221c8ae4ac7e93763239dfd
7
+ metadata.gz: fd9ee346389d36362fba8638436f6e4fd820627b
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in snapshooter.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Julio Santos
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Snapshooter
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'snapshooter'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install snapshooter
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,54 @@
1
+ module Snapshooter
2
+ class Base
3
+ include Log
4
+
5
+ def initialize(id, storage=:local)
6
+ @id = id
7
+ case storage
8
+ when :s3
9
+ @storage = Storage::S3.new
10
+ when :local
11
+ @storage = Storage::Local.new
12
+ else
13
+ raise "Unknown storage type: #{storage}"
14
+ end
15
+ end
16
+
17
+ def restore
18
+ datastores.each do |datastore|
19
+ filename = "#{@id}-#{datastore.filename}"
20
+
21
+ @storage.get filename do |file|
22
+ with_log("Restore from #{filename}") do
23
+ datastore.restore(file)
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ def snapshot!
30
+ datastores.each do |datastore|
31
+ filename = "#{@id}-#{datastore.filename}"
32
+
33
+ @storage.save filename do |tmp_file|
34
+ with_log "Snapshot to #{filename}" do
35
+ datastore.snapshot tmp_file
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def datastores
44
+ Snapshooter.datastores.map do |store|
45
+ case store[:type]
46
+ when :mysql
47
+ Datastore::Mysql.new(store[:config])
48
+ else
49
+ raise "Unknown Datastore: #{store[:type]}"
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,50 @@
1
+ require 'optparse'
2
+
3
+ module Snapshooter
4
+ class CLI
5
+ def self.start
6
+ new.run
7
+ end
8
+
9
+ def initialize
10
+ @options = {
11
+ :storage => :local,
12
+ :id => Time.now.to_i,
13
+ :restore => false
14
+ }
15
+ @options.merge! parse_options
16
+ end
17
+
18
+ def run
19
+ shooter = Base.new(@options[:id], @options[:storage])
20
+ if @options[:restore]
21
+ shooter.restore
22
+ else
23
+ shooter.snapshot!
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def parse_options
30
+ options = {}
31
+ OptionParser.new do |opts|
32
+ opts.banner = "Usage: script/snapshot [options]"
33
+
34
+ opts.on("-i", "--id [IDENTIFIER]", "The identifier for the snapshot (default current timestamp)") do |identifier|
35
+ options[:id] = identifier
36
+ end
37
+
38
+ opts.on("-s", "--storage [STORAGE]", "Select the storage mechanism to use (default local)") do |storage|
39
+ options[:storage] = storage.to_sym
40
+ end
41
+
42
+ opts.on("--restore", "Restores a snapshot given the identifier") do
43
+ options[:restore] = true
44
+ end
45
+ end.parse!
46
+
47
+ options
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,42 @@
1
+ require 'digest/md5'
2
+
3
+ module Snapshooter
4
+ module Datastore
5
+ class Mysql
6
+ def initialize(options={})
7
+ @options = options
8
+ end
9
+
10
+ def snapshot(tmp_file)
11
+ `mysqldump #{mysqldump_options} #{@options[:database]} > #{tmp_file}`
12
+ end
13
+
14
+ def restore(file)
15
+ `mysql #{mysql_options} #{@options[:database]} < #{file}`
16
+ end
17
+
18
+ def filename
19
+ "#{id}.sql"
20
+ end
21
+
22
+ private
23
+
24
+ def id
25
+ Digest::MD5.hexdigest(@options.to_s)
26
+ end
27
+
28
+ def mysql_options
29
+ options = []
30
+ options << "-u#{@options[:user]}" if @options[:user]
31
+ options << "-p#{@options[:password]}" if @options[:password]
32
+ options << "-h#{@options[:host]}" if @options[:host]
33
+
34
+ options.join(" ")
35
+ end
36
+
37
+ def mysqldump_options
38
+ ["--single-transaction", mysql_options].join(" ")
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,12 @@
1
+ module Snapshooter
2
+ module Log
3
+ def with_log(message, &block)
4
+ t0 = Time.now
5
+ puts "[START] #{message}"
6
+ result = yield
7
+ t1 = Time.now
8
+ puts "[END] #{message} (it took #{(t1-t0)/60.0} minutes)"
9
+ result
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ module Snapshooter
2
+ module Storage
3
+ class Base
4
+ include Log
5
+
6
+ def save(filename, &block)
7
+ raise "NotImplemented"
8
+ end
9
+
10
+ def get(filename, &block)
11
+ raise "NotImplemented"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,30 @@
1
+ require 'fileutils'
2
+
3
+ module Snapshooter
4
+ module Storage
5
+ class Local < Base
6
+ def save(filename, &block)
7
+ with_log("Save snapshots/#{filename} to local") do
8
+ FileUtils.mkdir_p "snapshots" unless File.exists?("snapshots")
9
+
10
+ file = Tempfile.new("snapshooter_local_storage")
11
+ file.close
12
+
13
+ begin
14
+ yield file.path
15
+
16
+ File.open "snapshots/#{filename}", "w" do |f|
17
+ f.write File.read(file.path)
18
+ end
19
+ ensure
20
+ file.unlink
21
+ end
22
+ end
23
+ end
24
+
25
+ def get(filename, &block)
26
+ yield "snapshots/#{filename}"
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,58 @@
1
+ require 'tempfile'
2
+
3
+ module Snapshooter
4
+ module Storage
5
+ class S3 < Base
6
+ def save(filename, &block)
7
+ with_log("Save #{bucket}/#{filename} to S3") do
8
+ directory = connection.directories.create(
9
+ :key => bucket,
10
+ :public => false
11
+ )
12
+
13
+ file = Tempfile.new("snapshooter_s3_storage")
14
+ file.close
15
+
16
+ begin
17
+ yield file.path
18
+
19
+ directory.files.create(
20
+ :key => filename,
21
+ :body => File.open(file.path),
22
+ :public => false
23
+ )
24
+ ensure
25
+ file.unlink
26
+ end
27
+ end
28
+ end
29
+
30
+ def get(filename, &block)
31
+ directory = connection.directories.get(bucket)
32
+ file = directory.files.get(filename)
33
+
34
+ with_tempfile file.body, &block
35
+ end
36
+
37
+ private
38
+
39
+ def bucket
40
+ "#{Snapshooter.environment}-snapshots-#{Snapshooter.application_name}"
41
+ end
42
+
43
+ def connection
44
+ FogFactory.storage
45
+ end
46
+
47
+ def with_tempfile(data)
48
+ file = Tempfile.new("snapshooter_s3_storage")
49
+ file.write data
50
+ file.close
51
+
52
+ yield file.path
53
+
54
+ file.unlink
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module Snapshooter
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,12 @@
1
+ require 'snapshooter/version'
2
+ require 'snapshooter/log'
3
+ require 'snapshooter/base'
4
+ require 'snapshooter/cli'
5
+ require 'snapshooter/storage/base'
6
+ require 'snapshooter/storage/local'
7
+ require 'snapshooter/storage/s3'
8
+ require 'snapshooter/datastore/mysql'
9
+
10
+ module Snapshooter
11
+ mattr_accessor :datastores, :environment, :application_name
12
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'snapshooter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "snapshooter"
8
+ spec.version = Snapshooter::VERSION
9
+ spec.authors = ["Julio Santos", "Trae Robrock"]
10
+ spec.email = ["julio@morgane.com", "trobrock@gmail.com"]
11
+ spec.description = %q{Simple tool for snapshotting application data.}
12
+ spec.summary = %q{Simple tool for snapshotting application data.}
13
+ spec.homepage = "https://github.com/trobrock/snapshooter"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snapshooter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Julio Santos
8
+ - Trae Robrock
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2013-10-28 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: "1.3"
23
+ type: :development
24
+ version_requirements: *id001
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ prerelease: false
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - &id003
31
+ - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ type: :development
35
+ version_requirements: *id002
36
+ description: Simple tool for snapshotting application data.
37
+ email:
38
+ - julio@morgane.com
39
+ - trobrock@gmail.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - LICENSE.txt
50
+ - README.md
51
+ - Rakefile
52
+ - lib/snapshooter.rb
53
+ - lib/snapshooter/base.rb
54
+ - lib/snapshooter/cli.rb
55
+ - lib/snapshooter/datastore/mysql.rb
56
+ - lib/snapshooter/log.rb
57
+ - lib/snapshooter/storage/base.rb
58
+ - lib/snapshooter/storage/local.rb
59
+ - lib/snapshooter/storage/s3.rb
60
+ - lib/snapshooter/version.rb
61
+ - snapshooter.gemspec
62
+ homepage: https://github.com/trobrock/snapshooter
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+
67
+ post_install_message:
68
+ rdoc_options: []
69
+
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - *id003
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - *id003
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 2.0.4
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Simple tool for snapshotting application data.
85
+ test_files: []
86
+
87
+ has_rdoc: