forked from Ravenholdt-TC/SimcScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Archiver.rb
52 lines (47 loc) · 1.25 KB
/
Archiver.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
require 'zlib'
require 'rubygems/package'
require_relative 'lib/Interactive'
require_relative 'lib/SimcConfig'
filesToPack = "{#{SimcConfig['LogsFolder']},#{SimcConfig['ReportsFolder']},#{SimcConfig['GeneratedFolder']}}/**/*.{json,log,csv,simc,html}"
targetArchive = Time.now.strftime("#{SimcConfig['ArchivesFolder']}/%Y%m%d-%H%M%S.tar.gz")
fileArray = Dir.glob(filesToPack)
if fileArray.length < 1
puts 'Found nothing to pack...'
puts 'Press enter to quit...'
Interactive.GetInputOrArg()
exit
end
puts "Packaging logs and reports to #{targetArchive}..."
File.open(targetArchive, 'wb') do |archive|
Zlib::GzipWriter.wrap(archive) do |gz|
Gem::Package::TarWriter.new(gz) do |tar|
fileArray.each do |file|
mode = File.stat(file).mode
size = File.stat(file).size
tar.add_file_simple(file, mode, size) do |tf|
File.open(file, 'rb') do |f|
while buffer = f.read(1000000)
tf.write buffer
end
print '.'
end
end
end
end
end
end
puts
puts
puts 'Deleting old files...'
fileArray.each do |file|
begin
FileUtils.rm(file)
print '.'
rescue => err
puts err
end
end
puts
puts
puts 'Done! Press enter to quit...'
Interactive.GetInputOrArg()