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 'fileutils'
require 'optparse' require 'optparse'
require 'zlib' require 'zlib'
require 'find'
# END: Includes & Requires # 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[:discord_version_file] = $CONFIG[:discord_path] / 'resources' / 'build_info.json'
$CONFIG[:action] = 'install' $CONFIG[:action] = 'install'
$CONFIG[:keep_installer] = false $CONFIG[:keep_installer] = false
$CONFIG[:debug] = false
DISCORD_DESKTOP_CONTENTS=<<EOF DISCORD_DESKTOP_CONTENTS=<<EOF
[Desktop Entry] [Desktop Entry]
Name=Discord Name=Discord
@ -95,9 +97,13 @@ OptionParser.new do |parser|
$CONFIG[:action] = 'uninstall' $CONFIG[:action] = 'uninstall'
end 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' $CONFIG[:action] = 'backup'
$CONFIG[:backup_filename] = 'discord-backup-' + Time.now.strftime('%Y-%m-%d') + '.tar.gz' 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 end
parser.separator "" parser.separator ""
@ -107,6 +113,11 @@ OptionParser.new do |parser|
puts parser puts parser
exit exit
end 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 parser.on('--keep', 'Keep discord tar.gz file (default: False)') do
$CONFIG[:keep_installer] = true $CONFIG[:keep_installer] = true
end end
@ -201,45 +212,54 @@ def make_backup
symlinks: Array.new 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|
if File.file?(item) puts "make_backup: Getting Files from #{backup_dir})"
files[:files] << item Find.find("#{backup_dir}").each{ |item|
elsif File.symlink?(item) puts("#{item} was retrieved from #{backup_dir}") if $CONFIG[:debug]
files[:symlinks] << item if File.file?(item)
elsif File.directory?(item) puts("#{item} was found to be a file") if $CONFIG[:debug]
files[:dirs] << item files[:files] << item
end elsif File.symlink?(item)
} puts("#{item} was found to be a symlink") if $CONFIG[:debug]
files[:symlinks] << item
Dir.glob("#{$CONFIG[:discord_path].path}/**/**/**/**").each {|item| elsif File.directory?(item)
if File.file?(item) puts("#{item} was found to be a directory") if $CONFIG[:debug]
files[:files] << item files[:dirs] << item
elsif File.symlink?(item) end
files[:symlinks] << item }
elsif File.directory?(item) end
files[:dirs] << item
end
}
Dir.chdir(Dir.home) do 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| File.open($CONFIG[:backup_filename], "wb") do |backup_file|
Zlib::GzipWriter.wrap(backup_file) do |gzip| Zlib::GzipWriter.wrap(backup_file) do |gzip|
Gem::Package::TarWriter.new(gzip) do |tar| 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| File.open(file, 'rb') { |file_to_archive|
contents = file_to_archive.read() 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) 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})"
tar.add_symlink(symlink.sub("#{Dir.home}/",""), File.readlink(symlink), File.lstat(symlink).mode) 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)
}
end end
end end