-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-ftp-backup.rb
executable file
·494 lines (394 loc) · 11.4 KB
/
simple-ftp-backup.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/usr/bin/env ruby
# Add local directory to LOAD_PATH
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))
# Define some variables
$backupsize = 0
$logmessage = ""
$errors = 0
$db_error = false
$dir_error = false
$file_error = false
$ftp_error = false
# Include some standard libs
require 'net/ftp'
require 'net/https'
require 'socket'
require 'settings'
require 'rubygems'
require 'date'
require 'base64'
require 'open3'
# Monkey patch
class Object
def to_sb
return 'false' if [FalseClass, NilClass].include?(self.class)
return 'true' if self.class == TrueClass
self
end
end
# Dashboard ping
def ping_dashboard(fatal_out = false)
begin
# Push to the dashboard if activated
if defined?(DASHBOARD) and DASHBOARD == true
errordata = "db:" + $db_error.to_sb + ",dir:" + $dir_error.to_sb + ",file:" + $file_error.to_sb + ",ftp:" + $ftp_error.to_sb
if fatal_out == true
errordata = "fatal:true"
end
uri = URI.parse(DASHBOARD_URL)
request_params = {
"server" => Socket.gethostname,
"bucket" => FTP_FOLDER,
"size" => $backupsize.to_s,
"errors" => $errors.to_s,
"output" => Base64.encode64($logmessage),
"errordata" => errordata
}
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.path)
request.set_form_data(request_params)
response = http.request(request)
end
end
end
# Some puts enhancement
def say(message)
begin
puts message
$logmessage += message.to_s
end
end
# Include additional libs
begin
require 'sequel'
require 'filesize'
rescue Exception => e
say("ERROR: Some includes are not found. Sure you have the gems installed?")
$errors += 1
ping_dashboard(true)
exit 1
end
# Trapping the user
trap "SIGINT" do
say("\nExiting. ATTENTION: The backup is not finished!")
$errors += 1
ping_dashboard(true)
exit 130
end
# Function for creating recursive directories
class Dir
def self.mkdirs(p)
return if File.exists?(p)
dir, file = File.split(p)
Dir.mkdirs(dir) if !File.exists?(dir)
Dir.mkdir(p)
end
end
# FTP function to remove a nonempty folder
def ftp_remove_all(ftp,file)
begin
ftp.chdir(file)
ftp.chdir("../")
data = ftp.list('-A ' + file)
for line in data
filename = line.split(/(\s)/)
ftp_remove_all(ftp,file + "/" + filename[filename.length-1])
end
ftp.rmdir(file)
rescue
ftp.delete(file)
end
end
def ftp_open
begin
ftp = Net::FTP.new(FTP_HOST, FTP_USER, FTP_PASS)
ftp.passive = FTP_PASSIVE
return ftp
rescue
say("ERROR: FTP connection failed: " + $!.to_s)
$errors += 1
$ftp_error = true
ping_dashboard()
exit
end
end
def ftp_close(ftp)
begin
path = ftp.pwd
ftp.close
return path
end
end
def ftp_go_upload(path, file)
begin
basename = File.basename(file)
# Open FTP and check for path existing
ftp = ftp_open
ftp.chdir(FTP_GROUND_PATH)
folderlist = ftp.list()
if !folderlist.any?{|dir| dir.match(/\s#{path}$/)}
ftp.mkdir(path)
end
ftp.chdir(path)
# Split and upload file to FTP
baresize = File.size(file)
$backupsize += baresize
filesize = Filesize.from("#{baresize} B").pretty
if (baresize/1024000) > SPLIT_SIZE
system("split -d -b #{SPLIT_SIZE}m #{file} #{file}.")
system("rm -rf #{file}")
Dir.glob("#{file}.*") do |item|
uploadname = File.basename(item)
ftp.putbinaryfile("#{item}", "#{uploadname}")
end
texttag = " (splitted)"
else
ftp.putbinaryfile("#{file}", "#{basename}")
texttag = ""
end
# Close ftp and return status
ftp_close(ftp)
say("Archive #{basename} uploaded (Size: #{filesize})#{texttag}")
rescue
# Rescue (hopefully...)
say("ERROR: Archive #{basename} is not fully uploaded! (#{$!})")
$errors += 1
$ftp_error = true
end
end
# Initial setup
timestamp = Time.now.strftime("%Y%m%d-%H%M")
full_tmp_path = File.join(TMP_BACKUP_PATH, "simple-ftp-backup-" << timestamp)
if defined?(DATEPATH) and DATEPATH!=false
basepath = "#{FTP_BASEPATH}/#{FTP_FOLDER}/#{timestamp}"
clean_basepath = "/#{FTP_BASEPATH}/#{FTP_FOLDER}"
elsif defined?(FTP_BASEPATH) and FTP_BASEPATH!=nil and FTP_BASEPATH!=""
basepath = "#{FTP_BASEPATH}/#{FTP_FOLDER}"
clean_basepath = "/#{FTP_BASEPATH}/#{FTP_FOLDER}"
else
basepath = FTP_FOLDER
clean_basepath = "/#{FTP_FOLDER}"
end
# Remove double slash
basepath = basepath.squeeze('/').strip
# Find/create the backup bucket
ftp = ftp_open()
flist = basepath.split("/")
flist.each do |folder|
folderlist = ftp.list()
ftp.mkdir(folder) if !folderlist.any?{|dir| dir.match(/\s#{folder}$/)}
ftp.chdir(folder)
end
FTP_GROUND_PATH = ftp.pwd
ftp.close
say("\nConnected to FTP and selected bucket\n")
# Create tmp directory
begin
Dir.mkdirs full_tmp_path
say('Created temporary path: ' + full_tmp_path)
rescue
say('Could not create temporary directory: ' + $!)
$errors += 1
ping_dashboard(true)
exit 1
end
# Perform MySQL backup of all databases or specific ones
if defined?(MYSQL_ALL) or defined?(MYSQL_DBS)
# Build an array of databases to backup
@databases = []
if defined?(MYSQL_ALL)
begin
connection = Sequel.mysql2 nil, :user => MYSQL_USER, :password => MYSQL_PASS, :host => 'localhost', :encoding => 'utf8'
@databases = connection['show databases;'].collect { |db| db[:Database] }
@databases.delete('performance_schema')
@databases.delete('information_schema')
@databases.delete('mysql')
rescue Exception => e
say('ERROR: MySQL connection not successful: ')
say($!)
$errors += 1
ping_dashboard(true)
exit 1
end
elsif defined?(MYSQL_DBS)
@databases = MYSQL_DBS
end
# Fail if there are no databases to backup
if @databases.empty?
say("Error: There are no db's to backup.")
$errors += 1
$db_error = true
end
@databases.each do |db|
# Define file name
db_filename = "db-#{db}-#{timestamp}.sql.gz"
# Define password parameter
if defined?(MYSQL_PASS) and MYSQL_PASS!=nil and MYSQL_PASS!=""
password_param = "-p#{MYSQL_PASS}"
else
password_param = ""
end
# Perform the mysqldump and compress the output to file
cmd = "#{MYSQLDUMP_CMD} -u #{MYSQL_USER} #{password_param} --single-transaction --add-drop-table --add-locks --create-options --disable-keys --extended-insert --events --quick #{db} | #{GZIP_CMD} -#{GZIP_STRENGTH} -c > #{full_tmp_path}/#{db_filename}"
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
while line = stderr.gets
say(line)
$errors += 1
$db_error = true
end
end
# Upload file to FTP
ftp_go_upload(MYSQLPATH, "#{full_tmp_path}/#{db_filename}")
# Remove the file
system("rm -rf #{full_tmp_path}/#{db_filename}")
end
say("\nMySQL backup finished\n")
end
# Perform MongoDB backups
if defined?(MONGO_DBS)
# Create dump dir
mdb_dump_dir = File.join(full_tmp_path, "mdbs")
Dir.mkdirs(mdb_dump_dir)
# Create dumps and upload them to ftp
MONGO_DBS.each do |mdb|
# Create dump and archive
mdb_filename = "mdb-#{mdb}.tgz"
cmd = "#{MONGODUMP_CMD} -h #{MONGO_HOST} -d #{mdb} -o #{mdb_dump_dir} && #{TAR_CMD} #{tarswitch} #{excludes} -cPf - . | #{GZIP_CMD} -#{GZIP_STRENGTH} -c > #{full_tmp_path}/#{dir_filename}"
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
while line = stderr.gets
say(line)
$errors += 1
$db_error = true
end
end
# Upload file to FTP
ftp_go_upload(MONGOPATH, "#{full_tmp_path}/#{mdb_filename}")
# Remove the file
system("rm -rf #{full_tmp_path}/#{mdb_filename}")
end
say("\nMongoDB backup finished\n")
end
# Perform directory backups
if defined?(DIRECTORIES) and ARGV[0] != "NOFILES"
# Add symlink tar switch
tarswitch = ""
if defined?(SYMLINKS) and SYMLINKS == true
tarswitch += " --dereference"
end
# Add ignore failed read option
if defined?(IGNORE_FAILED_READS) and IGNORE_FAILED_READS == true
tarswitch += " --ignore-failed-read --warning=none"
end
# For each list entry do some backups...
DIRECTORIES.each do |name, dir|
# Check if multiple subdirs backup
if dir.include? "*"
# Set constant if not sets
if (defined?(DIRECTORIES_EXCLUDE)).nil?
DIRECTORIES_EXCLUDE = []
end
# Go through each dir
Dir.glob("#{dir}").each do |dirpath|
# Make tar gz name
dirname = File.basename(dirpath)
dir_filename = "dir-#{name}-#{dirname}.tgz"
# Get excludes
excludes = ""
if defined?(DIRECTORIES_EXCLUDE)
DIRECTORIES_EXCLUDE.each do |de|
excludes += "--exclude=\"#{de}\" "
end
end
# Hell yeah, make some tgz!!
cmd = "#{TAR_CMD} #{tarswitch} #{excludes} -cPf - #{dirpath} | #{GZIP_CMD} -#{GZIP_STRENGTH} -c > #{full_tmp_path}/#{dir_filename}"
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
while line = stderr.gets
say(line)
$errors += 1
$dir_error = true
end
end
# Upload file to FTP
ftp_go_upload(FILEPATH, "#{full_tmp_path}/#{dir_filename}")
# Remove the file
system("rm -rf #{full_tmp_path}/#{dir_filename}")
end
else
# Define file name
dir_filename = "dir-#{name}.tgz"
# Get excludes
excludes = ""
if defined?(DIRECTORIES_EXCLUDE)
DIRECTORIES_EXCLUDE.each do |de|
excludes += "--exclude=\"#{de}\" "
end
end
# Create archive
cmd = "#{TAR_CMD} #{tarswitch} #{excludes} -cPf - #{dir} | #{GZIP_CMD} -#{GZIP_STRENGTH} -c > #{full_tmp_path}/#{dir_filename}"
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
while line = stderr.gets
say(line)
$errors += 1
$dir_error = true
end
end
# Upload file to FTP
ftp_go_upload(FILEPATH, "#{full_tmp_path}/#{dir_filename}")
# Remove the file
system("rm -rf #{full_tmp_path}/#{dir_filename}")
end
end
say("\nDirectories backup finished\n")
end
# Perform single files backups
if defined?(SINGLE_FILES)
SINGLE_FILES.each do |name, files|
# Create a directory to collect the files
files_tmp_path = File.join(full_tmp_path, "#{name}-tmp")
Dir.mkdirs(files_tmp_path)
# Filename for files
files_filename = "files-#{name}.tgz"
# Copy files to temp directory
files.each do |file|
system("cp #{file} #{files_tmp_path}")
end
# Create archive
cmd = "cd #{files_tmp_path} && #{TAR_CMD} #{tarswitch} #{excludes} -cPf - * | #{GZIP_CMD} -#{GZIP_STRENGTH} -c > #{full_tmp_path}/#{dir_filename}"
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
while line = stderr.gets
say(line)
$errors += 1
$file_error = true
end
end
# Upload file to FTP
ftp_go_upload(FILEPATH, "#{full_tmp_path}/#{files_filename}")
# Remove the files
system("rm -rf #{files_tmp_path} && rm -rf #{full_tmp_path}/#{files_filename}")
end
say("\nFile backup finished\n")
end
# Remove tmp directory
system("rm -rf #{full_tmp_path}")
print "\n"
# Now, clean up unwanted archives
ftp = ftp_open
list = ftp.list("#{clean_basepath}")
list.each do |file|
# Get file name
file = file.split(/\s+/).last
# Get dates
date = DateTime.strptime(file, "%Y%m%d-%H%M")
limit = DateTime.now - DAYS_OF_ARCHIVES
# Check if deletion is needed, if yes, delete
if date < limit
ftp_remove_all(ftp, "#{clean_basepath}/#{file}")
say("Old backup #{file} deleted.\n")
end
end
ftp.close
say("\nBackup finished.\n")
ping_dashboard()
exit