backup 2.4.5.1 → 3.0.0.build.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.infinity_test +7 -0
- data/.rspec +3 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +88 -0
- data/LICENSE.md +24 -0
- data/README.md +189 -75
- data/backup.gemspec +41 -0
- data/bin/backup +161 -90
- data/lib/backup.rb +133 -117
- data/lib/backup/archive.rb +54 -0
- data/lib/backup/cli.rb +50 -0
- data/lib/backup/compressor/base.rb +17 -0
- data/lib/backup/compressor/gzip.rb +61 -0
- data/lib/backup/configuration/base.rb +7 -67
- data/lib/backup/configuration/compressor/base.rb +10 -0
- data/lib/backup/configuration/compressor/gzip.rb +23 -0
- data/lib/backup/configuration/database/base.rb +18 -0
- data/lib/backup/configuration/database/mongodb.rb +37 -0
- data/lib/backup/configuration/database/mysql.rb +37 -0
- data/lib/backup/configuration/database/postgresql.rb +37 -0
- data/lib/backup/configuration/database/redis.rb +35 -0
- data/lib/backup/configuration/encryptor/base.rb +10 -0
- data/lib/backup/configuration/encryptor/gpg.rb +17 -0
- data/lib/backup/configuration/encryptor/open_ssl.rb +26 -0
- data/lib/backup/configuration/helpers.rb +47 -17
- data/lib/backup/configuration/notifier/base.rb +39 -0
- data/lib/backup/configuration/notifier/mail.rb +52 -0
- data/lib/backup/configuration/storage/base.rb +18 -0
- data/lib/backup/configuration/storage/cloudfiles.rb +21 -0
- data/lib/backup/configuration/storage/dropbox.rb +25 -0
- data/lib/backup/configuration/storage/ftp.rb +25 -0
- data/lib/backup/configuration/storage/rsync.rb +25 -0
- data/lib/backup/configuration/storage/s3.rb +25 -0
- data/lib/backup/configuration/storage/scp.rb +25 -0
- data/lib/backup/configuration/storage/sftp.rb +25 -0
- data/lib/backup/database/base.rb +33 -0
- data/lib/backup/database/mongodb.rb +137 -0
- data/lib/backup/database/mysql.rb +104 -0
- data/lib/backup/database/postgresql.rb +111 -0
- data/lib/backup/database/redis.rb +105 -0
- data/lib/backup/encryptor/base.rb +17 -0
- data/lib/backup/encryptor/gpg.rb +78 -0
- data/lib/backup/encryptor/open_ssl.rb +67 -0
- data/lib/backup/finder.rb +39 -0
- data/lib/backup/logger.rb +80 -0
- data/lib/backup/model.rb +249 -0
- data/lib/backup/notifier/base.rb +29 -0
- data/lib/backup/notifier/binder.rb +32 -0
- data/lib/backup/notifier/mail.rb +141 -0
- data/lib/backup/notifier/templates/notify_failure.erb +31 -0
- data/lib/backup/notifier/templates/notify_success.erb +16 -0
- data/lib/backup/storage/base.rb +60 -3
- data/lib/backup/storage/cloudfiles.rb +85 -6
- data/lib/backup/storage/dropbox.rb +74 -4
- data/lib/backup/storage/ftp.rb +103 -27
- data/lib/backup/storage/object.rb +45 -0
- data/lib/backup/storage/rsync.rb +100 -0
- data/lib/backup/storage/s3.rb +100 -7
- data/lib/backup/storage/scp.rb +94 -19
- data/lib/backup/storage/sftp.rb +94 -19
- data/lib/backup/version.rb +70 -1
- data/lib/templates/archive +4 -0
- data/lib/templates/compressor/gzip +4 -0
- data/lib/templates/database/mongodb +10 -0
- data/lib/templates/database/mysql +11 -0
- data/lib/templates/database/postgresql +11 -0
- data/lib/templates/database/redis +10 -0
- data/lib/templates/encryptor/gpg +9 -0
- data/lib/templates/encryptor/openssl +5 -0
- data/lib/templates/notifier/mail +14 -0
- data/lib/templates/readme +15 -0
- data/lib/templates/storage/cloudfiles +7 -0
- data/lib/templates/storage/dropbox +8 -0
- data/lib/templates/storage/ftp +8 -0
- data/lib/templates/storage/rsync +7 -0
- data/lib/templates/storage/s3 +8 -0
- data/lib/templates/storage/scp +8 -0
- data/lib/templates/storage/sftp +8 -0
- data/spec/archive_spec.rb +53 -0
- data/spec/backup_spec.rb +11 -0
- data/spec/compressor/gzip_spec.rb +59 -0
- data/spec/configuration/base_spec.rb +35 -0
- data/spec/configuration/compressor/gzip_spec.rb +28 -0
- data/spec/configuration/database/base_spec.rb +16 -0
- data/spec/configuration/database/mongodb_spec.rb +30 -0
- data/spec/configuration/database/mysql_spec.rb +32 -0
- data/spec/configuration/database/postgresql_spec.rb +32 -0
- data/spec/configuration/database/redis_spec.rb +30 -0
- data/spec/configuration/encryptor/gpg_spec.rb +25 -0
- data/spec/configuration/encryptor/open_ssl_spec.rb +31 -0
- data/spec/configuration/notifier/mail_spec.rb +32 -0
- data/spec/configuration/storage/cloudfiles_spec.rb +34 -0
- data/spec/configuration/storage/dropbox_spec.rb +40 -0
- data/spec/configuration/storage/ftp_spec.rb +40 -0
- data/spec/configuration/storage/rsync_spec.rb +37 -0
- data/spec/configuration/storage/s3_spec.rb +37 -0
- data/spec/configuration/storage/scp_spec.rb +40 -0
- data/spec/configuration/storage/sftp_spec.rb +40 -0
- data/spec/database/base_spec.rb +30 -0
- data/spec/database/mongodb_spec.rb +144 -0
- data/spec/database/mysql_spec.rb +150 -0
- data/spec/database/postgresql_spec.rb +164 -0
- data/spec/database/redis_spec.rb +122 -0
- data/spec/encryptor/gpg_spec.rb +57 -0
- data/spec/encryptor/open_ssl_spec.rb +102 -0
- data/spec/logger_spec.rb +37 -0
- data/spec/model_spec.rb +236 -0
- data/spec/notifier/mail_spec.rb +97 -0
- data/spec/spec_helper.rb +21 -0
- data/spec/storage/base_spec.rb +33 -0
- data/spec/storage/cloudfiles_spec.rb +102 -0
- data/spec/storage/dropbox_spec.rb +89 -0
- data/spec/storage/ftp_spec.rb +133 -0
- data/spec/storage/object_spec.rb +74 -0
- data/spec/storage/rsync_spec.rb +115 -0
- data/spec/storage/s3_spec.rb +110 -0
- data/spec/storage/scp_spec.rb +129 -0
- data/spec/storage/sftp_spec.rb +125 -0
- data/spec/version_spec.rb +32 -0
- metadata +139 -123
- data/CHANGELOG +0 -131
- data/LICENSE +0 -20
- data/generators/backup/backup_generator.rb +0 -69
- data/generators/backup/templates/backup.rake +0 -56
- data/generators/backup/templates/backup.rb +0 -253
- data/generators/backup/templates/create_backup_tables.rb +0 -18
- data/generators/backup_update/backup_update_generator.rb +0 -50
- data/generators/backup_update/templates/migrations/update_backup_tables.rb +0 -27
- data/lib/backup/adapters/archive.rb +0 -34
- data/lib/backup/adapters/base.rb +0 -167
- data/lib/backup/adapters/custom.rb +0 -41
- data/lib/backup/adapters/mongo_db.rb +0 -139
- data/lib/backup/adapters/mysql.rb +0 -60
- data/lib/backup/adapters/postgresql.rb +0 -60
- data/lib/backup/adapters/sqlite.rb +0 -25
- data/lib/backup/command_helper.rb +0 -14
- data/lib/backup/configuration/adapter.rb +0 -21
- data/lib/backup/configuration/adapter_options.rb +0 -8
- data/lib/backup/configuration/attributes.rb +0 -19
- data/lib/backup/configuration/mail.rb +0 -20
- data/lib/backup/configuration/smtp.rb +0 -8
- data/lib/backup/configuration/storage.rb +0 -8
- data/lib/backup/connection/cloudfiles.rb +0 -75
- data/lib/backup/connection/dropbox.rb +0 -63
- data/lib/backup/connection/s3.rb +0 -88
- data/lib/backup/core_ext/object.rb +0 -5
- data/lib/backup/environment/base.rb +0 -12
- data/lib/backup/environment/rails_configuration.rb +0 -15
- data/lib/backup/environment/unix_configuration.rb +0 -109
- data/lib/backup/mail/base.rb +0 -97
- data/lib/backup/mail/mail.txt +0 -7
- data/lib/backup/record/base.rb +0 -65
- data/lib/backup/record/cloudfiles.rb +0 -28
- data/lib/backup/record/dropbox.rb +0 -27
- data/lib/backup/record/ftp.rb +0 -39
- data/lib/backup/record/local.rb +0 -26
- data/lib/backup/record/s3.rb +0 -25
- data/lib/backup/record/scp.rb +0 -33
- data/lib/backup/record/sftp.rb +0 -38
- data/lib/backup/storage/local.rb +0 -22
- data/lib/generators/backup/USAGE +0 -10
- data/lib/generators/backup/backup_generator.rb +0 -47
- data/lib/generators/backup/templates/backup.rake +0 -56
- data/lib/generators/backup/templates/backup.rb +0 -236
- data/lib/generators/backup/templates/create_backup_tables.rb +0 -18
- data/setup/backup.rb +0 -257
- data/setup/backup.sqlite3 +0 -0
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backup
|
4
|
+
module Notifier
|
5
|
+
class Base
|
6
|
+
include Backup::Configuration::Helpers
|
7
|
+
|
8
|
+
##
|
9
|
+
# When set to true, the user will be notified by email
|
10
|
+
# when a backup process ends without raising any exceptions
|
11
|
+
attr_accessor :on_success
|
12
|
+
alias :notify_on_success? :on_success
|
13
|
+
|
14
|
+
##
|
15
|
+
# When set to true, the user will be notified by email
|
16
|
+
# when a backup process raises an exception before finishing
|
17
|
+
attr_accessor :on_failure
|
18
|
+
alias :notify_on_failure? :on_failure
|
19
|
+
|
20
|
+
##
|
21
|
+
# Logs a message to the console and log file to inform
|
22
|
+
# the client that Backup is notifying about the process
|
23
|
+
def log!
|
24
|
+
Logger.message "#{ self.class } started notifying about the process."
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backup
|
4
|
+
module Notifier
|
5
|
+
class Binder
|
6
|
+
|
7
|
+
##
|
8
|
+
# Shortcut to create a new instance of a binder, setting
|
9
|
+
# the instance variables using a Hash of key/values and getting
|
10
|
+
# the instance's binding. This returns the binding of the new Binder instance
|
11
|
+
def self.bind(key_and_values = {})
|
12
|
+
Binder.new(key_and_values).get_binding
|
13
|
+
end
|
14
|
+
|
15
|
+
##
|
16
|
+
# Creates a new Backup::Notifier::Binder instance. Loops through the provided
|
17
|
+
# Hash to set instance variables
|
18
|
+
def initialize(key_and_values)
|
19
|
+
key_and_values.each do |key, value|
|
20
|
+
instance_variable_set("@#{key}", value)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
##
|
25
|
+
# Returns the binding (needs a wrapper method because #binding is a private method)
|
26
|
+
def get_binding
|
27
|
+
binding
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
##
|
4
|
+
# Only load the Mail gem and Erb library when using Mail notifications
|
5
|
+
require 'mail'
|
6
|
+
require 'erb'
|
7
|
+
|
8
|
+
module Backup
|
9
|
+
module Notifier
|
10
|
+
class Mail < Base
|
11
|
+
|
12
|
+
##
|
13
|
+
# Container for the Mail object
|
14
|
+
attr_accessor :mail
|
15
|
+
|
16
|
+
##
|
17
|
+
# Container for the Model object
|
18
|
+
attr_accessor :model
|
19
|
+
|
20
|
+
##
|
21
|
+
# Sender and Receiver email addresses
|
22
|
+
# Examples:
|
23
|
+
# sender - my.email.address@gmail.com
|
24
|
+
# receiver - your.email.address@gmail.com
|
25
|
+
attr_accessor :from, :to
|
26
|
+
|
27
|
+
##
|
28
|
+
# The address to use
|
29
|
+
# Example: smtp.gmail.com
|
30
|
+
attr_accessor :address
|
31
|
+
|
32
|
+
##
|
33
|
+
# The port to connect to
|
34
|
+
# Example: 587
|
35
|
+
attr_accessor :port
|
36
|
+
|
37
|
+
##
|
38
|
+
# Your domain (if applicable)
|
39
|
+
# Example: mydomain.com
|
40
|
+
attr_accessor :domain
|
41
|
+
|
42
|
+
##
|
43
|
+
# Username and Password (sender email's credentials)
|
44
|
+
# Examples:
|
45
|
+
# user_name - meskyanichi
|
46
|
+
# password - my_secret_password
|
47
|
+
attr_accessor :user_name, :password
|
48
|
+
|
49
|
+
##
|
50
|
+
# Authentication type
|
51
|
+
# Example: plain
|
52
|
+
attr_accessor :authentication
|
53
|
+
|
54
|
+
##
|
55
|
+
# Automatically set TLS
|
56
|
+
# Example: true
|
57
|
+
attr_accessor :enable_starttls_auto
|
58
|
+
|
59
|
+
##
|
60
|
+
# Instantiates a new Backup::Notifier::Mail object
|
61
|
+
def initialize(&block)
|
62
|
+
load_defaults!
|
63
|
+
|
64
|
+
instance_eval(&block) if block_given?
|
65
|
+
|
66
|
+
set_defaults!
|
67
|
+
end
|
68
|
+
|
69
|
+
##
|
70
|
+
# Performs the notification
|
71
|
+
# Takes an exception object that might've been created if an exception occurred.
|
72
|
+
# If this is the case it'll invoke notify_failure!(exception), otherwise, if no
|
73
|
+
# error was raised, it'll go ahead and notify_success!
|
74
|
+
#
|
75
|
+
# If'll only perform these if on_success is true or on_failure is true
|
76
|
+
def perform!(model, exception = false)
|
77
|
+
@model = model
|
78
|
+
|
79
|
+
if notify_on_success? and exception.eql?(false)
|
80
|
+
log!
|
81
|
+
notify_success!
|
82
|
+
elsif notify_on_failure? and not exception.eql?(false)
|
83
|
+
log!
|
84
|
+
notify_failure!(exception)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
##
|
91
|
+
# Sends an email informing the user that the backup operation
|
92
|
+
# proceeded without any errors
|
93
|
+
def notify_success!
|
94
|
+
mail[:subject] = "[Backup::Succeeded] #{model.label} (#{model.trigger})"
|
95
|
+
mail[:body] = read_template('notify_success', Binder.bind(:model => @model))
|
96
|
+
mail.deliver!
|
97
|
+
end
|
98
|
+
|
99
|
+
##
|
100
|
+
# Sends an email informing the user that the backup operation
|
101
|
+
# raised an exception and will send the user the error details
|
102
|
+
def notify_failure!(exception)
|
103
|
+
mail[:subject] = "[Backup::Failed] #{model.label} (#{model.trigger})"
|
104
|
+
mail[:body] = read_template('notify_failure', Binder.bind(:model => @model, :exception => exception))
|
105
|
+
mail.deliver!
|
106
|
+
end
|
107
|
+
|
108
|
+
##
|
109
|
+
# Configures the Mail gem by setting the defaults.
|
110
|
+
# Instantiates the @mail object with the @to and @from attributes
|
111
|
+
def set_defaults!
|
112
|
+
defaults = {
|
113
|
+
:address => @address,
|
114
|
+
:port => @port,
|
115
|
+
:domain => @domain,
|
116
|
+
:user_name => @user_name,
|
117
|
+
:password => @password,
|
118
|
+
:authentication => @authentication,
|
119
|
+
:enable_starttls_auto => @enable_starttls_auto
|
120
|
+
}
|
121
|
+
|
122
|
+
::Mail.defaults do
|
123
|
+
delivery_method :smtp, defaults
|
124
|
+
end
|
125
|
+
|
126
|
+
@mail = ::Mail.new
|
127
|
+
@mail[:from] = @from
|
128
|
+
@mail[:to] = @to
|
129
|
+
end
|
130
|
+
|
131
|
+
##
|
132
|
+
# Returns the path to the templates, appended by the passed in argument
|
133
|
+
def read_template(file, binder)
|
134
|
+
ERB.new(File.read(
|
135
|
+
File.join( File.dirname(__FILE__), "templates", "#{file}.erb" )
|
136
|
+
)).result(binder)
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
|
2
|
+
There seemed to be a problem backing up <%= @model.label %> (<%= @model.trigger %>).
|
3
|
+
|
4
|
+
===========================================================================
|
5
|
+
Exception that got raised:
|
6
|
+
<%= @exception.to_s %>
|
7
|
+
===========================================================================
|
8
|
+
<%= @exception.backtrace.join("\n") %>
|
9
|
+
===========================================================================
|
10
|
+
|
11
|
+
|
12
|
+
You are running Backup version:
|
13
|
+
<%= Backup::Version.current %>
|
14
|
+
|
15
|
+
You are running Ruby version:
|
16
|
+
<%= ENV["RUBY_VERSION"] %>
|
17
|
+
|
18
|
+
|
19
|
+
---------------------------------------------------------------------------
|
20
|
+
|
21
|
+
View all Backup gem releases here:
|
22
|
+
http://rubygems.org/gems/backup
|
23
|
+
|
24
|
+
View the Backup git repository here:
|
25
|
+
https://github.com/meskyanichi/backup
|
26
|
+
|
27
|
+
View the Backup issues here:
|
28
|
+
https://github.com/meskyanichi/backup/issues
|
29
|
+
|
30
|
+
View the Backup wiki here:
|
31
|
+
https://github.com/meskyanichi/backup/wiki
|
@@ -0,0 +1,16 @@
|
|
1
|
+
|
2
|
+
Backup <%= @model.label %> (<%= @model.trigger %>) finished without any errors!
|
3
|
+
|
4
|
+
---------------------------------------------------------------------------
|
5
|
+
|
6
|
+
View all Backup gem releases here:
|
7
|
+
http://rubygems.org/gems/backup
|
8
|
+
|
9
|
+
View the Backup git repository here:
|
10
|
+
https://github.com/meskyanichi/backup
|
11
|
+
|
12
|
+
View the Backup issues here:
|
13
|
+
https://github.com/meskyanichi/backup/issues
|
14
|
+
|
15
|
+
View the Backup wiki here:
|
16
|
+
https://github.com/meskyanichi/backup/wiki
|
data/lib/backup/storage/base.rb
CHANGED
@@ -1,10 +1,67 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
module Backup
|
2
4
|
module Storage
|
3
5
|
class Base
|
4
|
-
include Backup::
|
6
|
+
include Backup::Configuration::Helpers
|
7
|
+
|
8
|
+
##
|
9
|
+
# The time when the backup initiated (in format: 2011.02.20.03.29.59)
|
10
|
+
attr_accessor :time
|
11
|
+
|
12
|
+
##
|
13
|
+
# Sets the limit to how many backups to keep in the remote location.
|
14
|
+
# If the limit exceeds it will remove the oldest backup to make room for the newest
|
15
|
+
attr_accessor :keep
|
16
|
+
|
17
|
+
##
|
18
|
+
# Returns the local path
|
19
|
+
def local_path
|
20
|
+
TMP_PATH
|
21
|
+
end
|
22
|
+
|
23
|
+
##
|
24
|
+
# Returns the local archive filename
|
25
|
+
def local_file
|
26
|
+
@local_file ||= File.basename(Backup::Model.file)
|
27
|
+
end
|
28
|
+
|
29
|
+
##
|
30
|
+
# Returns the name of the file that's stored on the remote location
|
31
|
+
def remote_file
|
32
|
+
@remote_file ||= local_file
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Provider defaults to false and will be overridden when using
|
37
|
+
# a service-based storage such as Amazon S3, Rackspace Cloud Files or Dropbox
|
38
|
+
def provider
|
39
|
+
false
|
40
|
+
end
|
41
|
+
|
42
|
+
##
|
43
|
+
# Checks the persisted storage data by type (S3, CloudFiles, SCP, etc)
|
44
|
+
# to see if the amount of stored backups is greater than the amount of
|
45
|
+
# backups allowed. If this is the case it'll invoke the #remove! method
|
46
|
+
# on each of the oldest backups that exceed the storage limit (specified by @keep).
|
47
|
+
# After that it'll re-assign the objects variable with an array of objects that still remain
|
48
|
+
# after the removal of the older objects and files (that exceeded the @keep range). And finally
|
49
|
+
# these remaining objects will be converted to YAML format and are written back to the YAML file
|
50
|
+
def cycle!
|
51
|
+
type = self.class.name.split("::").last
|
52
|
+
storage_object = Backup::Storage::Object.new(type)
|
53
|
+
objects = [self] + storage_object.load
|
54
|
+
if keep.is_a?(Integer) and keep > 0 and objects.count > keep
|
55
|
+
objects_to_remove = objects[keep..-1]
|
56
|
+
objects_to_remove.each do |object|
|
57
|
+
Logger.message "#{ self.class } started removing (cycling) \"#{ object.remote_file }\"."
|
58
|
+
object.send(:remove!)
|
59
|
+
end
|
60
|
+
objects = objects - objects_to_remove
|
61
|
+
end
|
62
|
+
storage_object.write(objects)
|
63
|
+
end
|
5
64
|
|
6
65
|
end
|
7
66
|
end
|
8
67
|
end
|
9
|
-
|
10
|
-
|
@@ -1,14 +1,93 @@
|
|
1
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
##
|
4
|
+
# Only load the Fog gem when the Backup::Storage::CloudFiles class is loaded
|
5
|
+
require 'fog'
|
2
6
|
|
3
7
|
module Backup
|
4
8
|
module Storage
|
5
9
|
class CloudFiles < Base
|
6
10
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
11
|
+
##
|
12
|
+
# Rackspace Cloud Files Credentials
|
13
|
+
attr_accessor :username, :api_key
|
14
|
+
|
15
|
+
##
|
16
|
+
# Rackspace Cloud Files container name and path
|
17
|
+
attr_accessor :container, :path
|
18
|
+
|
19
|
+
##
|
20
|
+
# Creates a new instance of the Rackspace Cloud Files storage object
|
21
|
+
# First it sets the defaults (if any exist) and then evaluates
|
22
|
+
# the configuration block which may overwrite these defaults
|
23
|
+
def initialize(&block)
|
24
|
+
load_defaults!
|
25
|
+
|
26
|
+
@path ||= 'backups'
|
27
|
+
|
28
|
+
instance_eval(&block) if block_given?
|
29
|
+
|
30
|
+
@time = TIME
|
31
|
+
end
|
32
|
+
|
33
|
+
##
|
34
|
+
# This is the remote path to where the backup files will be stored
|
35
|
+
def remote_path
|
36
|
+
File.join(path, TRIGGER)
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# This is the provider that Fog uses for the Cloud Files Storage
|
41
|
+
def provider
|
42
|
+
'Rackspace'
|
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 Rackspace Cloud Files and returns the Fog 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
|
+
Fog::Storage.new(
|
62
|
+
:provider => provider,
|
63
|
+
:rackspace_username => username,
|
64
|
+
:rackspace_api_key => api_key
|
65
|
+
)
|
66
|
+
end
|
67
|
+
|
68
|
+
##
|
69
|
+
# Transfers the archived file to the specified Cloud Files container
|
70
|
+
def transfer!
|
71
|
+
begin
|
72
|
+
Logger.message("#{ self.class } started transferring \"#{ remote_file }\".")
|
73
|
+
connection.put_object(
|
74
|
+
container,
|
75
|
+
File.join(remote_path, remote_file),
|
76
|
+
File.read(File.join(local_path, local_file))
|
77
|
+
)
|
78
|
+
rescue Excon::Errors::SocketError => e
|
79
|
+
puts "\nAn error occurred while trying to transfer the backup."
|
80
|
+
puts "Make sure the container exists and try again.\n\n"
|
81
|
+
exit
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
##
|
86
|
+
# Removes the transferred archive file from the Cloud Files container
|
87
|
+
def remove!
|
88
|
+
begin
|
89
|
+
connection.delete_object(container, File.join(remote_path, remote_file))
|
90
|
+
rescue Excon::Errors::SocketError; end
|
12
91
|
end
|
13
92
|
|
14
93
|
end
|
@@ -1,12 +1,82 @@
|
|
1
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
##
|
4
|
+
# Only load the Dropbox gem when the Backup::Storage::Dropbox class is loaded
|
5
|
+
require 'dropbox'
|
2
6
|
|
3
7
|
module Backup
|
4
8
|
module Storage
|
5
9
|
class Dropbox < Base
|
6
|
-
|
7
|
-
|
8
|
-
|
10
|
+
|
11
|
+
##
|
12
|
+
# Dropbox user credentials
|
13
|
+
attr_accessor :email, :password
|
14
|
+
|
15
|
+
##
|
16
|
+
# Dropbox API credentials
|
17
|
+
attr_accessor :api_key, :api_secret
|
18
|
+
|
19
|
+
##
|
20
|
+
# Path to where the backups will be stored
|
21
|
+
attr_accessor :path
|
22
|
+
|
23
|
+
##
|
24
|
+
# Creates a new instance of the Dropbox 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
|
+
@path ||= 'backups'
|
31
|
+
|
32
|
+
instance_eval(&block) if block_given?
|
33
|
+
|
34
|
+
@time = TIME
|
35
|
+
end
|
36
|
+
|
37
|
+
##
|
38
|
+
# This is the remote path to where the backup files will be stored
|
39
|
+
def remote_path
|
40
|
+
File.join(path, TRIGGER)
|
9
41
|
end
|
42
|
+
|
43
|
+
##
|
44
|
+
# Performs the backup transfer
|
45
|
+
def perform!
|
46
|
+
transfer!
|
47
|
+
cycle!
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
##
|
53
|
+
# Establishes a connection to Dropbox and returns the Dropbox::Session object.
|
54
|
+
# Not doing any instance variable caching because this object gets persisted in YAML
|
55
|
+
# format to a file and will issues. This, however has no impact on performance since it only
|
56
|
+
# gets invoked once per object for a #transfer! and once for a remove! Backups run in the
|
57
|
+
# background anyway so even if it were a bit slower it shouldn't matter.
|
58
|
+
def connection
|
59
|
+
session = ::Dropbox::Session.new(api_key, api_secret)
|
60
|
+
session.mode = :dropbox
|
61
|
+
session.authorizing_user = email
|
62
|
+
session.authorizing_password = password
|
63
|
+
session.authorize!
|
64
|
+
session
|
65
|
+
end
|
66
|
+
|
67
|
+
##
|
68
|
+
# Transfers the archived file to the specified Dropbox folder
|
69
|
+
def transfer!
|
70
|
+
Logger.message("#{ self.class } started transferring \"#{ remote_file }\".")
|
71
|
+
connection.upload(File.join(local_path, local_file), remote_path, :timeout => 300)
|
72
|
+
end
|
73
|
+
|
74
|
+
##
|
75
|
+
# Removes the transferred archive file from the Dropbox folder
|
76
|
+
def remove!
|
77
|
+
connection.delete(File.join(remote_path, remote_file))
|
78
|
+
end
|
79
|
+
|
10
80
|
end
|
11
81
|
end
|
12
82
|
end
|