diff --git a/update b/update index e412d90..1d2c7a6 100755 --- a/update +++ b/update @@ -9,14 +9,15 @@ IYELLOW="\033[0;93m" # Intense Yellow NO_COLOR="\033[0m" # Text Reset TMP="/tmp/update" DATE="$(date +"%Y-%m-%dT%H:%M:%S%:z")" # RFC 3339 date-time https://datatracker.ietf.org/doc/html/rfc3339#section-5.6 -FINAL_COMMAND="" +FINAL_COMMAND="true" trap interrupt_function INT interrupt_function() { echo "Interrupt has been detected" - [[ -f /var/lib/pacman/db.lck ]] && sudo rm /var/lib/pacman/db.lck - rm --recursive "$TMP" + [[ -f /var/lib/pacman/db.lck ]] && sudo rm --force /var/lib/pacman/db.lck > /dev/null 2>&1 + rm --recursive --force "$TMP" > /dev/null 2>&1 + rm --force "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst.new exit 1 } @@ -31,7 +32,7 @@ help() { echo "-g shutdowns the computer afterwards" echo "-r reboots the computer afterwards" echo "--help displays this message" - echo "--preview shows a preview of which pkg's can be updated" + echo "--preview shows a preview of pkg's and flatpaks which can be updated" echo "--version prints out the version number" echo "--backup just does the before-backup without updating" } @@ -46,58 +47,63 @@ lock_pacman_db() { } delete_oldest_backup() { - if [[ $(ls -Ub "$BACKUP_LOCATION" | grep -c $1) -ge $BACKUP_AMOUNT ]]; then - OLDEST_FILE="$(ls -rt1 $BACKUP_LOCATION | grep $1 | head -1)" # sorts by time (oldes first) and gets the first item - rm "$BACKUP_LOCATION"/"$OLDEST_FILE" + if [[ $(find $BACKUP_LOCATION -name '*$1*' -exec printf %c {} + | wc -c) -ge $BACKUP_AMOUNT ]]; then + rm --force $(find "$BACKUP_LOCATION" -name '*$1*' | sort -rn | head -1) fi } before_backup() { lock_pacman_db - delete_oldest_backup before-backup - [[ -d $TMP ]] && rm -r "$TMP" + [[ -d $TMP ]] && rm --recursive --force "$TMP" mkdir --parents "$TMP"/before-backup_"$DATE" "$TMP"/after-backup_"$DATE" - pacman -Q > "$TMP"/before-backup_"$DATE"/pacman-before.txt - [[ -x /usr/bin/flatpak ]] && flatpak list --all --show-details > "$TMP"/before-backup_"$DATE"/flatpak-before.txt - tar --create --xz --file "$TMP"/before-backup_"$DATE".tar.xz.new "$TMP"/before-backup_"$DATE" /var/lib/pacman/local > /dev/null 2>&1 # for some reason it needs the output suppresion - sudo rm /var/lib/pacman/db.lck - mv "$TMP"/before-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION" - mv "$BACKUP_LOCATION"/before-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION"/before-backup_"$DATE".tar.xz + + pacman --verbose --query > "$TMP"/before-backup_"$DATE"/pacman-before.txt + [[ -f /usr/bin/flatpak ]] && flatpak list --all --show-details > "$TMP"/before-backup_"$DATE"/flatpak-before.txt + + tar --create --zstd --file "$TMP"/before-backup_"$DATE".tar.zst.new "$TMP"/before-backup_"$DATE" /var/lib/pacman/local > /dev/null 2>&1 # for some reason it needs the output suppresion + mv "$TMP"/before-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION" + + sudo rm --force /var/lib/pacman/db.lck } after_backup() { + pacman --verbose --query > "$TMP"/after-backup_"$DATE"/pacman-after.txt + flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt + tar --create --zstd --file "$TMP"/after-backup_"$DATE".tar.zst.new "$TMP"/after-backup_"$DATE" > /dev/null 2>&1 + + mv "$TMP"/after-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION" delete_oldest_backup after-backup - tar --create --xz --file "$TMP"/after-backup_"$DATE".tar.xz.new "$TMP"/after-backup_"$DATE" > /dev/null 2>&1 - mv "$TMP"/after-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION" - mv "$BACKUP_LOCATION"/after-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION"/after-backup_"$DATE".tar.xz - rm --recursive "$TMP" + mv "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst + + delete_oldest_backup before-backup + mv "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst + + rm --recursive --force "$TMP" } -update_with_pacman_wrapper() { +update() { "$PACMAN_WRAPPER" - pacman -Q > "$TMP"/after-backup_"$DATE"/pacman-after.txt -} - -update_with_flatpak() { if [[ -x /usr/bin/flatpak ]]; then flatpak update --assumeyes flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt fi } -if [[ ${1:0:2} = -- ]]; then +if [[ ${1:0:2} == \-\- ]]; then case "${1:2}" in help) help ;; preview) - "$PACMAN_WRAPPER" -Sy - "$PACMAN_WRAPPER" -Qu + "$PACMAN_WRAPPER" -Syy + "$PACMAN_WRAPPER" --query --upgrades [[ -x /usr/bin/flatpak ]] && flatpak remote-ls --updates ;; version) echo "$VERSION" ;; backup) before_backup - rm --recursive "$TMP" ;; + delete_oldest_backup before-backup + mv "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst + rm --recursive --force "$TMP" ;; ?) help; exit 1;; esac @@ -106,26 +112,23 @@ fi before_backup && echo -e "${IGREEN}pre-backup complete${NO_COLOR}" -if [[ -z $1 ]]; then -update_with_pacman_wrapper; update_with_flatpak -fi +[[ -z $1 ]] && update while getopts 'fpagr' OPTIONS; do case $OPTIONS in f) - update_with_flatpak ;; + if [[ -x /usr/bin/flatpak ]]; then + flatpak update --assumeyes + flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt + fi ;; p) - update_with_pacman_wrapper ;; + "$PACMAN_WRAPPER" ;; a) - update_with_pacman_wrapper; update_with_flatpak ;; + update ;; g) - if [ "$1" == "-g" ]; then - update_with_pacman_wrapper; update_with_flatpak - fi + [[ $1 == \-\g ]] && update FINAL_COMMAND="systemctl --check-inhibitors=yes poweroff" ;; r) - if [ "$1" == "-r" ]; then - update_with_pacman_wrapper; update_with_flatpak - fi + [[ $1 == \-\r ]] && update FINAL_COMMAND="systemctl --check-inhibitors=yes reboot" ;; ?) help; exit 1;; @@ -133,4 +136,4 @@ while getopts 'fpagr' OPTIONS; do done after_backup && echo -e "${IGREEN}after-backup complete${NO_COLOR}" -$FINAL_COMMAND; exit +$FINAL_COMMAND && exit 0