-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.rb
76 lines (58 loc) · 1.6 KB
/
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
require 'net/ftp'
require 'zip'
module Minecraft
class BackupError < StandardError; end
class Backup
class << self
def create(dir)
create_backup(dir)
end
private
def create_backup(dir)
zip = Zip::OutputStream.write_buffer do |zos|
fetch_files(dir).each do |ftp_file_path|
zos.put_next_entry ftp_file_path.gsub(%r{^\/}, '')
zos.print(ftp_file_data(ftp_file_path))
end
end
zip.rewind
File.open("#{backup_destination}#{backup_filename}", 'w') { |f| f.write zip.read }
end
def backup_filename
time_format = '%Y-%m-%dT%H-%M-%S' # "2019-08-18T12-17-57"
"#{ENV['WORLD_DIR']}_#{Time.now.strftime(time_format)}.zip"
end
def backup_destination
'../backups/'
end
def fetch_files(dir, accumulator = [])
ftp.chdir dir
ftp.list do |entry|
if directory? entry
fetch_files(entry.split.last, accumulator)
else
accumulator.push filepath(entry)
end
end
ftp.chdir '..'
accumulator
end
def ftp_file_data(filepath)
data = ''
ftp.retrbinary('RETR ' + filepath, Net::FTP::DEFAULT_BLOCKSIZE) do |block|
data << block
end
data
end
def ftp
@ftp ||= Net::FTP.new(ENV['FTP_HOST'], ENV['FTP_USERNAME'], ENV['FTP_PASSWORD'])
end
def directory?(entry)
entry.split(/\s+/)[0][0, 1] == 'd'
end
def filepath(entry)
ftp.pwd + '/' + entry.split.last
end
end
end
end