interu-backup 3.0.16
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +31 -0
- data/Gemfile.lock +117 -0
- data/Guardfile +17 -0
- data/LICENSE.md +24 -0
- data/README.md +332 -0
- data/backup.gemspec +31 -0
- data/bin/backup +267 -0
- data/lib/backup.rb +181 -0
- data/lib/backup/archive.rb +73 -0
- data/lib/backup/cli.rb +82 -0
- data/lib/backup/compressor/base.rb +17 -0
- data/lib/backup/compressor/bzip2.rb +64 -0
- data/lib/backup/compressor/gzip.rb +61 -0
- data/lib/backup/configuration/base.rb +15 -0
- data/lib/backup/configuration/compressor/base.rb +10 -0
- data/lib/backup/configuration/compressor/bzip2.rb +23 -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 +41 -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 +54 -0
- data/lib/backup/configuration/notifier/base.rb +39 -0
- data/lib/backup/configuration/notifier/campfire.rb +25 -0
- data/lib/backup/configuration/notifier/mail.rb +52 -0
- data/lib/backup/configuration/notifier/presently.rb +25 -0
- data/lib/backup/configuration/notifier/twitter.rb +21 -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 +29 -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/configuration/syncer/rsync.rb +45 -0
- data/lib/backup/configuration/syncer/s3.rb +33 -0
- data/lib/backup/database/base.rb +33 -0
- data/lib/backup/database/mongodb.rb +179 -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/dependency.rb +96 -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/exception/command_not_found.rb +8 -0
- data/lib/backup/finder.rb +39 -0
- data/lib/backup/logger.rb +102 -0
- data/lib/backup/model.rb +272 -0
- data/lib/backup/notifier/base.rb +29 -0
- data/lib/backup/notifier/binder.rb +32 -0
- data/lib/backup/notifier/campfire.rb +194 -0
- data/lib/backup/notifier/mail.rb +141 -0
- data/lib/backup/notifier/presently.rb +105 -0
- data/lib/backup/notifier/templates/notify_failure.erb +33 -0
- data/lib/backup/notifier/templates/notify_success.erb +16 -0
- data/lib/backup/notifier/twitter.rb +87 -0
- data/lib/backup/storage/base.rb +67 -0
- data/lib/backup/storage/cloudfiles.rb +95 -0
- data/lib/backup/storage/dropbox.rb +91 -0
- data/lib/backup/storage/ftp.rb +114 -0
- data/lib/backup/storage/object.rb +45 -0
- data/lib/backup/storage/rsync.rb +129 -0
- data/lib/backup/storage/s3.rb +180 -0
- data/lib/backup/storage/scp.rb +106 -0
- data/lib/backup/storage/sftp.rb +106 -0
- data/lib/backup/syncer/base.rb +10 -0
- data/lib/backup/syncer/rsync.rb +152 -0
- data/lib/backup/syncer/s3.rb +118 -0
- data/lib/backup/version.rb +43 -0
- data/lib/templates/archive +7 -0
- data/lib/templates/compressor/bzip2 +7 -0
- data/lib/templates/compressor/gzip +7 -0
- data/lib/templates/database/mongodb +14 -0
- data/lib/templates/database/mysql +14 -0
- data/lib/templates/database/postgresql +14 -0
- data/lib/templates/database/redis +13 -0
- data/lib/templates/encryptor/gpg +12 -0
- data/lib/templates/encryptor/openssl +8 -0
- data/lib/templates/notifier/campfire +11 -0
- data/lib/templates/notifier/mail +17 -0
- data/lib/templates/notifier/presently +12 -0
- data/lib/templates/notifier/twitter +12 -0
- data/lib/templates/readme +15 -0
- data/lib/templates/storage/cloudfiles +10 -0
- data/lib/templates/storage/dropbox +12 -0
- data/lib/templates/storage/ftp +11 -0
- data/lib/templates/storage/rsync +10 -0
- data/lib/templates/storage/s3 +21 -0
- data/lib/templates/storage/scp +11 -0
- data/lib/templates/storage/sftp +11 -0
- data/lib/templates/syncer/rsync +17 -0
- data/lib/templates/syncer/s3 +15 -0
- data/spec/archive_spec.rb +90 -0
- data/spec/backup_spec.rb +11 -0
- data/spec/compressor/bzip2_spec.rb +59 -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/campfire_spec.rb +20 -0
- data/spec/configuration/notifier/mail_spec.rb +32 -0
- data/spec/configuration/notifier/twitter_spec.rb +22 -0
- data/spec/configuration/storage/cloudfiles_spec.rb +34 -0
- data/spec/configuration/storage/dropbox_spec.rb +43 -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/configuration/syncer/rsync_spec.rb +46 -0
- data/spec/configuration/syncer/s3_spec.rb +43 -0
- data/spec/database/base_spec.rb +30 -0
- data/spec/database/mongodb_spec.rb +181 -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 +58 -0
- data/spec/model_spec.rb +236 -0
- data/spec/notifier/campfire_spec.rb +96 -0
- data/spec/notifier/mail_spec.rb +97 -0
- data/spec/notifier/presently_spec.rb +99 -0
- data/spec/notifier/twitter_spec.rb +86 -0
- data/spec/spec_helper.rb +25 -0
- data/spec/storage/base_spec.rb +33 -0
- data/spec/storage/cloudfiles_spec.rb +102 -0
- data/spec/storage/dropbox_spec.rb +105 -0
- data/spec/storage/ftp_spec.rb +133 -0
- data/spec/storage/object_spec.rb +74 -0
- data/spec/storage/rsync_spec.rb +131 -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/syncer/rsync_spec.rb +195 -0
- data/spec/syncer/s3_spec.rb +139 -0
- data/spec/version_spec.rb +21 -0
- metadata +231 -0
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backup
|
4
|
+
module Configuration
|
5
|
+
module Storage
|
6
|
+
class RSync < Base
|
7
|
+
class << self
|
8
|
+
|
9
|
+
##
|
10
|
+
# Server credentials
|
11
|
+
attr_accessor :username, :password
|
12
|
+
|
13
|
+
##
|
14
|
+
# Server IP Address and SSH port
|
15
|
+
attr_accessor :ip, :port
|
16
|
+
|
17
|
+
##
|
18
|
+
# Path to store backups to
|
19
|
+
attr_accessor :path
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backup
|
4
|
+
module Configuration
|
5
|
+
module Storage
|
6
|
+
class S3 < Base
|
7
|
+
class << self
|
8
|
+
|
9
|
+
##
|
10
|
+
# Amazon Simple Storage Service (S3) Credentials
|
11
|
+
attr_accessor :access_key_id, :secret_access_key
|
12
|
+
|
13
|
+
##
|
14
|
+
# Amazon S3 bucket name and path
|
15
|
+
attr_accessor :bucket, :path
|
16
|
+
|
17
|
+
##
|
18
|
+
# Region of the specified S3 bucket
|
19
|
+
attr_accessor :region
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backup
|
4
|
+
module Configuration
|
5
|
+
module Storage
|
6
|
+
class SCP < Base
|
7
|
+
class << self
|
8
|
+
|
9
|
+
##
|
10
|
+
# Server credentials
|
11
|
+
attr_accessor :username, :password
|
12
|
+
|
13
|
+
##
|
14
|
+
# Server IP Address and SCP port
|
15
|
+
attr_accessor :ip, :port
|
16
|
+
|
17
|
+
##
|
18
|
+
# Path to store backups to
|
19
|
+
attr_accessor :path
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backup
|
4
|
+
module Configuration
|
5
|
+
module Storage
|
6
|
+
class SFTP < Base
|
7
|
+
class << self
|
8
|
+
|
9
|
+
##
|
10
|
+
# Server credentials
|
11
|
+
attr_accessor :username, :password
|
12
|
+
|
13
|
+
##
|
14
|
+
# Server IP Address and SFTP port
|
15
|
+
attr_accessor :ip, :port
|
16
|
+
|
17
|
+
##
|
18
|
+
# Path to store backups to
|
19
|
+
attr_accessor :path
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backup
|
4
|
+
module Configuration
|
5
|
+
module Syncer
|
6
|
+
class RSync < Base
|
7
|
+
class << self
|
8
|
+
|
9
|
+
##
|
10
|
+
# Server credentials
|
11
|
+
attr_accessor :username, :password
|
12
|
+
|
13
|
+
##
|
14
|
+
# Server IP Address and SSH port
|
15
|
+
attr_accessor :ip
|
16
|
+
|
17
|
+
##
|
18
|
+
# The SSH port to connect to
|
19
|
+
attr_accessor :port
|
20
|
+
|
21
|
+
##
|
22
|
+
# Directories to sync
|
23
|
+
attr_accessor :directories
|
24
|
+
|
25
|
+
##
|
26
|
+
# Path to store the synced files/directories to
|
27
|
+
attr_accessor :path
|
28
|
+
|
29
|
+
##
|
30
|
+
# Flag for mirroring the files/directories
|
31
|
+
attr_accessor :mirror
|
32
|
+
|
33
|
+
##
|
34
|
+
# Flag for compressing (only compresses for the transfer)
|
35
|
+
attr_accessor :compress
|
36
|
+
|
37
|
+
##
|
38
|
+
# Additional options for the rsync cli
|
39
|
+
attr_accessor :additional_options
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backup
|
4
|
+
module Configuration
|
5
|
+
module Syncer
|
6
|
+
class S3 < Base
|
7
|
+
class << self
|
8
|
+
|
9
|
+
##
|
10
|
+
# Amazon Simple Storage Service (S3) Credentials
|
11
|
+
attr_accessor :access_key_id, :secret_access_key
|
12
|
+
|
13
|
+
##
|
14
|
+
# Amazon S3 bucket name and path to sync to
|
15
|
+
attr_accessor :bucket, :path
|
16
|
+
|
17
|
+
##
|
18
|
+
# Directories to sync
|
19
|
+
attr_accessor :directories
|
20
|
+
|
21
|
+
##
|
22
|
+
# Flag to enable mirroring
|
23
|
+
attr_accessor :mirror
|
24
|
+
|
25
|
+
##
|
26
|
+
# Additional options for the s3sync cli
|
27
|
+
attr_accessor :additional_options
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backup
|
4
|
+
module Database
|
5
|
+
class Base
|
6
|
+
include Backup::CLI
|
7
|
+
include Backup::Configuration::Helpers
|
8
|
+
|
9
|
+
##
|
10
|
+
# Contains the path to where the database should be dumped
|
11
|
+
attr_accessor :dump_path
|
12
|
+
|
13
|
+
##
|
14
|
+
# Allows the user to specify the path to a "dump" utility
|
15
|
+
# in case it cannot be auto-detected by Backup
|
16
|
+
attr_accessor :utility_path
|
17
|
+
|
18
|
+
##
|
19
|
+
# Defines the @dump_path and ensures it exists by creating it
|
20
|
+
def prepare!
|
21
|
+
@dump_path = File.join(TMP_PATH, TRIGGER, self.class.name.split('::').last)
|
22
|
+
mkdir(dump_path)
|
23
|
+
end
|
24
|
+
|
25
|
+
##
|
26
|
+
# Logs a message to the console and log file to inform
|
27
|
+
# the client that Backup is dumping the database
|
28
|
+
def log!
|
29
|
+
Logger.message("#{ self.class } started dumping and archiving \"#{ name }\".")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,179 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backup
|
4
|
+
module Database
|
5
|
+
class MongoDB < Base
|
6
|
+
|
7
|
+
##
|
8
|
+
# Name of the database that needs to get dumped
|
9
|
+
attr_accessor :name
|
10
|
+
|
11
|
+
##
|
12
|
+
# Credentials for the specified database
|
13
|
+
attr_accessor :username, :password
|
14
|
+
|
15
|
+
##
|
16
|
+
# Connectivity options
|
17
|
+
attr_accessor :host, :port
|
18
|
+
|
19
|
+
##
|
20
|
+
# IPv6 support (disabled by default)
|
21
|
+
attr_accessor :ipv6
|
22
|
+
|
23
|
+
##
|
24
|
+
# Collections to dump, collections that aren't specified won't get dumped
|
25
|
+
attr_accessor :only_collections
|
26
|
+
|
27
|
+
##
|
28
|
+
# Additional "mongodump" options
|
29
|
+
attr_accessor :additional_options
|
30
|
+
|
31
|
+
##
|
32
|
+
# 'lock' dump meaning wrapping mongodump with fsync & lock
|
33
|
+
attr_accessor :lock
|
34
|
+
|
35
|
+
##
|
36
|
+
# Creates a new instance of the MongoDB database object
|
37
|
+
def initialize(&block)
|
38
|
+
load_defaults!
|
39
|
+
|
40
|
+
@only_collections ||= Array.new
|
41
|
+
@additional_options ||= Array.new
|
42
|
+
@ipv6 ||= false
|
43
|
+
@lock ||= false
|
44
|
+
|
45
|
+
instance_eval(&block)
|
46
|
+
prepare!
|
47
|
+
end
|
48
|
+
|
49
|
+
##
|
50
|
+
# Builds the MongoDB credentials syntax to authenticate the user
|
51
|
+
# to perform the database dumping process
|
52
|
+
def credential_options
|
53
|
+
%w[username password].map do |option|
|
54
|
+
next if send(option).nil? or send(option).empty?
|
55
|
+
"--#{option}='#{send(option)}'"
|
56
|
+
end.compact.join("\s")
|
57
|
+
end
|
58
|
+
|
59
|
+
##
|
60
|
+
# Builds the MongoDB connectivity options syntax to connect the user
|
61
|
+
# to perform the database dumping process
|
62
|
+
def connectivity_options
|
63
|
+
%w[host port].map do |option|
|
64
|
+
next if send(option).nil? or (send(option).respond_to?(:empty?) and send(option).empty?)
|
65
|
+
"--#{option}='#{send(option)}'"
|
66
|
+
end.compact.join("\s")
|
67
|
+
end
|
68
|
+
|
69
|
+
##
|
70
|
+
# Builds a MongoDB compatible string for the
|
71
|
+
# additional options specified by the user
|
72
|
+
def additional_options
|
73
|
+
@additional_options.join("\s")
|
74
|
+
end
|
75
|
+
|
76
|
+
##
|
77
|
+
# Returns an array of collections to dump
|
78
|
+
def collections_to_dump
|
79
|
+
@only_collections
|
80
|
+
end
|
81
|
+
|
82
|
+
##
|
83
|
+
# Returns the MongoDB database selector syntax
|
84
|
+
def database
|
85
|
+
"--db='#{ name }'"
|
86
|
+
end
|
87
|
+
|
88
|
+
##
|
89
|
+
# Returns the mongodump syntax for enabling ipv6
|
90
|
+
def ipv6
|
91
|
+
@ipv6.eql?(true) ? '--ipv6' : ''
|
92
|
+
end
|
93
|
+
|
94
|
+
##
|
95
|
+
# Returns the MongoDB syntax for determining where to output all the database dumps,
|
96
|
+
# e.g. ~/Backup/.tmp/MongoDB/<databases here>/<database collections>
|
97
|
+
def dump_directory
|
98
|
+
"--out='#{ dump_path }'"
|
99
|
+
end
|
100
|
+
|
101
|
+
##
|
102
|
+
# Builds the full mongodump string based on all attributes
|
103
|
+
def mongodump
|
104
|
+
"#{ utility(:mongodump) } #{ database } #{ credential_options } " +
|
105
|
+
"#{ connectivity_options } #{ ipv6 } #{ additional_options } #{ dump_directory }"
|
106
|
+
end
|
107
|
+
|
108
|
+
##
|
109
|
+
# Performs the mongodump command and outputs the data to the
|
110
|
+
# specified path based on the 'trigger'. If the user hasn't specified any
|
111
|
+
# specific collections to dump, it'll dump everything. If the user has specified
|
112
|
+
# collections to dump, it'll loop through the array of collections and invoke the
|
113
|
+
# 'mongodump' command once per collection
|
114
|
+
def perform!
|
115
|
+
log!
|
116
|
+
|
117
|
+
begin
|
118
|
+
lock_database if @lock.eql?(true)
|
119
|
+
if collections_to_dump.is_a?(Array) and not collections_to_dump.empty?
|
120
|
+
specific_collection_dump!
|
121
|
+
else
|
122
|
+
dump!
|
123
|
+
end
|
124
|
+
unlock_database if @lock.eql?(true)
|
125
|
+
rescue => exception
|
126
|
+
unlock_database if @lock.eql?(true)
|
127
|
+
raise exception
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
##
|
132
|
+
# Builds and runs the mongodump command
|
133
|
+
def dump!
|
134
|
+
run(mongodump)
|
135
|
+
end
|
136
|
+
|
137
|
+
##
|
138
|
+
# For each collection in the @only_collections array, it'll
|
139
|
+
# build the whole 'mongodump' command, append the '--collection' option,
|
140
|
+
# and run the command built command
|
141
|
+
def specific_collection_dump!
|
142
|
+
collections_to_dump.each do |collection|
|
143
|
+
run("#{mongodump} --collection='#{collection}'")
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
##
|
148
|
+
# Builds the full mongo string based on all attributes
|
149
|
+
def mongo_shell
|
150
|
+
[utility(:mongo), database, credential_options, connectivity_options, ipv6].join(' ')
|
151
|
+
end
|
152
|
+
|
153
|
+
##
|
154
|
+
# Locks and FSyncs the database to bring it up to sync
|
155
|
+
# and ensure no 'write operations' are performed during the
|
156
|
+
# dump process
|
157
|
+
def lock_database
|
158
|
+
lock_command = <<-EOS
|
159
|
+
echo 'use admin
|
160
|
+
db.runCommand({"fsync" : 1, "lock" : 1})' | #{mongo_shell}
|
161
|
+
EOS
|
162
|
+
|
163
|
+
run(lock_command)
|
164
|
+
end
|
165
|
+
|
166
|
+
##
|
167
|
+
# Unlocks the (locked) database
|
168
|
+
def unlock_database
|
169
|
+
unlock_command = <<-EOS
|
170
|
+
echo 'use admin
|
171
|
+
db.$cmd.sys.unlock.findOne()' | #{mongo_shell}
|
172
|
+
EOS
|
173
|
+
|
174
|
+
run(unlock_command)
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backup
|
4
|
+
module Database
|
5
|
+
class MySQL < Base
|
6
|
+
|
7
|
+
##
|
8
|
+
# Name of the database that needs to get dumped
|
9
|
+
attr_accessor :name
|
10
|
+
|
11
|
+
##
|
12
|
+
# Credentials for the specified database
|
13
|
+
attr_accessor :username, :password
|
14
|
+
|
15
|
+
##
|
16
|
+
# Connectivity options
|
17
|
+
attr_accessor :host, :port, :socket
|
18
|
+
|
19
|
+
##
|
20
|
+
# Tables to skip while dumping the database
|
21
|
+
attr_accessor :skip_tables
|
22
|
+
|
23
|
+
##
|
24
|
+
# Tables to dump, tables that aren't specified won't get dumped
|
25
|
+
attr_accessor :only_tables
|
26
|
+
|
27
|
+
##
|
28
|
+
# Additional "mysqldump" options
|
29
|
+
attr_accessor :additional_options
|
30
|
+
|
31
|
+
##
|
32
|
+
# Creates a new instance of the MySQL adapter object
|
33
|
+
def initialize(&block)
|
34
|
+
load_defaults!
|
35
|
+
|
36
|
+
@skip_tables ||= Array.new
|
37
|
+
@only_tables ||= Array.new
|
38
|
+
@additional_options ||= Array.new
|
39
|
+
|
40
|
+
instance_eval(&block)
|
41
|
+
prepare!
|
42
|
+
end
|
43
|
+
|
44
|
+
##
|
45
|
+
# Builds the MySQL syntax for specifying which tables to skip
|
46
|
+
# during the dumping of the database
|
47
|
+
def tables_to_skip
|
48
|
+
skip_tables.map do |table|
|
49
|
+
"--ignore-table='#{name}.#{table}'"
|
50
|
+
end.join("\s")
|
51
|
+
end
|
52
|
+
|
53
|
+
##
|
54
|
+
# Builds the MySQL syntax for specifying which tables to dump
|
55
|
+
# during the dumping of the database
|
56
|
+
def tables_to_dump
|
57
|
+
only_tables.join("\s")
|
58
|
+
end
|
59
|
+
|
60
|
+
##
|
61
|
+
# Builds the credentials MySQL syntax to authenticate the user
|
62
|
+
# to perform the database dumping process
|
63
|
+
def credential_options
|
64
|
+
%w[username password].map do |option|
|
65
|
+
next if send(option).nil? or send(option).empty?
|
66
|
+
"--#{option}='#{send(option)}'".gsub('--username', '--user')
|
67
|
+
end.compact.join("\s")
|
68
|
+
end
|
69
|
+
|
70
|
+
##
|
71
|
+
# Builds the MySQL connectivity options syntax to connect the user
|
72
|
+
# to perform the database dumping process
|
73
|
+
def connectivity_options
|
74
|
+
%w[host port socket].map do |option|
|
75
|
+
next if send(option).nil? or (send(option).respond_to?(:empty?) and send(option).empty?)
|
76
|
+
"--#{option}='#{send(option)}'"
|
77
|
+
end.compact.join("\s")
|
78
|
+
end
|
79
|
+
|
80
|
+
##
|
81
|
+
# Builds a MySQL compatible string for the additional options
|
82
|
+
# specified by the user
|
83
|
+
def options
|
84
|
+
additional_options.join("\s")
|
85
|
+
end
|
86
|
+
|
87
|
+
##
|
88
|
+
# Builds the full mysqldump string based on all attributes
|
89
|
+
def mysqldump
|
90
|
+
"#{ utility(:mysqldump) } #{ credential_options } #{ connectivity_options } " +
|
91
|
+
"#{ options } #{ name } #{ tables_to_dump } #{ tables_to_skip }"
|
92
|
+
end
|
93
|
+
|
94
|
+
##
|
95
|
+
# Performs the mysqldump command and outputs the
|
96
|
+
# data to the specified path based on the 'trigger'
|
97
|
+
def perform!
|
98
|
+
log!
|
99
|
+
run("#{mysqldump} > '#{File.join(dump_path, name)}.sql'")
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|