heroku-mongo-backup 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +11 -0
- data/heroku-mongo-backup.gemspec +1 -1
- data/lib/heroku-mongo-backup.rb +24 -13
- data/lib/s3_helpers.rb +32 -5
- data/lib/tasks/heroku_mongo_backup.rake +19 -4
- metadata +2 -2
data/README.md
CHANGED
@@ -40,8 +40,19 @@ For FTP set these variables:
|
|
40
40
|
## Rake Commands
|
41
41
|
|
42
42
|
* ```heroku run rake mongo:backup```
|
43
|
+
|
44
|
+
If you want to automatically remove old backup files pass ```MAX_BACKUPS``` parameter to the rake command:
|
45
|
+
|
46
|
+
* ```heroku run rake mongo:backup MAX_BACKUPS=7```
|
47
|
+
|
48
|
+
Restore from backup:
|
49
|
+
|
43
50
|
* ```heroku run rake mongo:restore FILE=backup-file-name.gz```
|
44
51
|
|
52
|
+
If you want to restore from local file run:
|
53
|
+
|
54
|
+
* ```rake mongo:restore LOCAL=/absolute/path/to/<backup-file.gz>```
|
55
|
+
|
45
56
|
For Rails 2 add this to your Rakefile to import rake tasks:
|
46
57
|
|
47
58
|
```import File.expand_path(File.join(Gem.datadir('heroku-mongo-backup'), '..', '..', 'lib', 'tasks', 'heroku_mongo_backup.rake'))```
|
data/heroku-mongo-backup.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'heroku-mongo-backup'
|
3
|
-
s.version = '0.4.
|
3
|
+
s.version = '0.4.2'
|
4
4
|
s.summary = 'Rake task backups mongo database on Heroku and push gzipped file to Amazon S3 or FTP.'
|
5
5
|
s.description = 'Rake task for backing up mongo database on heroku and push it to S3 or FTP. Library can be used as rake task or be easily integrated into daily cron job.'
|
6
6
|
|
data/lib/heroku-mongo-backup.rb
CHANGED
@@ -149,10 +149,11 @@ module HerokuMongoBackup
|
|
149
149
|
end
|
150
150
|
end
|
151
151
|
|
152
|
-
def initialize
|
152
|
+
def initialize connect = true
|
153
153
|
@file_name = Time.now.strftime("%Y-%m-%d_%H-%M-%S.gz")
|
154
154
|
|
155
|
-
if((ENV['RAILS_ENV'] || ENV['RACK_ENV'])
|
155
|
+
if( ['production', 'staging'].include?(ENV['RAILS_ENV'] || ENV['RACK_ENV']) )
|
156
|
+
|
156
157
|
#config_template = ERB.new(IO.read("config/mongoid.yml"))
|
157
158
|
#uri = YAML.load(config_template.result)['production']['uri']
|
158
159
|
uri = ENV['MONGO_URL']
|
@@ -163,6 +164,7 @@ module HerokuMongoBackup
|
|
163
164
|
if uri.nil?
|
164
165
|
uri = ENV['MONGOLAB_URI']
|
165
166
|
end
|
167
|
+
|
166
168
|
else
|
167
169
|
mongoid_config = YAML.load_file("config/mongoid.yml")
|
168
170
|
config = {}
|
@@ -192,14 +194,16 @@ module HerokuMongoBackup
|
|
192
194
|
|
193
195
|
self.db_connect
|
194
196
|
|
195
|
-
if
|
196
|
-
|
197
|
-
|
198
|
-
|
197
|
+
if connect
|
198
|
+
if ENV['UPLOAD_TYPE'] == 'ftp'
|
199
|
+
self.ftp_connect
|
200
|
+
else
|
201
|
+
self.s3_connect
|
202
|
+
end
|
199
203
|
end
|
200
204
|
end
|
201
205
|
|
202
|
-
def backup
|
206
|
+
def backup files_number_to_leave=0
|
203
207
|
self.chdir
|
204
208
|
self.store
|
205
209
|
|
@@ -209,19 +213,26 @@ module HerokuMongoBackup
|
|
209
213
|
else
|
210
214
|
self.s3_upload
|
211
215
|
end
|
216
|
+
|
217
|
+
if files_number_to_leave > 0
|
218
|
+
HerokuMongoBackup::remove_old_backup_files(@bucket, files_number_to_leave)
|
219
|
+
end
|
212
220
|
end
|
213
221
|
|
214
|
-
def restore file_name
|
222
|
+
def restore file_name, download_file = true
|
215
223
|
@file_name = file_name
|
216
224
|
|
217
225
|
self.chdir
|
218
226
|
|
219
|
-
if
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
227
|
+
if download_file
|
228
|
+
if ENV['UPLOAD_TYPE'] == 'ftp'
|
229
|
+
self.ftp_download
|
230
|
+
@ftp.close
|
231
|
+
else
|
232
|
+
self.s3_download
|
233
|
+
end
|
224
234
|
end
|
235
|
+
|
225
236
|
self.load
|
226
237
|
end
|
227
238
|
end
|
data/lib/s3_helpers.rb
CHANGED
@@ -38,6 +38,10 @@ if defined?(S3)
|
|
38
38
|
|
39
39
|
return content
|
40
40
|
end
|
41
|
+
|
42
|
+
def HerokuMongoBackup::remove_old_backup_files(files_number_to_leave)
|
43
|
+
end
|
44
|
+
|
41
45
|
end
|
42
46
|
|
43
47
|
|
@@ -59,7 +63,6 @@ if defined?(AWS)
|
|
59
63
|
def HerokuMongoBackup::s3_connect(bucket, key, secret)
|
60
64
|
AWS::S3::Base.establish_connection!(:access_key_id => key,
|
61
65
|
:secret_access_key => secret)
|
62
|
-
# This is probably doesn't work
|
63
66
|
return bucket
|
64
67
|
end
|
65
68
|
|
@@ -71,6 +74,22 @@ if defined?(AWS)
|
|
71
74
|
content = AWS::S3::S3Object.value("backups/#{filename}", bucket)
|
72
75
|
return content
|
73
76
|
end
|
77
|
+
|
78
|
+
def HerokuMongoBackup::remove_old_backup_files(bucket_name, files_number_to_leave)
|
79
|
+
bucket = Bucket.find(bucket_name)
|
80
|
+
|
81
|
+
object_keys = []
|
82
|
+
bucket.objects.each { |o| object_keys << o.key }
|
83
|
+
|
84
|
+
object_keys = object_keys.sort
|
85
|
+
|
86
|
+
excess = object_keys.count - files_number_to_leave
|
87
|
+
|
88
|
+
if excess > 0
|
89
|
+
(0..excess-1).each { |i| S3Object.find(object_keys[i], bucket_name).delete }
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
74
93
|
end
|
75
94
|
|
76
95
|
|
@@ -113,10 +132,18 @@ if defined?(Fog)
|
|
113
132
|
return file.body
|
114
133
|
end
|
115
134
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
135
|
+
def HerokuMongoBackup::remove_old_backup_files(directory, files_number_to_leave)
|
136
|
+
total_backups = directory.files.all.size
|
137
|
+
|
138
|
+
if total_backups > files_number_to_leave
|
139
|
+
|
140
|
+
files_to_destroy = (0..total_backups-files_number_to_leave-1).collect{|i| directory.files.all[i] }
|
141
|
+
|
142
|
+
files_to_destroy.each do |f|
|
143
|
+
f.destroy
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
120
147
|
|
121
148
|
else
|
122
149
|
logging = Logger.new(STDOUT)
|
@@ -2,18 +2,33 @@
|
|
2
2
|
|
3
3
|
namespace :mongo do
|
4
4
|
desc "Backup prodution database and store it on S3.\n
|
5
|
-
Example of usage: rake mongo:backup"
|
5
|
+
Example of usage: rake mongo:backup OR rake mongo:backup MAX_BACKUPS=7"
|
6
6
|
task :backup => :environment do
|
7
|
-
|
7
|
+
if ENV['MAX_BACKUPS']
|
8
|
+
HerokuMongoBackup::Backup.new.backup(ENV['MAX_BACKUPS'].to_i)
|
9
|
+
else
|
10
|
+
HerokuMongoBackup::Backup.new.backup
|
11
|
+
end
|
8
12
|
end
|
9
13
|
|
10
|
-
desc "Restore command gets backup file from S3 server and pushes data to production db.\n
|
14
|
+
desc "Restore command gets backup file from S3 server or local file and pushes data to production db.\n
|
11
15
|
Example of usage: rake mongo:restore FILE=<backup-file.gz>"
|
12
16
|
task :restore => :environment do
|
13
17
|
if ENV['FILE']
|
14
18
|
HerokuMongoBackup::Backup.new.restore ENV['FILE']
|
19
|
+
elsif ENV['LOCAL']
|
20
|
+
HerokuMongoBackup::Backup.new(false).restore ENV['LOCAL'], false
|
15
21
|
else
|
16
|
-
puts "
|
22
|
+
puts "\n* --------------------------------------------------------------- *\n" +
|
23
|
+
"| Provide backup file to restore from: |\n" +
|
24
|
+
"| |\n" +
|
25
|
+
"| rake mongo:restore FILE=<backup-file.gz> |\n" +
|
26
|
+
"| |\n" +
|
27
|
+
"| If backup file is already downloaded: |\n" +
|
28
|
+
"| |\n" +
|
29
|
+
"| rake mongo:restore LOCAL=/absolute/path/to/<backup-file.gz> |\n" +
|
30
|
+
"| |\n" +
|
31
|
+
"* --------------------------------------------------------------- *\n\n"
|
17
32
|
end
|
18
33
|
end
|
19
34
|
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.4.
|
4
|
+
version: 0.4.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ authors:
|
|
13
13
|
autorequire:
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
|
-
date: 2012-10-
|
16
|
+
date: 2012-10-03 00:00:00.000000000 Z
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|
19
19
|
name: mongo
|