-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive.rb
executable file
·175 lines (144 loc) · 5.01 KB
/
archive.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
#!/usr/bin/env ruby
require 'optparse'
require 'pp'
require 'find'
require 'wpcli'
require 'uri'
class Opts
Version = 1.0
CODES = %w[gz bz2 xz lzma]
CODE_ALIASES = { "gzip" => "gz", "bzip2" => "bz2", "lzma" => "xz" }
def self.parse(args)
# The options specified on the command line will be collected in *options*.
# We set default values here.
options = {
compression: "gz",
dest: "/tmp/",
target: "./",
switch: "z",
}
opts = OptionParser.new do |opts|
opts.banner = "Usage: #$0 [options]"
opts.separator ""
opts.separator "Specific options:"
# Cast 'target' argument to a object.
opts.on("-t", "--target TARGET", "Backup target(PATH must be absolute)") do |target|
options[:target] = target
end
# Cast 'dest' argument to a object.
opts.on("-d", "--dest [DESTINATION]", "Backup Destination") do |dest|
options[:dest] = dest
end
# Cast 'name' argument to a object.
opts.on("-n", "--name [NAME]", "Backup name") do |name|
options[:name] = name
end
code_list = (CODE_ALIASES.keys + CODES).join(',')
opts.on("-c", "--code [CODE]", CODES, CODE_ALIASES, "Select Compression", " (#{code_list})") do |compression|
options[:compression] = compression
if compression == "gzip"
switch = "z"
elsif compression == "bzip2"
switch = "j"
elsif compression == "lzma"
switch = "J"
end
options[:switch] = switch
end
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
options[:verbose] = v
end
opts.separator ""
opts.separator "Common options:"
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
opts.on_tail("-V", "--version", "Show version") do
puts Version
exit
end
end
begin
opts.parse!
mandatory = [:target]
missing = mandatory.select{ |param| options[param].nil? }
unless missing.empty?
puts "Missing options: #{missing.join(', ')}"
puts ""
puts opts
exit
end
rescue OptionParser::InvalidOption, OptionParser::MissingArgument
puts $!.to_s # Friendly output when parsing fails
puts opts
exit
end
options
end # parse
end # class
class Archiver
## Initialize options ##
def initialize(options)
@options = options
@target = @options[:target]
@dest = @options[:dest]
end
def wp_found(options)
begin
puts "Hello, #{@options[:target]} shall be searched to find WP installations..."
Dir.chdir(@target)
wpconfigs = Array.new()
Find.find(@options[:target]) do |path|
wpconfigs << path if path =~ /\/(wp|local)\-config\.php$/
end
wpconfigs.each do |file|
if file =~ /(bak|repo|archive|backup|safe|db|html\w|html\.)/
next
end
@wpcli = Wpcli::Client.new File.dirname(file)
puts "Backing up..."
ugly_site_name = @wpcli.run "option get siteurl --allow-root"
site_name = ugly_site_name.to_s.match(URI.regexp).to_s.sub(/^https?\:\/\//, '').sub(/^www./,'')
puts site_name
export_sql = @wpcli.run "db export #{site_name}.sql --allow-root"
export_sql
@backup_sql = "#{site_name}.sql"
@backup_target = File.basename(File.dirname(file))
@backup_parent = File.dirname(File.dirname(file))
compressor(options,site_name)
end
rescue => e
puts e
end
end # def
def compressor(options,site_name)
begin
tarballed_name = "#{site_name}.tar.#{@options[:compression]}"
puts "Compressing! with the following algorithm: #{@options[:compression]}"
Dir.chdir(@options[:dest])
`tar c#{@options[:switch]}vf #{tarballed_name} #{@backup_sql} -C #{@backup_parent} #{@backup_target}`
puts "Finished! Checking if #{@options[:dest]}#{@backup_sql} exists..."
thetruth_sql = File.exist?(File.expand_path("#{@options[:dest]}#{@backup_sql}"))
puts "The existence of #{@backup_sql} is #{thetruth_sql}"
puts "Deleting #{@options[:dest]}#{@backup_sql}..."
File.delete(@backup_sql)
thenewtruth = File.exist?(File.expand_path("#{@options[:dest]}#{@backup_sql}"))
puts "The existence of #{@backup_sql} is #{thenewtruth}"
deflated = "#{@options[:dest]}#{tarballed_name}"
puts "Finished! Checking if #{deflated} exists..."
thetruth_tar = File.exist?(File.expand_path(deflated))
puts "The existence of #{deflated} is #{thetruth_tar}"
puts `file #{deflated}`
rescue => e
puts e
end
end # def
end # class
### PARSE ###
begin
options = Opts.parse(ARGV)
Archiver.new(options).wp_found(options)
rescue => e
puts e
end