Added installation backup functionality

This commit is contained in:
Tristan Ancelet 2024-10-19 10:16:53 -05:00
parent 01a838c7f2
commit 7b61dd700a

View File

@ -161,7 +161,6 @@ def do_install(version)
File.open(installer_file, 'rb') do |file|
Gem::Package.new("").extract_tar_gz(file, $CONFIG[:install_dir])
end
end
if not $CONFIG[:keep_installer]
puts "do_install: Removing new installer (#{installer_file})"
@ -169,6 +168,8 @@ def do_install(version)
end
end
end
def update_desktop_file
file_needs_updating = true
puts "update_desktop_file: Checking if desktop entry needs updating"
@ -194,32 +195,52 @@ def update_desktop_file
end
def make_backup
files = Array.new
Dir.glob("#{$CONFIG[:discord_config_path]}/**").each{|item|
if File.file?(item) or File.directory?(item)
files << File.new(item)
files = {
files: Array.new,
dirs: Array.new,
symlinks: Array.new
}
Dir.glob("#{$CONFIG[:discord_config_path].path}/**/**/**/**").each{ |item|
if File.file?(item)
files[:files] << item
elsif File.symlink?(item)
files[:symlinks] << item
elsif File.directory?(item)
files[:dirs] << item
end
}
Dir.glob("#{$CONFIG[:discord_path]}/**").each {|item|
if File.file?(item) or File.directory?(item)
files << File.new(item)
Dir.glob("#{$CONFIG[:discord_path].path}/**/**/**/**").each {|item|
if File.file?(item)
files[:files] << item
elsif File.symlink?(item)
files[:symlinks] << item
elsif File.directory?(item)
files[:dirs] << item
end
}
Dir.chdir(Dir.home) do
File.open($CONFIG[:backup_filename], "wb") do |file|
Zlib::GzipWriter.wrap(file) do |gzip|
File.open($CONFIG[:backup_filename], "wb") do |backup_file|
Zlib::GzipWriter.wrap(backup_file) do |gzip|
Gem::Package::TarWriter.new(gzip) do |tar|
files.each do |file|
if File.file?(file)
content = file.read()
tar.add_file_simple(file.path.sub("#{Dir.home}/",''), file.stat.mode, content.length) do |io|
io.write(content)
files[:files].each do |file|
File.open(file, 'rb') { |file_to_archive|
contents = file_to_archive.read()
tar.add_file_simple(file_to_archive.path.sub("#{Dir.home}/",''), file_to_archive.stat.mode, contents.length) do |io|
io.write(contents)
end
elsif File.directory?(file)
tar.mkdir(file.path.sub("#{Dir.home}/",""), file.stat.mode)
}
end
files[:dirs].each do |dir|
tar.mkdir(dir.sub("#{Dir.home}/",""), File.stat(dir).mode)
end
files[:symlinks].each {|symlink|
tar.add_symlink(symlink.sub("#{Dir.home}/",""), File.readlink(symlink), File.lstat(symlink).mode)
}
end
end
end