mongo-backup 0.1.0

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
+ SHA1:
3
+ metadata.gz: 3f29774706c6dd5b822f8f4bc5c2fb416b567a15
4
+ data.tar.gz: 59379076221f359c0810fa12f53c4280957cc4a9
5
+ SHA512:
6
+ metadata.gz: 2240aa0c026b9b2cb977f9530c5bbcf652b28790750b9690603528931c71f87d197ad41af4b4a5eb4b0851740043d39a672ccee8a039e688cf7886bba28104ef
7
+ data.tar.gz: aec4e851e1a212e53b755fd725cf053db699df8cfbb0a723d9dc6a6ecb479540d145b6c9ec72bafbefac170132b20225081bb08da91786180c81e93681475488
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in character.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Mongo Backup
2
+
3
+ Backup database and upload it to S3, restore tool is included as well. Make sure ```mongodb-client``` package is installed and ```mongodump``` command is available on the production server. If not please run: ```sudo apt-get install mongodb-clients``` — on Ubuntu.
4
+
5
+ ## Setup
6
+
7
+ Add to ```Gemfile```:
8
+
9
+ gem 'mongo-backup'
10
+
11
+ Add environment variables:
12
+
13
+ MONGODB_URL = ::value::
14
+ S3_BACKUPS_BUCKET = ::value::
15
+ AWS_ACCESS_KEY_ID = ::value::
16
+ AWS_SECRET_ACCESS_KEY = ::value::
17
+
18
+ Add cron job:
19
+
20
+ rake mongo:backup
21
+
22
+ ## Rake command available
23
+
24
+ Do backup:
25
+
26
+ rake mongo:backup
27
+
28
+ List available backups:
29
+
30
+ rake mongo:list_backups
31
+
32
+ Restore from FILE:
33
+
34
+ rake mongo:restore FILE=<filename.tag.gz>
35
+
36
+ Restore latest backup to localhost:
37
+
38
+ rake mongo:restore S3_BACKUPS_BUCKET=_ AWS_ACCESS_KEY_ID=_ AWS_SECRET_ACCESS_KEY=_
39
+
40
+ ## Author
41
+
42
+ Alexander Kravets @ [Slate Studio](http://www.slatestudio.com)
43
+
44
+ ## License
45
+
46
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'rubygems/package_task'
2
+
3
+ spec = Gem::Specification.load(Dir['*.gemspec'].first)
4
+ gem = Gem::PackageTask.new(spec)
5
+ gem.define()
@@ -0,0 +1,7 @@
1
+ require 'fog'
2
+
3
+ module MongoBackup
4
+ end
5
+
6
+ require 'mongo_backup/engine'
7
+ require 'mongo_backup/version'
@@ -0,0 +1,7 @@
1
+ module MongoBackup
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace MongoBackup::Rails
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module MongoBackup
2
+ module Rails
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,95 @@
1
+ namespace :mongo do
2
+ desc "Show list of available backup files"
3
+ task :list_backups => :environment do
4
+ backups.each { |file| puts file.key.gsub('backups/', '') }
5
+ end
6
+
7
+ desc "Restore database from backup"
8
+ task :restore => :environment do
9
+ if ENV['FILE']
10
+ restore(ENV['FILE'])
11
+ else
12
+ filename = backups.last.key.gsub('backups/', '')
13
+ restore(filename)
14
+ end
15
+ end
16
+
17
+ desc "Backup database and upload to S3 bucket"
18
+ task :backup => :environment do
19
+ # TODO: check if mongodump command is installed on a server
20
+ # sudo apt-get install mongodb-clients
21
+
22
+ uri, db_name = parse_uri()
23
+ filename = Time.now.strftime("%Y-%m-%d_%H-%M-%S.tar.gz")
24
+ backup_cmd = "mongodump -u #{uri.user} -p #{uri.password} -h #{uri.host}:#{uri.port} -d #{db_name}"
25
+
26
+ system "cd /tmp ; #{backup_cmd} ; GZIP=-9 tar -zcvf #{filename} dump/"
27
+
28
+ bucket.files.create(key: "backups/#{filename}", body: open("/tmp/#{filename}"))
29
+
30
+ system "rm /tmp/#{filename} ; rm -Rf /tmp/dump"
31
+ end
32
+
33
+ private
34
+
35
+ def connection
36
+ @connection ||= Fog::Storage.new({ provider: 'AWS',
37
+ aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
38
+ aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'] })
39
+ end
40
+
41
+ def backups
42
+ connection.directories.get(ENV['S3_BACKUPS_BUCKET'], prefix: 'backups').files
43
+ end
44
+
45
+ def bucket
46
+ connection.directories.new(key: ENV['S3_BACKUPS_BUCKET'])
47
+ end
48
+
49
+ def restore(filename)
50
+ s3_file = bucket.files.get("backups/#{filename}")
51
+ db_name = ::Mongoid::Sessions.default.options[:database]
52
+
53
+ open("/tmp/#{filename}", 'w') { |f| f.binmode ; f.write s3_file.body }
54
+
55
+ system "cd /tmp ; tar -xzf /tmp/#{filename}"
56
+
57
+ ::Mongoid::Sessions.default.drop
58
+
59
+ if Rails.env.production?
60
+ uri, backup_db_name = parse_uri()
61
+ system "mongorestore -u #{uri.user} -p #{uri.password} -h #{uri.host}:#{uri.port} -d #{db_name} /tmp/dump/#{backup_db_name}"
62
+ else
63
+ backup_db_name = Dir.entries('/tmp/dump').select { |file| File.directory? File.join('/tmp/dump', file) }.last
64
+ system "mongorestore -d #{db_name} /tmp/dump/#{backup_db_name}"
65
+ end
66
+
67
+ system "rm /tmp/#{filename} ; rm -Rf /tmp/dump"
68
+ end
69
+
70
+ def parse_uri
71
+ db_url = ENV['MONGODB_URL'] || ENV['MONGO_URL'] || ENV['MONGODB_URI']
72
+
73
+ if db_url.nil?
74
+ puts "ENV['MONGODB_URL'] has to be defined."
75
+ exit
76
+ end
77
+
78
+ # use first mongodb server if a few provided
79
+ first_uri = db_url.split(',').first
80
+ uri = URI.parse(first_uri)
81
+ db_name = db_url.split('/').last
82
+
83
+ return uri, db_name
84
+ end
85
+
86
+ # TODO: sync for c66:
87
+ # 1. Get mongo database uri from c66 via API or toolbelt (if possible or clone and make a feature)
88
+ # 2. Check if possible to use with mongodump
89
+ # 3. Parse uri and pass values to mongodump command if not possible usage of uri with mongodump
90
+ # 4. Execute all backup commands with proper values
91
+
92
+ #"""mongodump -u admin -p '' -h ds029120-a0.mongolab.com:29120 -d wta
93
+ # mongo wta --eval "db.dropDatabase()"
94
+ # mongorestore dump/wta"""
95
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mongo_backup/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mongo-backup"
8
+ spec.version = MongoBackup::Rails::VERSION
9
+ spec.authors = ["Slate Studio"]
10
+ spec.email = ["alex@slatestudio.com"]
11
+ spec.description = %q{Backup (restore) mongo database and upload backup file to S3}
12
+ spec.summary = %q{Backup (restore) mongo database and upload backup file to S3}
13
+ spec.homepage = "http://www.slatestudio.com"
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_runtime_dependency 'fog'
22
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo-backup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Slate Studio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fog
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Backup (restore) mongo database and upload backup file to S3
28
+ email:
29
+ - alex@slatestudio.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - Gemfile
35
+ - README.md
36
+ - Rakefile
37
+ - lib/mongo-backup.rb
38
+ - lib/mongo_backup/engine.rb
39
+ - lib/mongo_backup/version.rb
40
+ - lib/tasks/mongo.rake
41
+ - mongo-backup.gemspec
42
+ homepage: http://www.slatestudio.com
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.2.1
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Backup (restore) mongo database and upload backup file to S3
66
+ test_files: []