Updated common and fixed bugs

Refactored 'archive_system' function
    - function now explicitly identifies which require compressing.
    - symbolic links are removed from the list(s) due to causing issues
      during the decompression stage when restoring the system. Symlinks
      are then stored in a record with the symlink name followed by the
      target. Both prefixed with paths to location and ':' delimited,
      respectfully.
    - system is then compressed using tar gzip compression, overwriting
      any previously generated archive generated.

Refactored 'restore_system' function
    - changed 'backup_file' to no longer require the final sed command
      similar what's found in 'archive_function' - unnecessary
      processing.
    - fixed tar statement to correctly output the contents of the
      decompression to the root of the system ("/").
    - added function to recreate the symlink files. It's unlikely that
      symlink files need replacing due to their behaviour; however, for
      clarity, the identified symlink files during archiving are recreated
      to their original location and linked to their target location.

Removed trailing whitespaces.
This commit is contained in:
Ethan Smith-Coss 2021-06-21 15:06:18 +01:00
parent da6922a337
commit d2e13eb940

View File

@ -9,7 +9,7 @@
# Author: Ethan Smith-Coss #
# Version: 0.1.4 #
# Created: 2021-05-20T16:47+0100 #
# Last Modified: 2021-06-13T21:36+0100 #
# Last Modified: 2021-06-21T15:06+0100 #
# #
# #################################### #
# #
@ -48,7 +48,21 @@ function archive_system {
local archive_output="/tmp/microsoft-edge-autoupdater/$(echo $1 | sed 's/\//_/').tar.gz"
[[ -f "$archive_output" ]] && return 0
grep 'usr/.*' "/tmp/microsoft-edge-autoupdater/Contents-amd64" | tar --overwrite -czf "$archive_output" "/opt/$1/" > "/tmp/microsoft-edge-autoupdater/tar_dump.log" 2>>"$log_out"
usr_files="$(grep -E '^usr/.*beta.*$' "/tmp/microsoft-edge-autoupdater/Contents-amd64" | awk '{print "/" $1}')"
opt_files="$(ls -1 "/opt/$1/" | sed -E "s/^(.*)$/\/opt\/"$(echo $1 | sed 's/\//\\\//')"\/\1/g")"
## :@Ethan: we need to identify all the symlinked files and store them in a record. When we decompress we reinstate the symlinks.
## This file is included in the archive and tar will dereference symlink files.
symlink_record="/tmp/microsoft-edge-autoupdater/symlink-record"
ls -l $opt_files $usr_files | grep -Eo ':.*->.*$' | sed 's/\s->\s/:/g' | cut -d' ' -f2- > "$symlink_record"
## remove identified symlink files from lists
while read -r record ; do
record=$(echo "$record" | cut -d: -f1)
[[ "$record" =~ \/usr\/ ]] && record=$(echo $record | sed 's/\//\\\//g') && usr_files=$(echo "$usr_files" | sed "s/^"$record"$//") && continue
[[ "$record" =~ \/opt\/ ]] && record=$(echo $record | sed 's/\//\\\//g') && opt_files=$(echo "$opt_files" | sed "s/^"$record"$//")
done < <(cat $symlink_record)
tar --overwrite -czf "$archive_output" $opt_files $usr_files > "/tmp/microsoft-edge-autoupdater/tar_dump.log" 2>>"$log_out"
exit_code=$?
[[ ! $exit_code -eq 0 ]] && { log "ERROR" "(Archive) There was an issue creating the archive file of system. Returning...(10)" "$log_out" ; return 10 ; }
@ -58,13 +72,29 @@ function archive_system {
function restore_files {
local log_out=$(realpath "${0%/*}/.log.1")
[[ -e $1 ]] && log "ERROR" "Restore function was called without any arguments." "$log_out"
local backup_file="/tmp/microsoft-edge-autoupdater/$(echo $1 | sed 's/\//_/').tar.gz"
local backup_file="/tmp/microsoft-edge-autoupdater/$1.tar.gz"
[[ ! -f "$backup_file" ]] && { log "ERROR" "There is no backup of Microsoft Edge (Beta) identified. Restoration not possible." "$log_out" ; return 12 ; }
tar -xzf "$backup_file" "/" > "/tmp/microsoft-edge-autoupdater/tar_dump.log" 2>>"$log_out"
tar --overwrite -xzf "$backup_file" -C "/" > "/tmp/microsoft-edge-autoupdater/tar_dump.log" 2>>"$log_out"
exit_code=$?
[[ ! $exit_code -eq 0 ]] && { log "ERROR" "There was an issue restoring pervious files to system." \
"The backup created will not be removed to allow for manual restoration. Returning...(12)" ; return 12 ; }
log "DEBUG" "Re-establishing symlinks..." "$log_out"
symlink_record="/tmp/microsoft-edge-autoupdater/symlink-record"
while read -r record ; do
symlink="$(echo $record | cut -d':' -f1)"
symlink_to="$(echo $record | cut -d':' -f2)"
## :NOTE: symlinks may already exist from new update and theoretically could be different in a newer version. Remove them first
rm "$symlink"
## create new sylinks based on the restored files
ln -s "$symlink_to" "$symlink" >/dev/null 2>>"$log_out"
exit_code=$?
[[ ! $exit_code -eq 0 ]] && { log "ERROR" "There was an issue trying to create a symlink to "$symlink_to"" "$log_out" ; continue ; }
log "DEBUG" "Successfully created symlink: "$symlink" -> "$symlink_to"" "$log_out"
done < <(cat $symlink_record)
log "DEBUG" "Symlinks re-established." "$log_out"
return 0
}