mongo_backer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ .bundle
2
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source :rubygems
2
+
3
+ gem 'aws-s3', '>= 0.6.2'
4
+ gem 'thor', '>= 0.13.8'
5
+
6
+ group :gems do
7
+ gem 'jeweler'
8
+ gem 'gemcutter'
9
+ end
10
+
11
+ group :test do
12
+ gem 'rspec', '>= 2.0.0.beta.19'
13
+ end
@@ -0,0 +1,39 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ aws-s3 (0.6.2)
5
+ builder
6
+ mime-types
7
+ xml-simple
8
+ builder (2.1.2)
9
+ diff-lcs (1.1.2)
10
+ gemcutter (0.6.1)
11
+ git (1.2.5)
12
+ jeweler (1.4.0)
13
+ gemcutter (>= 0.1.0)
14
+ git (>= 1.2.5)
15
+ rubyforge (>= 2.0.0)
16
+ json_pure (1.4.3)
17
+ mime-types (1.16)
18
+ rspec (2.0.0.beta.19)
19
+ rspec-core (= 2.0.0.beta.19)
20
+ rspec-expectations (= 2.0.0.beta.19)
21
+ rspec-mocks (= 2.0.0.beta.19)
22
+ rspec-core (2.0.0.beta.19)
23
+ rspec-expectations (2.0.0.beta.19)
24
+ diff-lcs (>= 1.1.2)
25
+ rspec-mocks (2.0.0.beta.19)
26
+ rubyforge (2.0.4)
27
+ json_pure (>= 1.1.7)
28
+ thor (0.14.0)
29
+ xml-simple (1.0.12)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ aws-s3 (>= 0.6.2)
36
+ gemcutter
37
+ jeweler
38
+ rspec (>= 2.0.0.beta.19)
39
+ thor (>= 0.13.8)
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Ryan Fitzgerald
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.
@@ -0,0 +1,11 @@
1
+ = Mongobacker
2
+ command line tool to backup and restore mongoDB databases using s3
3
+
4
+ mongobacker is currently in very early development and at this time is not usable
5
+
6
+ == Installation
7
+
8
+ tbd
9
+
10
+ == Setup
11
+ tbd
@@ -0,0 +1,24 @@
1
+ require 'rake'
2
+ require "rspec/core/rake_task"
3
+ require File.dirname(__FILE__) + "/lib/mongo_backer/version"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ begin
8
+ require 'jeweler'
9
+ Jeweler::Tasks.new do |gemspec|
10
+ gemspec.name = "mongo_backer"
11
+ gemspec.version = MongoBacker::Version
12
+ gemspec.summary = "command line tool to backup mongoDB databases to s3"
13
+ gemspec.description = "mongo backer uses mongodump to perform backups on a live running mongodb instance and then zips and uploads the backup to s3"
14
+ gemspec.email = "ryan.fitz1@gmail.com"
15
+ gemspec.homepage = "http://github.com/ryanfitz/mongobacker"
16
+ gemspec.authors = ["Ryan Fitzgerald"]
17
+
18
+ gemspec.add_dependency('aws-s3', '>= 0.6.2')
19
+ gemspec.add_dependency('thor', '>= 0.13.8')
20
+ end
21
+ Jeweler::GemcutterTasks.new
22
+ rescue LoadError
23
+ puts "Jeweler not available. Install it with: gem install jeweler"
24
+ end
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems' unless defined?(Gem)
3
+
4
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
5
+
6
+ require 'mongo_backer'
7
+
8
+ # We need our config boot because we need to load registered generators so:
9
+ MongoBacker::Cli.start(ARGV)
@@ -0,0 +1,9 @@
1
+ require 'thor'
2
+ require 'yaml'
3
+ require 'aws/s3'
4
+
5
+ require 'mongo_backer/version'
6
+ require 'mongo_backer/actions'
7
+ require 'mongo_backer/configuration'
8
+ require 'mongo_backer/cli'
9
+ require 'mongo_backer/s3_manager'
@@ -0,0 +1,51 @@
1
+ module MongoBacker
2
+ module Actions
3
+ include Thor::Actions
4
+
5
+ def ask_with_default (msg, default_value)
6
+ answer = ask(msg + "(#{default_value})")
7
+
8
+ if answer.nil? or answer.empty?
9
+ answer = default_value
10
+ end
11
+
12
+ answer
13
+ end
14
+
15
+ def run_mongodump(config)
16
+ time = Time.new.strftime("%Y_%m_%d_%H-%M-%S")
17
+ backup_dir = "#{Dir.tmpdir}/mongodump_#{time}"
18
+
19
+ run("#{config.mongodump} -h #{config.host}:#{config.port} -o #{backup_dir}")
20
+
21
+ backup_dir
22
+ end
23
+
24
+ def gzip_directory(dir)
25
+ zip_file = "#{dir}.tar.gz"
26
+ run("tar -czf #{zip_file} #{dir}")
27
+
28
+ zip_file
29
+ end
30
+
31
+ def upload_to_s3(file, config)
32
+ say "uploading backup to s3"
33
+
34
+ manager = MongoBacker::S3Manager.new config
35
+ manager.upload_file file
36
+
37
+ say "finished uploading backup to s3"
38
+ end
39
+
40
+ def list_backups(config)
41
+ manager = MongoBacker::S3Manager.new config
42
+ backups = manager.find_all
43
+
44
+ backups.each do |backup|
45
+ say "#{backup.key} created at #{backup.last_modified}"
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,58 @@
1
+ require 'tmpdir'
2
+
3
+ module MongoBacker
4
+ class Cli < Thor
5
+ include Thor::Actions
6
+ include MongoBacker::Actions
7
+
8
+ def self.source_root
9
+ File.dirname(__FILE__)
10
+ end
11
+
12
+ desc "init", "Generates a configuration file into the current working directory"
13
+ def init
14
+ @host = ask_with_default("mongo host: ", "localhost")
15
+ @port = ask_with_default("mongo port: ", "27017")
16
+ @mongodump = ask_with_default("mongodump path: ", "/usr/bin/mongodump")
17
+ @access_key_id = ask("s3 access key id:")
18
+ @secret_access_key = ask("s3 secret access key:")
19
+ @bucket = ask("s3 bucket:")
20
+
21
+ template('templates/config.tt', "mongobacker.yml")
22
+ end
23
+
24
+ desc "backup", "backup the configured mongodb instance to s3"
25
+ method_option :config, :type => :string, :aliases => "-c", :required => true,
26
+ :desc => "Path to mongo backer config file"
27
+ def backup
28
+ config = MongoBacker::Configuration.new options[:config]
29
+
30
+ backup_dir = run_mongodump(config)
31
+
32
+ backup_file = gzip_directory(backup_dir)
33
+
34
+ upload_to_s3(backup_file, config)
35
+
36
+ FileUtils.rm_rf backup_dir
37
+ FileUtils.rm backup_file
38
+ end
39
+
40
+ desc "list", "list the backups currently in s3"
41
+ method_option :config, :type => :string, :aliases => "-c", :required => true,
42
+ :desc => "Path to mongo backer config file"
43
+ def list
44
+ config = MongoBacker::Configuration.new options[:config]
45
+ list_backups config
46
+ end
47
+
48
+ desc "version", "mongobacker version"
49
+ def version()
50
+ puts "mongobacker version #{MongoBacker::Version}"
51
+ end
52
+
53
+ # def help
54
+ # puts "helpppppp"
55
+ # end
56
+ end
57
+
58
+ end
@@ -0,0 +1,22 @@
1
+ module MongoBacker
2
+ class Configuration
3
+ attr_accessor :host, :port, :access_key_id, :secret_access_key, :mongodump, :bucket
4
+
5
+ def initialize(*args)
6
+ unless args.empty?
7
+ config = YAML.load_file(args[0])
8
+ @host = config["mongo"]["host"]
9
+ @port = config["mongo"]["port"]
10
+ @mongodump = config["mongo"]["mongodump"]
11
+ @access_key_id = config["s3"]["access_key_id"]
12
+ @secret_access_key = config["s3"]["secret_access_key"]
13
+ @bucket = config["s3"]["bucket"]
14
+ end
15
+
16
+ rescue Errno::ENOENT
17
+ raise ArgumentError, "file not found at #{args[0]}"
18
+
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,37 @@
1
+ module MongoBacker
2
+ class S3Manager
3
+ include AWS::S3
4
+
5
+ attr_reader :bucket
6
+
7
+ def initialize(config)
8
+ AWS::S3::Base.establish_connection!(
9
+ :access_key_id => config.access_key_id,
10
+ :secret_access_key => config.secret_access_key
11
+ )
12
+
13
+ @bucket = create_bucket config.bucket
14
+ end
15
+
16
+ def create_bucket(bucket)
17
+ if Service.buckets.collect{ |b| b.name }.include?(bucket)
18
+ Bucket.find(bucket)
19
+ else
20
+ Bucket.create(bucket)
21
+ Bucket.find(bucket)
22
+ end
23
+ end
24
+
25
+ def upload_file(file)
26
+ name = File.basename file
27
+
28
+ S3Object.store(name, open(file), @bucket.name)
29
+ end
30
+
31
+ def find_all
32
+ @bucket.objects
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,9 @@
1
+ mongo:
2
+ host: <%= @host %>
3
+ port: <%= @port %>
4
+ mongodump: <%= @mongodump %>
5
+
6
+ s3:
7
+ access_key_id: <%= @access_key_id %>
8
+ secret_access_key: <%= @secret_access_key %>
9
+ bucket: <%= @bucket %>
@@ -0,0 +1,3 @@
1
+ module MongoBacker
2
+ Version = '0.0.1'
3
+ end
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mongo_backer}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ryan Fitzgerald"]
12
+ s.date = %q{2010-07-30}
13
+ s.default_executable = %q{mongobacker}
14
+ s.description = %q{mongo backer uses mongodump to perform backups on a live running mongodb instance and then zips and uploads the backup to s3}
15
+ s.email = %q{ryan.fitz1@gmail.com}
16
+ s.executables = ["mongobacker"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".gitignore",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE",
26
+ "README.rdoc",
27
+ "Rakefile",
28
+ "bin/mongobacker",
29
+ "lib/mongo_backer.rb",
30
+ "lib/mongo_backer/actions.rb",
31
+ "lib/mongo_backer/cli.rb",
32
+ "lib/mongo_backer/configuration.rb",
33
+ "lib/mongo_backer/s3_manager.rb",
34
+ "lib/mongo_backer/templates/config.tt",
35
+ "lib/mongo_backer/version.rb",
36
+ "mongo_backer.gemspec",
37
+ "spec/configuration_spec.rb",
38
+ "spec/fixtures/valid_config.yml",
39
+ "spec/s3_manager_spec.rb",
40
+ "spec/spec_helper.rb"
41
+ ]
42
+ s.homepage = %q{http://github.com/ryanfitz/mongobacker}
43
+ s.rdoc_options = ["--charset=UTF-8"]
44
+ s.require_paths = ["lib"]
45
+ s.rubygems_version = %q{1.3.7}
46
+ s.summary = %q{command line tool to backup mongoDB databases to s3}
47
+ s.test_files = [
48
+ "spec/configuration_spec.rb",
49
+ "spec/s3_manager_spec.rb",
50
+ "spec/spec_helper.rb"
51
+ ]
52
+
53
+ if s.respond_to? :specification_version then
54
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
55
+ s.specification_version = 3
56
+
57
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
58
+ s.add_runtime_dependency(%q<aws-s3>, [">= 0.6.2"])
59
+ s.add_runtime_dependency(%q<thor>, [">= 0.13.8"])
60
+ else
61
+ s.add_dependency(%q<aws-s3>, [">= 0.6.2"])
62
+ s.add_dependency(%q<thor>, [">= 0.13.8"])
63
+ end
64
+ else
65
+ s.add_dependency(%q<aws-s3>, [">= 0.6.2"])
66
+ s.add_dependency(%q<thor>, [">= 0.13.8"])
67
+ end
68
+ end
69
+
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe MongoBacker::Configuration do
4
+
5
+ it "correctly reads all settings from valid yaml config" do
6
+ config = MongoBacker::Configuration.new File.expand_path(File.dirname(__FILE__) + '/fixtures/valid_config.yml')
7
+
8
+ config.host.should eq "localhost"
9
+ config.port.should eq 27017
10
+ config.mongodump.should eq "/usr/bin/mongodump"
11
+ config.bucket.should eq "mongo_backup"
12
+ config.access_key_id.should eq 123
13
+ config.secret_access_key.should eq "asdf"
14
+ end
15
+
16
+ it "raises exception when yaml file is not found" do
17
+ lambda { MongoBacker::Configuration.new("i_dont_exist.yml") }.should raise_error(ArgumentError)
18
+ end
19
+
20
+ it "has default constructor" do
21
+ MongoBacker::Configuration.new.should_not be_nil
22
+ end
23
+
24
+ end
@@ -0,0 +1,9 @@
1
+ mongo:
2
+ host: localhost
3
+ port: 27017
4
+ mongodump: /usr/bin/mongodump
5
+
6
+ s3:
7
+ access_key_id: 123
8
+ secret_access_key: asdf
9
+ bucket: mongo_backup
@@ -0,0 +1,52 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe MongoBacker::S3Manager do
4
+
5
+ before :each do
6
+ @config = MongoBacker::Configuration.new
7
+ @config.access_key_id = "some_key_id"
8
+ @config.secret_access_key = "some_access_key"
9
+ @config.bucket = "some_bucket"
10
+
11
+ AWS::S3::Base.stub! :establish_connection!
12
+ end
13
+
14
+ it "creates new s3 bucket" do
15
+ mock_s3_bucket_exists "another_bucket"
16
+ expects_creates_new_s3_bucket @config.bucket
17
+ expects_find_existing_bucket @config.bucket
18
+
19
+ manager = MongoBacker::S3Manager.new @config
20
+ manager.bucket.should eq @config.bucket
21
+ end
22
+
23
+ it "returns existing bucket that exists in s3" do
24
+ mock_s3_bucket_exists @config.bucket
25
+ expects_find_existing_bucket @config.bucket
26
+
27
+ manager = MongoBacker::S3Manager.new @config
28
+ manager.bucket.should eq @config.bucket
29
+ end
30
+
31
+ def mock_s3_bucket_exists(*names)
32
+ mock_buckets = []
33
+
34
+ names.each do |name|
35
+ m = mock(name)
36
+ m.should_receive(:name).once.and_return(name)
37
+
38
+ mock_buckets << m
39
+ end
40
+
41
+ AWS::S3::Service.stub!(:buckets).and_return(mock_buckets)
42
+ end
43
+
44
+ def expects_creates_new_s3_bucket(name)
45
+ AWS::S3::Bucket.should_receive(:create).with(name).and_return name
46
+ end
47
+
48
+ def expects_find_existing_bucket(name)
49
+ AWS::S3::Bucket.should_receive(:find).with(name).and_return name
50
+ end
51
+
52
+ end
@@ -0,0 +1,11 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
2
+
3
+ require 'rspec'
4
+ # optionally add autorun support
5
+ require 'rspec/autorun'
6
+
7
+ Rspec.configure do |c|
8
+ c.mock_with :rspec
9
+ end
10
+
11
+ require 'mongo_backer'
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo_backer
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Ryan Fitzgerald
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-30 00:00:00 -04:00
18
+ default_executable: mongobacker
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: aws-s3
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 6
31
+ - 2
32
+ version: 0.6.2
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: thor
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 0
45
+ - 13
46
+ - 8
47
+ version: 0.13.8
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ description: mongo backer uses mongodump to perform backups on a live running mongodb instance and then zips and uploads the backup to s3
51
+ email: ryan.fitz1@gmail.com
52
+ executables:
53
+ - mongobacker
54
+ extensions: []
55
+
56
+ extra_rdoc_files:
57
+ - LICENSE
58
+ - README.rdoc
59
+ files:
60
+ - .gitignore
61
+ - Gemfile
62
+ - Gemfile.lock
63
+ - LICENSE
64
+ - README.rdoc
65
+ - Rakefile
66
+ - bin/mongobacker
67
+ - lib/mongo_backer.rb
68
+ - lib/mongo_backer/actions.rb
69
+ - lib/mongo_backer/cli.rb
70
+ - lib/mongo_backer/configuration.rb
71
+ - lib/mongo_backer/s3_manager.rb
72
+ - lib/mongo_backer/templates/config.tt
73
+ - lib/mongo_backer/version.rb
74
+ - mongo_backer.gemspec
75
+ - spec/configuration_spec.rb
76
+ - spec/fixtures/valid_config.yml
77
+ - spec/s3_manager_spec.rb
78
+ - spec/spec_helper.rb
79
+ has_rdoc: true
80
+ homepage: http://github.com/ryanfitz/mongobacker
81
+ licenses: []
82
+
83
+ post_install_message:
84
+ rdoc_options:
85
+ - --charset=UTF-8
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ requirements: []
105
+
106
+ rubyforge_project:
107
+ rubygems_version: 1.3.7
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: command line tool to backup mongoDB databases to s3
111
+ test_files:
112
+ - spec/configuration_spec.rb
113
+ - spec/s3_manager_spec.rb
114
+ - spec/spec_helper.rb