interu-backup 3.0.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (151) hide show
  1. data/.gitignore +2 -0
  2. data/Gemfile +31 -0
  3. data/Gemfile.lock +117 -0
  4. data/Guardfile +17 -0
  5. data/LICENSE.md +24 -0
  6. data/README.md +332 -0
  7. data/backup.gemspec +31 -0
  8. data/bin/backup +267 -0
  9. data/lib/backup.rb +181 -0
  10. data/lib/backup/archive.rb +73 -0
  11. data/lib/backup/cli.rb +82 -0
  12. data/lib/backup/compressor/base.rb +17 -0
  13. data/lib/backup/compressor/bzip2.rb +64 -0
  14. data/lib/backup/compressor/gzip.rb +61 -0
  15. data/lib/backup/configuration/base.rb +15 -0
  16. data/lib/backup/configuration/compressor/base.rb +10 -0
  17. data/lib/backup/configuration/compressor/bzip2.rb +23 -0
  18. data/lib/backup/configuration/compressor/gzip.rb +23 -0
  19. data/lib/backup/configuration/database/base.rb +18 -0
  20. data/lib/backup/configuration/database/mongodb.rb +41 -0
  21. data/lib/backup/configuration/database/mysql.rb +37 -0
  22. data/lib/backup/configuration/database/postgresql.rb +37 -0
  23. data/lib/backup/configuration/database/redis.rb +35 -0
  24. data/lib/backup/configuration/encryptor/base.rb +10 -0
  25. data/lib/backup/configuration/encryptor/gpg.rb +17 -0
  26. data/lib/backup/configuration/encryptor/open_ssl.rb +26 -0
  27. data/lib/backup/configuration/helpers.rb +54 -0
  28. data/lib/backup/configuration/notifier/base.rb +39 -0
  29. data/lib/backup/configuration/notifier/campfire.rb +25 -0
  30. data/lib/backup/configuration/notifier/mail.rb +52 -0
  31. data/lib/backup/configuration/notifier/presently.rb +25 -0
  32. data/lib/backup/configuration/notifier/twitter.rb +21 -0
  33. data/lib/backup/configuration/storage/base.rb +18 -0
  34. data/lib/backup/configuration/storage/cloudfiles.rb +21 -0
  35. data/lib/backup/configuration/storage/dropbox.rb +29 -0
  36. data/lib/backup/configuration/storage/ftp.rb +25 -0
  37. data/lib/backup/configuration/storage/rsync.rb +25 -0
  38. data/lib/backup/configuration/storage/s3.rb +25 -0
  39. data/lib/backup/configuration/storage/scp.rb +25 -0
  40. data/lib/backup/configuration/storage/sftp.rb +25 -0
  41. data/lib/backup/configuration/syncer/rsync.rb +45 -0
  42. data/lib/backup/configuration/syncer/s3.rb +33 -0
  43. data/lib/backup/database/base.rb +33 -0
  44. data/lib/backup/database/mongodb.rb +179 -0
  45. data/lib/backup/database/mysql.rb +104 -0
  46. data/lib/backup/database/postgresql.rb +111 -0
  47. data/lib/backup/database/redis.rb +105 -0
  48. data/lib/backup/dependency.rb +96 -0
  49. data/lib/backup/encryptor/base.rb +17 -0
  50. data/lib/backup/encryptor/gpg.rb +78 -0
  51. data/lib/backup/encryptor/open_ssl.rb +67 -0
  52. data/lib/backup/exception/command_not_found.rb +8 -0
  53. data/lib/backup/finder.rb +39 -0
  54. data/lib/backup/logger.rb +102 -0
  55. data/lib/backup/model.rb +272 -0
  56. data/lib/backup/notifier/base.rb +29 -0
  57. data/lib/backup/notifier/binder.rb +32 -0
  58. data/lib/backup/notifier/campfire.rb +194 -0
  59. data/lib/backup/notifier/mail.rb +141 -0
  60. data/lib/backup/notifier/presently.rb +105 -0
  61. data/lib/backup/notifier/templates/notify_failure.erb +33 -0
  62. data/lib/backup/notifier/templates/notify_success.erb +16 -0
  63. data/lib/backup/notifier/twitter.rb +87 -0
  64. data/lib/backup/storage/base.rb +67 -0
  65. data/lib/backup/storage/cloudfiles.rb +95 -0
  66. data/lib/backup/storage/dropbox.rb +91 -0
  67. data/lib/backup/storage/ftp.rb +114 -0
  68. data/lib/backup/storage/object.rb +45 -0
  69. data/lib/backup/storage/rsync.rb +129 -0
  70. data/lib/backup/storage/s3.rb +180 -0
  71. data/lib/backup/storage/scp.rb +106 -0
  72. data/lib/backup/storage/sftp.rb +106 -0
  73. data/lib/backup/syncer/base.rb +10 -0
  74. data/lib/backup/syncer/rsync.rb +152 -0
  75. data/lib/backup/syncer/s3.rb +118 -0
  76. data/lib/backup/version.rb +43 -0
  77. data/lib/templates/archive +7 -0
  78. data/lib/templates/compressor/bzip2 +7 -0
  79. data/lib/templates/compressor/gzip +7 -0
  80. data/lib/templates/database/mongodb +14 -0
  81. data/lib/templates/database/mysql +14 -0
  82. data/lib/templates/database/postgresql +14 -0
  83. data/lib/templates/database/redis +13 -0
  84. data/lib/templates/encryptor/gpg +12 -0
  85. data/lib/templates/encryptor/openssl +8 -0
  86. data/lib/templates/notifier/campfire +11 -0
  87. data/lib/templates/notifier/mail +17 -0
  88. data/lib/templates/notifier/presently +12 -0
  89. data/lib/templates/notifier/twitter +12 -0
  90. data/lib/templates/readme +15 -0
  91. data/lib/templates/storage/cloudfiles +10 -0
  92. data/lib/templates/storage/dropbox +12 -0
  93. data/lib/templates/storage/ftp +11 -0
  94. data/lib/templates/storage/rsync +10 -0
  95. data/lib/templates/storage/s3 +21 -0
  96. data/lib/templates/storage/scp +11 -0
  97. data/lib/templates/storage/sftp +11 -0
  98. data/lib/templates/syncer/rsync +17 -0
  99. data/lib/templates/syncer/s3 +15 -0
  100. data/spec/archive_spec.rb +90 -0
  101. data/spec/backup_spec.rb +11 -0
  102. data/spec/compressor/bzip2_spec.rb +59 -0
  103. data/spec/compressor/gzip_spec.rb +59 -0
  104. data/spec/configuration/base_spec.rb +35 -0
  105. data/spec/configuration/compressor/gzip_spec.rb +28 -0
  106. data/spec/configuration/database/base_spec.rb +16 -0
  107. data/spec/configuration/database/mongodb_spec.rb +30 -0
  108. data/spec/configuration/database/mysql_spec.rb +32 -0
  109. data/spec/configuration/database/postgresql_spec.rb +32 -0
  110. data/spec/configuration/database/redis_spec.rb +30 -0
  111. data/spec/configuration/encryptor/gpg_spec.rb +25 -0
  112. data/spec/configuration/encryptor/open_ssl_spec.rb +31 -0
  113. data/spec/configuration/notifier/campfire_spec.rb +20 -0
  114. data/spec/configuration/notifier/mail_spec.rb +32 -0
  115. data/spec/configuration/notifier/twitter_spec.rb +22 -0
  116. data/spec/configuration/storage/cloudfiles_spec.rb +34 -0
  117. data/spec/configuration/storage/dropbox_spec.rb +43 -0
  118. data/spec/configuration/storage/ftp_spec.rb +40 -0
  119. data/spec/configuration/storage/rsync_spec.rb +37 -0
  120. data/spec/configuration/storage/s3_spec.rb +37 -0
  121. data/spec/configuration/storage/scp_spec.rb +40 -0
  122. data/spec/configuration/storage/sftp_spec.rb +40 -0
  123. data/spec/configuration/syncer/rsync_spec.rb +46 -0
  124. data/spec/configuration/syncer/s3_spec.rb +43 -0
  125. data/spec/database/base_spec.rb +30 -0
  126. data/spec/database/mongodb_spec.rb +181 -0
  127. data/spec/database/mysql_spec.rb +150 -0
  128. data/spec/database/postgresql_spec.rb +164 -0
  129. data/spec/database/redis_spec.rb +122 -0
  130. data/spec/encryptor/gpg_spec.rb +57 -0
  131. data/spec/encryptor/open_ssl_spec.rb +102 -0
  132. data/spec/logger_spec.rb +58 -0
  133. data/spec/model_spec.rb +236 -0
  134. data/spec/notifier/campfire_spec.rb +96 -0
  135. data/spec/notifier/mail_spec.rb +97 -0
  136. data/spec/notifier/presently_spec.rb +99 -0
  137. data/spec/notifier/twitter_spec.rb +86 -0
  138. data/spec/spec_helper.rb +25 -0
  139. data/spec/storage/base_spec.rb +33 -0
  140. data/spec/storage/cloudfiles_spec.rb +102 -0
  141. data/spec/storage/dropbox_spec.rb +105 -0
  142. data/spec/storage/ftp_spec.rb +133 -0
  143. data/spec/storage/object_spec.rb +74 -0
  144. data/spec/storage/rsync_spec.rb +131 -0
  145. data/spec/storage/s3_spec.rb +110 -0
  146. data/spec/storage/scp_spec.rb +129 -0
  147. data/spec/storage/sftp_spec.rb +125 -0
  148. data/spec/syncer/rsync_spec.rb +195 -0
  149. data/spec/syncer/s3_spec.rb +139 -0
  150. data/spec/version_spec.rb +21 -0
  151. metadata +231 -0
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../../spec_helper'
4
+
5
+ describe Backup::Configuration::Storage::Dropbox do
6
+ before do
7
+ Backup::Configuration::Storage::Dropbox.defaults do |db|
8
+ db.email = 'my@email.com'
9
+ db.password = 'my_password'
10
+ db.api_key = 'my_api_key'
11
+ db.api_secret = 'my_secret'
12
+ db.path = 'my_backups'
13
+ db.keep = 20
14
+ db.timeout = 500
15
+ end
16
+ end
17
+
18
+ it 'should set the default Dropbox configuration' do
19
+ db = Backup::Configuration::Storage::Dropbox
20
+ db.email.should == 'my@email.com'
21
+ db.password.should == 'my_password'
22
+ db.api_key.should == 'my_api_key'
23
+ db.api_secret.should == 'my_secret'
24
+ db.path.should == 'my_backups'
25
+ db.keep.should == 20
26
+ db.timeout.should == 500
27
+ end
28
+
29
+ describe '#clear_defaults!' do
30
+ it 'should clear all the defaults, resetting them to nil' do
31
+ Backup::Configuration::Storage::Dropbox.clear_defaults!
32
+
33
+ db = Backup::Configuration::Storage::Dropbox
34
+ db.email.should == nil
35
+ db.password.should == nil
36
+ db.api_key.should == nil
37
+ db.api_secret.should == nil
38
+ db.path.should == nil
39
+ db.keep.should == nil
40
+ db.timeout.should == nil
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,40 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../../spec_helper'
4
+
5
+ describe Backup::Configuration::Storage::FTP do
6
+ before do
7
+ Backup::Configuration::Storage::FTP.defaults do |ftp|
8
+ ftp.username = 'my_username'
9
+ ftp.password = 'my_password'
10
+ ftp.ip = '123.45.678.90'
11
+ ftp.port = 21
12
+ ftp.path = 'my_backups'
13
+ ftp.keep = 20
14
+ end
15
+ end
16
+
17
+ it 'should set the default ftp configuration' do
18
+ ftp = Backup::Configuration::Storage::FTP
19
+ ftp.username.should == 'my_username'
20
+ ftp.password.should == 'my_password'
21
+ ftp.ip.should == '123.45.678.90'
22
+ ftp.port.should == 21
23
+ ftp.path.should == 'my_backups'
24
+ ftp.keep.should == 20
25
+ end
26
+
27
+ describe '#clear_defaults!' do
28
+ it 'should clear all the defaults, resetting them to nil' do
29
+ Backup::Configuration::Storage::FTP.clear_defaults!
30
+
31
+ ftp = Backup::Configuration::Storage::FTP
32
+ ftp.username.should == nil
33
+ ftp.password.should == nil
34
+ ftp.ip.should == nil
35
+ ftp.port.should == nil
36
+ ftp.path.should == nil
37
+ ftp.keep.should == nil
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../../spec_helper'
4
+
5
+ describe Backup::Configuration::Storage::RSync do
6
+ before do
7
+ Backup::Configuration::Storage::RSync.defaults do |rsync|
8
+ rsync.username = 'my_username'
9
+ rsync.password = 'my_password'
10
+ rsync.ip = '123.45.678.90'
11
+ rsync.port = 21
12
+ rsync.path = 'my_backups'
13
+ end
14
+ end
15
+
16
+ it 'should set the default rsync configuration' do
17
+ rsync = Backup::Configuration::Storage::RSync
18
+ rsync.username.should == 'my_username'
19
+ rsync.password.should == 'my_password'
20
+ rsync.ip.should == '123.45.678.90'
21
+ rsync.port.should == 21
22
+ rsync.path.should == 'my_backups'
23
+ end
24
+
25
+ describe '#clear_defaults!' do
26
+ it 'should clear all the defaults, resetting them to nil' do
27
+ Backup::Configuration::Storage::RSync.clear_defaults!
28
+
29
+ rsync = Backup::Configuration::Storage::RSync
30
+ rsync.username.should == nil
31
+ rsync.password.should == nil
32
+ rsync.ip.should == nil
33
+ rsync.port.should == nil
34
+ rsync.path.should == nil
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../../spec_helper'
4
+
5
+ describe Backup::Configuration::Storage::S3 do
6
+ before do
7
+ Backup::Configuration::Storage::S3.defaults do |s3|
8
+ s3.access_key_id = 'my_access_key_id'
9
+ s3.secret_access_key = 'my_secret_access_key'
10
+ s3.region = 'us-east-1'
11
+ s3.bucket = 'my-bucket'
12
+ s3.path = 'my_backups'
13
+ end
14
+ end
15
+
16
+ it 'should set the default S3 configuration' do
17
+ s3 = Backup::Configuration::Storage::S3
18
+ s3.access_key_id.should == 'my_access_key_id'
19
+ s3.secret_access_key.should == 'my_secret_access_key'
20
+ s3.region.should == 'us-east-1'
21
+ s3.bucket.should == 'my-bucket'
22
+ s3.path.should == 'my_backups'
23
+ end
24
+
25
+ describe '#clear_defaults!' do
26
+ it 'should clear all the defaults, resetting them to nil' do
27
+ Backup::Configuration::Storage::S3.clear_defaults!
28
+
29
+ s3 = Backup::Configuration::Storage::S3
30
+ s3.access_key_id.should == nil
31
+ s3.secret_access_key.should == nil
32
+ s3.region.should == nil
33
+ s3.bucket.should == nil
34
+ s3.path.should == nil
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,40 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../../spec_helper'
4
+
5
+ describe Backup::Configuration::Storage::SCP do
6
+ before do
7
+ Backup::Configuration::Storage::SCP.defaults do |scp|
8
+ scp.username = 'my_username'
9
+ scp.password = 'my_password'
10
+ scp.ip = '123.45.678.90'
11
+ scp.port = 21
12
+ scp.path = 'my_backups'
13
+ scp.keep = 20
14
+ end
15
+ end
16
+
17
+ it 'should set the default scp configuration' do
18
+ scp = Backup::Configuration::Storage::SCP
19
+ scp.username.should == 'my_username'
20
+ scp.password.should == 'my_password'
21
+ scp.ip.should == '123.45.678.90'
22
+ scp.port.should == 21
23
+ scp.path.should == 'my_backups'
24
+ scp.keep.should == 20
25
+ end
26
+
27
+ describe '#clear_defaults!' do
28
+ it 'should clear all the defaults, resetting them to nil' do
29
+ Backup::Configuration::Storage::SCP.clear_defaults!
30
+
31
+ scp = Backup::Configuration::Storage::SCP
32
+ scp.username.should == nil
33
+ scp.password.should == nil
34
+ scp.ip.should == nil
35
+ scp.port.should == nil
36
+ scp.path.should == nil
37
+ scp.keep.should == nil
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../../spec_helper'
4
+
5
+ describe Backup::Configuration::Storage::SFTP do
6
+ before do
7
+ Backup::Configuration::Storage::SFTP.defaults do |sftp|
8
+ sftp.username = 'my_username'
9
+ sftp.password = 'my_password'
10
+ sftp.ip = '123.45.678.90'
11
+ sftp.port = 22
12
+ sftp.path = 'my_backups'
13
+ sftp.keep = 20
14
+ end
15
+ end
16
+
17
+ it 'should set the default sftp configuration' do
18
+ sftp = Backup::Configuration::Storage::SFTP
19
+ sftp.username.should == 'my_username'
20
+ sftp.password.should == 'my_password'
21
+ sftp.ip.should == '123.45.678.90'
22
+ sftp.port.should == 22
23
+ sftp.path.should == 'my_backups'
24
+ sftp.keep.should == 20
25
+ end
26
+
27
+ describe '#clear_defaults!' do
28
+ it 'should clear all the defaults, resetting them to nil' do
29
+ Backup::Configuration::Storage::SFTP.clear_defaults!
30
+
31
+ sftp = Backup::Configuration::Storage::SFTP
32
+ sftp.username.should == nil
33
+ sftp.password.should == nil
34
+ sftp.ip.should == nil
35
+ sftp.port.should == nil
36
+ sftp.path.should == nil
37
+ sftp.keep.should == nil
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../../spec_helper'
4
+
5
+ describe Backup::Configuration::Syncer::RSync do
6
+ before do
7
+ Backup::Configuration::Syncer::RSync.defaults do |rsync|
8
+ rsync.username = 'my_username'
9
+ rsync.password = 'my_password'
10
+ rsync.ip = '123.45.678.90'
11
+ rsync.port = 22
12
+ rsync.path = '~/backups/'
13
+ rsync.mirror = true
14
+ rsync.compress = true
15
+ rsync.additional_options = []
16
+ end
17
+ end
18
+
19
+ it 'should set the default rsync configuration' do
20
+ rsync = Backup::Configuration::Syncer::RSync
21
+ rsync.username.should == 'my_username'
22
+ rsync.password.should == 'my_password'
23
+ rsync.ip.should == '123.45.678.90'
24
+ rsync.port.should == 22
25
+ rsync.path.should == '~/backups/'
26
+ rsync.mirror.should == true
27
+ rsync.compress.should == true
28
+ rsync.additional_options.should == []
29
+ end
30
+
31
+ describe '#clear_defaults!' do
32
+ it 'should clear all the defaults, resetting them to nil' do
33
+ Backup::Configuration::Syncer::RSync.clear_defaults!
34
+
35
+ rsync = Backup::Configuration::Syncer::RSync
36
+ rsync.username.should == nil
37
+ rsync.password.should == nil
38
+ rsync.ip.should == nil
39
+ rsync.port.should == nil
40
+ rsync.path.should == nil
41
+ rsync.mirror.should == nil
42
+ rsync.compress.should == nil
43
+ rsync.additional_options.should == nil
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../../spec_helper'
4
+
5
+ describe Backup::Configuration::Syncer::S3 do
6
+ before do
7
+ Backup::Configuration::Syncer::S3.defaults do |s3|
8
+ s3.access_key_id = 'my_access_key_id'
9
+ s3.secret_access_key = 'my_secret_access_key'
10
+ s3.bucket = 'my-bucket'
11
+ s3.path = '/backups/'
12
+ s3.directories = '/directories/to/backup/'
13
+ s3.mirror = true
14
+ s3.additional_options = ['--exclude="*.rb"']
15
+ end
16
+ end
17
+
18
+ it 'should set the default s3 configuration' do
19
+ s3 = Backup::Configuration::Syncer::S3
20
+ s3.access_key_id.should == 'my_access_key_id'
21
+ s3.secret_access_key.should == 'my_secret_access_key'
22
+ s3.bucket.should == 'my-bucket'
23
+ s3.path.should == '/backups/'
24
+ s3.directories.should == '/directories/to/backup/'
25
+ s3.mirror.should == true
26
+ s3.additional_options.should == ['--exclude="*.rb"']
27
+ end
28
+
29
+ describe '#clear_defaults!' do
30
+ it 'should clear all the defaults, resetting them to nil' do
31
+ Backup::Configuration::Syncer::S3.clear_defaults!
32
+
33
+ s3 = Backup::Configuration::Syncer::S3
34
+ s3.access_key_id.should == nil
35
+ s3.secret_access_key.should == nil
36
+ s3.bucket.should == nil
37
+ s3.path.should == nil
38
+ s3.directories.should == nil
39
+ s3.mirror.should == nil
40
+ s3.additional_options.should == nil
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe Backup::Database::Base do
6
+
7
+ before do
8
+ class Backup::Database::Base
9
+ def initialize(&block)
10
+ instance_eval(&block) if block_given?
11
+ end
12
+ end
13
+ end
14
+
15
+ let(:db) do
16
+ Backup::Database::Base.new do |db|
17
+ db.utility_path = '/var/usr/my_util'
18
+ end
19
+ end
20
+
21
+ it 'should return the utility path instead of auto-detecting it' do
22
+ db.utility(:my_database_util).should == '/var/usr/my_util'
23
+ end
24
+
25
+ it 'should ignore the utility_path when not defined' do
26
+ db = Backup::Database::Base.new
27
+ db.utility(:my_database_util).should == :my_database_util
28
+ end
29
+
30
+ end
@@ -0,0 +1,181 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe Backup::Database::MongoDB do
6
+
7
+ before do
8
+ Backup::Database::MongoDB.any_instance.stubs(:load_defaults!)
9
+ end
10
+
11
+ let(:db) do
12
+ Backup::Database::MongoDB.new do |db|
13
+ db.name = 'mydatabase'
14
+ db.username = 'someuser'
15
+ db.password = 'secret'
16
+ db.host = 'localhost'
17
+ db.port = 123
18
+
19
+ db.ipv6 = true
20
+ db.only_collections = ['users', 'pirates']
21
+ db.additional_options = ['--query']
22
+ end
23
+ end
24
+
25
+ describe '#new' do
26
+ it 'should read the adapter details correctly' do
27
+ db.name.should == 'mydatabase'
28
+ db.username.should == 'someuser'
29
+ db.password.should == 'secret'
30
+ db.host.should == 'localhost'
31
+ db.port.should == 123
32
+
33
+ db.only_collections.should == ['users', 'pirates']
34
+ db.additional_options.should == '--query'
35
+ end
36
+
37
+ it 'arrays should default to empty arrays when not specified' do
38
+ db = Backup::Database::MongoDB.new do |db|
39
+ db.name = 'mydatabase'
40
+ db.username = 'someuser'
41
+ db.password = 'secret'
42
+ end
43
+
44
+ db.only_collections.should == []
45
+ db.additional_options.should == ""
46
+ end
47
+
48
+ it 'should ensure the directory is available' do
49
+ Backup::Database::MongoDB.any_instance.expects(:mkdir).with("#{Backup::TMP_PATH}/myapp/MongoDB")
50
+ Backup::Database::MongoDB.new {}
51
+ end
52
+ end
53
+
54
+ describe '#only_collections' do
55
+ it 'should return a string for the mongodump selected table to dump option' do
56
+ db.collections_to_dump.should == %w[users pirates]
57
+ end
58
+ end
59
+
60
+ describe '#credential_options' do
61
+ it 'should return the mongo syntax for the credential options' do
62
+ db.credential_options.should == "--username='someuser' --password='secret'"
63
+ end
64
+
65
+ it 'should only return the mongo syntax for the user' do
66
+ db = Backup::Database::MongoDB.new do |db|
67
+ db.username = 'someuser'
68
+ end
69
+
70
+ db.credential_options.should == "--username='someuser'"
71
+ end
72
+ end
73
+
74
+ describe '#connectivity_options' do
75
+ it 'should return the mongo syntax for the connectivity options' do
76
+ db.connectivity_options.should == "--host='localhost' --port='123'"
77
+ end
78
+
79
+ it 'should return only the socket' do
80
+ db = Backup::Database::MongoDB.new do |db|
81
+ db.host = ''
82
+ db.port = 123
83
+ end
84
+
85
+ db.connectivity_options.should == "--port='123'"
86
+ end
87
+ end
88
+
89
+ describe '#ipv6' do
90
+ it 'should return a mongodb syntax compatible ipv6 flag' do
91
+ db.ipv6 = true
92
+ db.ipv6.should == '--ipv6'
93
+ end
94
+
95
+ it 'should return an empty string' do
96
+ db.ipv6 = nil
97
+ db.ipv6.should == ''
98
+ end
99
+ end
100
+
101
+ describe '#mongodump_string' do
102
+ it 'should return the full mongodump string' do
103
+ db.expects(:utility).with(:mongodump).returns('mongodump')
104
+ db.mongodump.should ==
105
+ "mongodump --db='mydatabase' --username='someuser' --password='secret' " +
106
+ "--host='localhost' --port='123' --ipv6 --query --out='#{ File.join(Backup::TMP_PATH, Backup::TRIGGER, 'MongoDB') }'"
107
+ end
108
+ end
109
+
110
+ describe '#perform!' do
111
+ before do
112
+ Backup::Logger.stubs(:message)
113
+ db.stubs(:utility).returns('mongodump')
114
+ db.stubs(:mkdir)
115
+ db.stubs(:run)
116
+ end
117
+
118
+ it 'should run the mongodump command and dump all collections' do
119
+ db.only_collections = []
120
+ db.expects(:dump!)
121
+
122
+ db.perform!
123
+ end
124
+
125
+ it 'should run the mongodump command and dump all collections' do
126
+ db.only_collections = nil
127
+ db.expects(:dump!)
128
+
129
+ db.perform!
130
+ end
131
+
132
+ it 'should lock database before dump if lock mode is enabled' do
133
+ db.lock = true
134
+ db.expects(:lock_database)
135
+
136
+ db.perform!
137
+ end
138
+
139
+ it 'should not lock database before dump if lock mode is disabled' do
140
+ db.lock = false
141
+ db.expects(:lock_database).never
142
+
143
+ db.perform!
144
+ end
145
+
146
+ it 'should unlock database after dump if lock mode is enabled' do
147
+ db.lock = true
148
+ db.expects(:unlock_database)
149
+
150
+ db.perform!
151
+ end
152
+
153
+ it 'should unlock the database if an exception is raised after it was locked' do
154
+ db.lock = true
155
+ db.expects(:unlock_database)
156
+ db.expects(:lock_database).raises(RuntimeError, 'something went wrong')
157
+ db.expects(:raise)
158
+
159
+ db.perform!
160
+ end
161
+
162
+ it 'should not unlock database after dump if lock mode is disabled' do
163
+ db.lock = false
164
+ db.expects(:unlock_database).never
165
+
166
+ db.perform!
167
+ end
168
+
169
+ it 'should dump only the provided collections' do
170
+ db.only_collections = %w[users admins profiles]
171
+ db.expects(:specific_collection_dump!)
172
+
173
+ db.perform!
174
+ end
175
+
176
+ it do
177
+ Backup::Logger.expects(:message).with("Backup::Database::MongoDB started dumping and archiving \"mydatabase\".")
178
+ db.perform!
179
+ end
180
+ end
181
+ end