interu-backup 3.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,106 @@
1
+ # encoding: utf-8
2
+
3
+ ##
4
+ # Only load the Net::SFTP library/gem when the Backup::Storage::SFTP class is loaded
5
+ Backup::Dependency.load('net-sftp')
6
+
7
+ module Backup
8
+ module Storage
9
+ class SFTP < Base
10
+
11
+ ##
12
+ # Server credentials
13
+ attr_accessor :username, :password
14
+
15
+ ##
16
+ # Server IP Address and SFTP port
17
+ attr_accessor :ip, :port
18
+
19
+ ##
20
+ # Path to store backups to
21
+ attr_accessor :path
22
+
23
+ ##
24
+ # Creates a new instance of the SFTP storage object
25
+ # First it sets the defaults (if any exist) and then evaluates
26
+ # the configuration block which may overwrite these defaults
27
+ def initialize(&block)
28
+ load_defaults!
29
+
30
+ @port ||= 22
31
+ @path ||= 'backups'
32
+
33
+ instance_eval(&block) if block_given?
34
+
35
+ @time = TIME
36
+ @path = path.sub(/^\~\//, '')
37
+ end
38
+
39
+ ##
40
+ # This is the remote path to where the backup files will be stored
41
+ def remote_path
42
+ File.join(path, TRIGGER)
43
+ end
44
+
45
+ ##
46
+ # Performs the backup transfer
47
+ def perform!
48
+ transfer!
49
+ cycle!
50
+ end
51
+
52
+ private
53
+
54
+ ##
55
+ # Establishes a connection to the remote server and returns the Net::SFTP object.
56
+ # Not doing any instance variable caching because this object gets persisted in YAML
57
+ # format to a file and will issues. This, however has no impact on performance since it only
58
+ # gets invoked once per object for a #transfer! and once for a remove! Backups run in the
59
+ # background anyway so even if it were a bit slower it shouldn't matter.
60
+ def connection
61
+ Net::SFTP.start(ip, username, :password => password, :port => port)
62
+ end
63
+
64
+ ##
65
+ # Transfers the archived file to the specified remote server
66
+ def transfer!
67
+ Logger.message("#{ self.class } started transferring \"#{ remote_file }\".")
68
+ create_remote_directories!
69
+ connection.upload!(
70
+ File.join(local_path, local_file),
71
+ File.join(remote_path, remote_file)
72
+ )
73
+ end
74
+
75
+ ##
76
+ # Removes the transferred archive file from the server
77
+ def remove!
78
+ begin
79
+ connection.remove!(
80
+ File.join(remote_path, remote_file)
81
+ )
82
+ rescue Net::SFTP::StatusException
83
+ Logger.warn "Could not remove file \"#{ File.join(remote_path, remote_file) }\"."
84
+ end
85
+ end
86
+
87
+ ##
88
+ # Creates (if they don't exist yet) all the directories on the remote
89
+ # server in order to upload the backup file. Net::SFTP does not support
90
+ # paths to directories that don't yet exist when creating new directories.
91
+ # Instead, we split the parts up in to an array (for each '/') and loop through
92
+ # that to create the directories one by one. Net::SFTP raises an exception when
93
+ # the directory it's trying ot create already exists, so we have rescue it
94
+ def create_remote_directories!
95
+ path_parts = Array.new
96
+ remote_path.split('/').each do |path_part|
97
+ path_parts << path_part
98
+ begin
99
+ connection.mkdir!(path_parts.join('/'))
100
+ rescue Net::SFTP::StatusException; end
101
+ end
102
+ end
103
+
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+
3
+ module Backup
4
+ module Syncer
5
+ class Base
6
+ include Backup::CLI
7
+ include Backup::Configuration::Helpers
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,152 @@
1
+ # encoding: utf-8
2
+
3
+ ##
4
+ # Require the tempfile Ruby library when Backup::Syncer::RSync is loaded
5
+ require 'tempfile'
6
+
7
+ module Backup
8
+ module Syncer
9
+ class RSync < Base
10
+
11
+ ##
12
+ # Server credentials
13
+ attr_accessor :username, :password
14
+
15
+ ##
16
+ # Server IP Address and SSH port
17
+ attr_accessor :ip
18
+
19
+ ##
20
+ # The SSH port to connect to
21
+ attr_writer :port
22
+
23
+ ##
24
+ # Directories to sync
25
+ attr_writer :directories
26
+
27
+ ##
28
+ # Path to store the synced files/directories to
29
+ attr_accessor :path
30
+
31
+ ##
32
+ # Flag for mirroring the files/directories
33
+ attr_writer :mirror
34
+
35
+ ##
36
+ # Flag for compressing (only compresses for the transfer)
37
+ attr_writer :compress
38
+
39
+ ##
40
+ # Additional options for the rsync cli
41
+ attr_accessor :additional_options
42
+
43
+ ##
44
+ # Instantiates a new RSync Syncer object and sets the default configuration
45
+ # specified in the Backup::Configuration::Syncer::RSync. Then it sets the object
46
+ # defaults if particular properties weren't set. Finally it'll evaluate the users
47
+ # configuration file and overwrite anything that's been defined
48
+ def initialize(&block)
49
+ load_defaults!
50
+
51
+ @directories = Array.new
52
+ @additional_options ||= Array.new
53
+ @path ||= 'backups'
54
+ @port ||= 22
55
+ @mirror ||= false
56
+ @compress ||= false
57
+
58
+ instance_eval(&block) if block_given?
59
+ write_password_file!
60
+
61
+ @path = path.sub(/^\~\//, '')
62
+ end
63
+
64
+ ##
65
+ # Performs the RSync operation
66
+ # debug options: -vhP
67
+ def perform!
68
+ Logger.message("#{ self.class } started syncing #{ directories }.")
69
+ Logger.silent(
70
+ run("#{ utility(:rsync) } -vhP #{ options } #{ directories } '#{ username }@#{ ip }:#{ path }'")
71
+ )
72
+
73
+ remove_password_file!
74
+ end
75
+
76
+ ##
77
+ # Returns all the specified Rsync options, concatenated, ready for the CLI
78
+ def options
79
+ ([archive, mirror, compress, port, password] + additional_options).compact.join("\s")
80
+ end
81
+
82
+ ##
83
+ # Returns Rsync syntax for enabling mirroring
84
+ def mirror
85
+ '--delete' if @mirror
86
+ end
87
+
88
+ ##
89
+ # Returns Rsync syntax for compressing the file transfers
90
+ def compress
91
+ '--compress' if @compress
92
+ end
93
+
94
+ ##
95
+ # Returns Rsync syntax for invoking "archive" mode
96
+ def archive
97
+ '--archive'
98
+ end
99
+
100
+ ##
101
+ # Returns Rsync syntax for defining a port to connect to
102
+ def port
103
+ "--port='#{@port}'"
104
+ end
105
+
106
+ ##
107
+ # Returns Rsync syntax for setting a password (via a file)
108
+ def password
109
+ "--password-file='#{@password_file.path}'" unless @password.nil?
110
+ end
111
+
112
+ ##
113
+ # If no block has been provided, it'll return the array of @directories.
114
+ # If a block has been provided, it'll evaluate it and add the defined paths to the @directories
115
+ def directories(&block)
116
+ unless block_given?
117
+ return @directories.map do |directory|
118
+ "'#{directory}'"
119
+ end.join("\s")
120
+ end
121
+ instance_eval(&block)
122
+ end
123
+
124
+ ##
125
+ # Adds a path to the @directories array
126
+ def add(path)
127
+ @directories << path
128
+ end
129
+
130
+ private
131
+
132
+ ##
133
+ # Writes the provided password to a temporary file so that
134
+ # the rsync utility can read the password from this file
135
+ def write_password_file!
136
+ unless @password.nil?
137
+ @password_file = Tempfile.new('backup-rsync-password')
138
+ @password_file.write(@password)
139
+ @password_file.close
140
+ end
141
+ end
142
+
143
+ ##
144
+ # Removes the previously created @password_file
145
+ # (temporary file containing the password)
146
+ def remove_password_file!
147
+ @password_file.unlink unless @password.nil?
148
+ end
149
+
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,118 @@
1
+ # encoding: utf-8
2
+
3
+ module Backup
4
+ module Syncer
5
+ class S3 < Base
6
+
7
+ ##
8
+ # Amazon Simple Storage Service (S3) Credentials
9
+ attr_accessor :access_key_id, :secret_access_key
10
+
11
+ ##
12
+ # Amazon S3 bucket name and path to sync to
13
+ attr_accessor :bucket, :path
14
+
15
+ ##
16
+ # Directories to sync
17
+ attr_accessor :directories
18
+
19
+ ##
20
+ # Flag to enable mirroring
21
+ attr_accessor :mirror
22
+
23
+ ##
24
+ # Additional options for the s3sync cli
25
+ attr_accessor :additional_options
26
+
27
+ ##
28
+ # Instantiates a new S3 Syncer object and sets the default configuration
29
+ # specified in the Backup::Configuration::Syncer::S3. Then it sets the object
30
+ # defaults if particular properties weren't set. Finally it'll evaluate the users
31
+ # configuration file and overwrite anything that's been defined
32
+ def initialize(&block)
33
+ load_defaults!
34
+
35
+ @path ||= 'backups'
36
+ @directories ||= Array.new
37
+ @mirror ||= false
38
+ @additional_options ||= []
39
+
40
+ instance_eval(&block) if block_given?
41
+
42
+ @path = path.sub(/^\//, '')
43
+ end
44
+
45
+ ##
46
+ # Performs the S3Sync operation
47
+ # First it'll set the Amazon S3 credentials for S3Sync before invoking it,
48
+ # and once it's finished syncing the files and directories to Amazon S3, it'll
49
+ # unset these credentials (back to nil values)
50
+ def perform!
51
+ set_environment_variables!
52
+
53
+ directories.each do |directory|
54
+ Logger.message("#{ self.class } started syncing '#{ directory }'.")
55
+ Logger.silent( run("#{ utility(:s3sync) } #{ options } '#{ directory }' '#{ bucket }:#{ path }'") )
56
+ end
57
+
58
+ unset_environment_variables!
59
+ end
60
+
61
+ ##
62
+ # Returns all the specified S3Sync options, concatenated, ready for the CLI
63
+ def options
64
+ ([verbose, recursive, mirror] + additional_options).compact.join("\s")
65
+ end
66
+
67
+ ##
68
+ # Returns S3Sync syntax for enabling mirroring
69
+ def mirror
70
+ '--delete' if @mirror
71
+ end
72
+
73
+ ##
74
+ # Returns S3Sync syntax for syncing recursively
75
+ def recursive
76
+ '--recursive'
77
+ end
78
+
79
+ ##
80
+ # Returns S3Sync syntax for making output verbose
81
+ def verbose
82
+ '--verbose'
83
+ end
84
+
85
+ ##
86
+ # Syntactical suger for the DSL for adding directories
87
+ def directories(&block)
88
+ return @directories unless block_given?
89
+ instance_eval(&block)
90
+ end
91
+
92
+ ##
93
+ # Adds a path to the @directories array
94
+ def add(path)
95
+ @directories << path
96
+ end
97
+
98
+ ##
99
+ # In order for S3Sync to know what credentials to use, we have to set the
100
+ # AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables, these
101
+ # evironment variables will be used by S3Sync
102
+ def set_environment_variables!
103
+ ENV['AWS_ACCESS_KEY_ID'] = access_key_id
104
+ ENV['AWS_SECRET_ACCESS_KEY'] = secret_access_key
105
+ ENV['AWS_CALLING_FORMAT'] = 'SUBDOMAIN'
106
+ end
107
+
108
+ ##
109
+ # Sets the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY back to nil
110
+ def unset_environment_variables!
111
+ ENV['AWS_ACCESS_KEY_ID'] = nil
112
+ ENV['AWS_SECRET_ACCESS_KEY'] = nil
113
+ ENV['AWS_CALLING_FORMAT'] = nil
114
+ end
115
+
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+
3
+ module Backup
4
+ class Version
5
+
6
+ ##
7
+ # Change the MAJOR, MINOR and PATCH constants below
8
+ # to adjust the version of the Backup gem
9
+ #
10
+ # MAJOR:
11
+ # Defines the major version
12
+ # MINOR:
13
+ # Defines the minor version
14
+ # PATCH:
15
+ # Defines the patch version
16
+ MAJOR, MINOR, PATCH = 3, 0, 16
17
+
18
+ ##
19
+ # Returns the major version ( big release based off of multiple minor releases )
20
+ def self.major
21
+ MAJOR
22
+ end
23
+
24
+ ##
25
+ # Returns the minor version ( small release based off of multiple patches )
26
+ def self.minor
27
+ MINOR
28
+ end
29
+
30
+ ##
31
+ # Returns the patch version ( updates, features and (crucial) bug fixes )
32
+ def self.patch
33
+ PATCH
34
+ end
35
+
36
+ ##
37
+ # Returns the current version of the Backup gem ( qualified for the gemspec )
38
+ def self.current
39
+ "#{major}.#{minor}.#{patch}"
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,7 @@
1
+ ##
2
+ # Archive [Archive]
3
+ #
4
+ archive :my_archive do |archive|
5
+ archive.add '/path/to/a/file.rb'
6
+ archive.add '/path/to/a/folder/'
7
+ end
@@ -0,0 +1,7 @@
1
+ ##
2
+ # Bzip2 [Compressor]
3
+ #
4
+ compress_with Bzip2 do |compression|
5
+ compression.best = true
6
+ compression.fast = false
7
+ end