heroku-mongo-backup 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- # coding: UTF-8
1
+ # encoding: UTF-8
2
2
 
3
3
  require 'mongo'
4
4
  require 'bson'
@@ -9,7 +9,9 @@ require 'yaml'
9
9
  require 'rubygems'
10
10
  require 's3'
11
11
 
12
- module Heroku::Mongo
12
+ module HerokuMongoBackup
13
+ require 'heroku_mongo_backup/railtie' if defined?(Rails)
14
+
13
15
  class Backup
14
16
  def chdir
15
17
  Dir.chdir("tmp")
@@ -19,30 +21,30 @@ module Heroku::Mongo
19
21
  end
20
22
  Dir.chdir("dump")
21
23
  end
22
-
24
+
23
25
  def store
24
26
  backup = {}
25
-
27
+
26
28
  @db.collections.each do |col|
27
29
  backup['system.indexes.db.name'] = col.db.name if col.name == "system.indexes"
28
-
30
+
29
31
  records = []
30
-
32
+
31
33
  col.find().each do |record|
32
34
  records << record
33
35
  end
34
36
 
35
37
  backup[col.name] = records
36
38
  end
37
-
39
+
38
40
  marshal_dump = Marshal.dump(backup)
39
-
41
+
40
42
  file = File.new(@file_name, 'w')
41
43
  file = Zlib::GzipWriter.new(file)
42
44
  file.write marshal_dump
43
45
  file.close
44
46
  end
45
-
47
+
46
48
  def load
47
49
  file = Zlib::GzipReader.open(@file_name)
48
50
  obj = Marshal.load file.read
@@ -50,15 +52,15 @@ module Heroku::Mongo
50
52
 
51
53
  obj.each do |col_name, records|
52
54
  next if col_name =~ /^system\./
53
-
55
+
54
56
  @db.drop_collection(col_name)
55
57
  dest_col = @db.create_collection(col_name)
56
-
58
+
57
59
  records.each do |record|
58
60
  dest_col.insert record
59
61
  end
60
62
  end
61
-
63
+
62
64
  # Load indexes here
63
65
  col_name = "system.indexes"
64
66
  dest_index_col = @db.collection(col_name)
@@ -69,65 +71,64 @@ module Heroku::Mongo
69
71
  end
70
72
  end
71
73
  end
72
-
74
+
73
75
  def connect
74
76
  uri = URI.parse(@url)
75
77
  connection = ::Mongo::Connection.new(uri.host, uri.port)
76
78
  @db = connection.db(uri.path.gsub(/^\//, ''))
77
79
  @db.authenticate(uri.user, uri.password) if uri.user
78
80
  end
79
-
81
+
80
82
  def s3_connect
81
- bucket = YAML.load_file("config/assets.yml")['s3_bucket']
82
- access_key_id = ENV['S3_KEY']
83
- secret_access_key = ENV['S3_SECRET']
83
+ bucket = ENV['S3_BUCKET']
84
+ access_key_id = ENV['S3_KEY_ID']
85
+ secret_access_key = ENV['S3_SECRET_KEY']
84
86
 
85
87
  service = S3::Service.new(:access_key_id => access_key_id,
86
88
  :secret_access_key => secret_access_key)
87
89
  @bucket = service.buckets.find(bucket)
88
90
  end
89
-
91
+
90
92
  def s3_upload
91
93
  object = @bucket.objects.build("backups/#{@file_name}")
92
94
  object.content = open(@file_name)
93
95
  object.save
94
96
  end
95
-
97
+
96
98
  def s3_download
97
99
  open(@file_name, 'w') do |file|
98
100
  object = @bucket.objects.find("backups/#{@file_name}")
99
101
  file.write object.content
100
102
  end
101
103
  end
102
-
104
+
103
105
  def initialize
104
- # Backup settings
105
106
  @file_name = Time.now.strftime("%Y-%m-%d_%H-%M-%S.gz")
106
-
107
- # MongoDB config
108
- local_db_name = YAML.load_file("config/mongoid.yml")['development']['database']
109
-
110
- development_uri = "mongodb://localhost:27017/#{local_db_name}"
111
- production_uri = YAML.load_file("config/mongoid.yml")['production']['uri']
112
-
113
- #@url = development_uri
114
- @url = production_uri
115
-
107
+
108
+ if ENV[:RAILS_ENV] == 'production'
109
+ uri = YAML.load_file("config/mongoid.yml")['production']['uri']
110
+ else
111
+ config = YAML.load_file("config/mongoid.yml")['development']
112
+ uri = "mongodb://#{config['host']}:#{config['port']}/#{config['database']}"
113
+ end
114
+
115
+ @url = uri
116
+
116
117
  puts "Using databased: #{@url}"
117
-
118
+
118
119
  self.connect
119
120
  self.s3_connect
120
121
  end
121
-
122
+
122
123
  def backup
123
124
  self.chdir
124
125
  self.store
125
126
  self.s3_upload
126
127
  end
127
-
128
+
128
129
  def restore file_name
129
130
  @file_name = file_name
130
-
131
+
131
132
  self.chdir
132
133
  self.s3_download
133
134
  self.load
@@ -0,0 +1,12 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'heroku_mongo_backup'
4
+ require 'rails'
5
+
6
+ module HerokuMongoBackup
7
+ class Railtie < Rails::Railtie
8
+ rake_tasks do
9
+ load "tasks/heroku_mongo_backup.rake"
10
+ end
11
+ end
12
+ end
@@ -1,15 +1,17 @@
1
- # coding: UTF-8
1
+ # encoding: UTF-8
2
2
 
3
3
  namespace :mongo do
4
- desc "Mongo backup and restore commands.\n\tBackup: rake mongo:backup\n\tRestore: rake mongo:restore FILE=<backup-file.gz>"
5
-
6
- task :backup => [:environment] do
7
- Heroku::Mongo::Backup.new.backup
4
+ desc "Backup prodution database and store it on S3.\n
5
+ Example of usage: rake mongo:backup"
6
+ task :backup => :environment do
7
+ HerokuMongoBackup::Backup.new.backup
8
8
  end
9
9
 
10
- task :restore => [:environment] do
10
+ desc "Restore command gets backup file from S3 server and pushes data to production db.\n
11
+ Example of usage: rake mongo:restore FILE=<backup-file.gz>"
12
+ task :restore => :environment do
11
13
  if ENV['FILE']
12
- Heroku::Mongo::Backup.new.restore ENV['FILE']
14
+ HerokuMongoBackup::Backup.new.restore ENV['FILE']
13
15
  else
14
16
  puts "Please provide backup file to restore from. Thanks!"
15
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: heroku-mongo-backup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-09-13 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70318711197760 !ruby/object:Gem::Requirement
16
+ requirement: &70358971982280 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70318711197760
24
+ version_requirements: *70358971982280
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mocha
27
- requirement: &70318711197280 !ruby/object:Gem::Requirement
27
+ requirement: &70358971981540 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,13 +32,14 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70318711197280
35
+ version_requirements: *70358971981540
36
36
  description: Rake task for backing up mongo database on heroku and push it to S3.
37
37
  email: mail@alexkravets.com
38
38
  executables: []
39
39
  extensions: []
40
40
  extra_rdoc_files: []
41
41
  files:
42
+ - lib/heroku_mongo_backup/railtie.rb
42
43
  - lib/heroku_mongo_backup.rb
43
44
  - lib/tasks/heroku_mongo_backup.rake
44
45
  homepage: https://github.com/alexkravets/heroku-mongo-backup