added feature to allow user to name their backup

This commit is contained in:
Tristan Ancelet 2024-10-19 10:55:30 -05:00
parent 7b61dd700a
commit eb875363e1

View File

@ -11,6 +11,7 @@ require 'rubygems/package'
require 'fileutils'
require 'optparse'
require 'zlib'
require 'find'
# END: Includes & Requires
@ -59,6 +60,7 @@ $CONFIG[:desktop_file_path] = $CONFIG[:discord_path] / 'discord.desktop'
$CONFIG[:discord_version_file] = $CONFIG[:discord_path] / 'resources' / 'build_info.json'
$CONFIG[:action] = 'install'
$CONFIG[:keep_installer] = false
$CONFIG[:debug] = false
DISCORD_DESKTOP_CONTENTS=<<EOF
[Desktop Entry]
Name=Discord
@ -95,9 +97,13 @@ OptionParser.new do |parser|
$CONFIG[:action] = 'uninstall'
end
parser.on('-b', '--backup', "Backup Discord Installation (not-implemented)") do
parser.on('-b', '--backup [FILENAME]', "Backup Discord Installation") do |backup_filename|
$CONFIG[:action] = 'backup'
if backup_filename.nil?
$CONFIG[:backup_filename] = 'discord-backup-' + Time.now.strftime('%Y-%m-%d') + '.tar.gz'
else
$CONFIG[:backup_filename] = backup_filename + ".tar.gz"
end
end
parser.separator ""
@ -107,6 +113,11 @@ OptionParser.new do |parser|
puts parser
exit
end
parser.on('-d', '--debug', "Turn on debug logging") do
$CONFIG[:debug] = true
end
parser.on('--keep', 'Keep discord tar.gz file (default: False)') do
$CONFIG[:keep_installer] = true
end
@ -201,43 +212,52 @@ def make_backup
symlinks: Array.new
}
Dir.glob("#{$CONFIG[:discord_config_path].path}/**/**/**/**").each{ |item|
[ $CONFIG[:discord_config_path].path, $CONFIG[:discord_path].path ].each do |backup_dir|
puts "make_backup: Getting Files from #{backup_dir})"
Find.find("#{backup_dir}").each{ |item|
puts("#{item} was retrieved from #{backup_dir}") if $CONFIG[:debug]
if File.file?(item)
puts("#{item} was found to be a file") if $CONFIG[:debug]
files[:files] << item
elsif File.symlink?(item)
puts("#{item} was found to be a symlink") if $CONFIG[:debug]
files[:symlinks] << item
elsif File.directory?(item)
puts("#{item} was found to be a directory") if $CONFIG[:debug]
files[:dirs] << item
end
}
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
puts "make_backup: Beginning backup process (OUTPUT_FILE = #{Dir.getwd}/#{$CONFIG[:backup_filename]})"
File.open($CONFIG[:backup_filename], "wb") do |backup_file|
Zlib::GzipWriter.wrap(backup_file) do |gzip|
Gem::Package::TarWriter.new(gzip) do |tar|
files[:files].each do |file|
puts "\nmake_backup : files : Beginning backup process for files (Count: #{files[:files].count})"
file_count = files[:files].count
files[:files].each_with_index { |file, i|
puts "make_backup : files : Backing up #{file} (#{i+1}/#{file_count})" if $CONFIG[:debug]
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|
tar.add_file_simple(file_to_archive.path.sub("#{Dir.home}/",''), file_to_archive.stat.mode, contents.length) { |io|
io.write(contents)
end
}
end
files[:dirs].each do |dir|
tar.mkdir(dir.sub("#{Dir.home}/",""), File.stat(dir).mode)
end
}
}
files[:symlinks].each {|symlink|
puts "\nmake_backup : directories : Beginning backup process for directories (Count: #{files[:dirs].count})"
dir_count = files[:dirs].count
files[:dirs].each_with_index {|dir,i|
puts "make_backup : directories : Backing up #{dir} (#{i+1}/#{dir_count})" if $CONFIG[:debug]
tar.mkdir(dir.sub("#{Dir.home}/",""), File.stat(dir).mode)
}
puts "\nmake_backup : symlinks : Beginning backup process for symlinks (Count: #{files[:symlinks].count})"
symlink_count = files[:symlinks].count
files[:symlinks].each_with_index {|symlink,i|
puts "make_backup : symlinks : Backing up #{symlink} (#{i+1}/#{symlink_count})" if $CONFIG[:debug]
tar.add_symlink(symlink.sub("#{Dir.home}/",""), File.readlink(symlink), File.lstat(symlink).mode)
}