From c7134e791972c5314b252b5be3289002f322ec92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Thu, 9 Mar 2023 00:19:24 +0100 Subject: [PATCH] Made flatpak optional and some code cleanup (#11) * Add PKGBUILD * This is a basic implementation that works. * I know what I am doing. Trust me. * Made flatpak optional and code cleanup * fixed issue with useless error msg --------- Co-authored-by: FUH22860 <101731147+FUH22860@users.noreply.github.com> --- PKGBUILD | 21 +++++++++++++++ README.md | 7 ++++- makefile | 6 ++--- update | 77 ++++++++++++++++++++++++++++++----------------------- update.conf | 7 +++-- 5 files changed, 79 insertions(+), 39 deletions(-) create mode 100644 PKGBUILD diff --git a/PKGBUILD b/PKGBUILD new file mode 100644 index 0000000..1b4dabd --- /dev/null +++ b/PKGBUILD @@ -0,0 +1,21 @@ +# Maintainer: AustrianToast +pkgname=update-git +pkgver=1.4 +pkgrel=1 +pkgdesc="Update script for Arch linux written in bash" +arch=('any') +url="https://github.com/AustrianToast/update" +license=('GPL3') +provides=(update) +depends=('bash') +makedepends=('git') +optdepends=('yay' 'paru' 'pacaur' 'pikaur' 'aura' 'flatpak') +home=${HOME} +backup=(${home#/}/.config/update.conf) +source=("$pkgname::git+https://github.com/AustrianToast/update.git") +md5sums=('SKIP') + +package() { + cd "$pkgname" + make install +} diff --git a/README.md b/README.md index 471b20e..b254349 100644 --- a/README.md +++ b/README.md @@ -13,18 +13,23 @@ Before installing, please edit the config and configure it to your liking. ```bash make install ``` + +You will likely have to + ## Usage ``` Usage: update [OPTION] options: -no flag same as -a +no flag same as -p -f updates using flatpak update only -p updates using a pacman-wrapper only -a updates using flatpak update and a pacman-wrapper -g shutdowns the computer afterwards (needs to be the last or only option to work properly) -r reboots the computer afterwards (needs to be the last or only option to work properly) +--help displays this message +-P, --preview shows a preview of which pkg's will be updates ``` ## Contributing diff --git a/makefile b/makefile index ef4679b..cef642b 100644 --- a/makefile +++ b/makefile @@ -1,12 +1,12 @@ -VER = "v1.4" +VER = "v1.7" install: - @echo "==> Installing update $(VER)..." + @echo "==> Installing update $(VER) into /usr/local/bin" @sudo install -Dm755 update /usr/local/bin/update @cp update.conf ${HOME}/.config/ @echo "==> Finished." uninstall: - @echo "==> Uninstalling update $(VER)..." + @echo "==> Uninstalling update $(VER) from /usr/local/bin" @sudo rm /usr/local/bin/update ${HOME}/.config/update.conf @echo "==> Finished." diff --git a/update b/update index 499d5c3..f4ed718 100644 --- a/update +++ b/update @@ -1,73 +1,82 @@ #!/bin/bash # updates using a pacman-wrapper and flatpak-update with flags -# version 1.4 +# version 1.7 -IGREEN="\033[0;92m" # Intense Green -IYELLOW="\033[0;93m" # Intense Red -NC="\033[0m" # Text Reset -TMP="/tmp/backup" +source "$HOME"/.config/update.conf -source "${HOME}"/.config/update.conf +IGREEN="\033[0;92m" # Intense Green +IYELLOW="\033[0;93m" # Intense Red +NC="\033[0m" # Text Reset +TMP="/tmp/update" +DATE="$(date +"%Y-%m-%d %H:%M:%S")" +PRE_BACKUP_AMOUNT="$(ls -Ub "$BACKUP_LOCATION"/ | grep -c ^before-backup)" +POST_BACKUP_AMOUNT="$(ls -Ub "$BACKUP_LOCATION"/ | grep -c ^after-backup)" -# output if wrong flag is used Help() { echo "Usage: update [OPTION]" echo echo "options:" - echo "no flag same as -a" + echo "no flag same as -p" echo "-f updates using flatpak update only" echo "-p updates using a pacman-wrapper only" echo "-a updates using flatpak update and a pacman-wrapper" echo "-g shutdowns the computer afterwards (needs to be the last or only option to work properly)" echo "-r reboots the computer afterwards (needs to be the last or only option to work properly)" + echo "--help displays this message" + echo "-P, --preview shows a preview of which pkg's will be updates" } -# creates package lists, moves them into a tar, adds some other files and adds the pacman database -# it then rsyncs the tar to the backup location before_backup() { - [[ ! -d /tmp/backup ]] && mkdir /tmp/backup - pacman -Q > "${TMP}"/pacman-pre.txt - flatpak list > "${TMP}"/flatpak-pre.txt + if [[ $PRE_BACKUP_AMOUNT > $BACKUP_AMOUNT ]]; then + OLDEST_FILE="$(ls -t "$BACKUP_LOCATION" | grep before-backup | tail -1)" + rm "$BACKUP_LOCATION"/"$OLDEST_FILE" + fi + rm -r "$TMP" &> /dev/null + mkdir "$TMP" && mkdir "$TMP"/before-backup_"$DATE" && mkdir "$TMP"/after-backup_"$DATE" + pacman -Q > "$TMP"/before-backup_"$DATE"/pacman-pre.txt + [[ -f /usr/bin/flatpak ]] && flatpak list > "$TMP"/before-backup_"$DATE"/flatpak-pre.txt sudo touch /var/lib/pacman/db.lck - tar -cJf "${TMP}"/before-backup.tar.xz.new "${TMP}"/pacman-pre.txt "${TMP}"/flatpak-pre.txt /var/lib/pacman/local &> /dev/null + tar -cJf "$TMP"/before-backup_"$DATE".tar.xz.new "$TMP"/before-backup_"$DATE" /var/lib/pacman/local &> /dev/null sudo rm /var/lib/pacman/db.lck - rsync "${TMP}"/before-backup.tar.xz.new "${backup_location}" - rename before-backup.tar.xz.new before-backup.tar.xz "${backup_location}"/before-backup.tar.xz.new + cp "$TMP"/before-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION" + rename before-backup_"$DATE".tar.xz.new before-backup_"$DATE".tar.xz "$BACKUP_LOCATION"/before-backup_"$DATE".tar.xz.new } -# moves the package lists into a tar, it then rsyncs the tar to the backup location after_backup() { - tar -cJf "${TMP}"/after-backup.tar.xz.new "${TMP}"/pacman-after.txt "${TMP}"/flatpak-after.txt &> /dev/null - rsync "${TMP}"/after-backup.tar.xz.new "${backup_location}" - rename after-backup.tar.xz.new after-backup.tar.xz "${backup_location}"/after-backup.tar.xz.new - rm -r /tmp/backup + if [[ $POST_BACKUP_AMOUNT > $BACKUP_AMOUNT ]]; then + OLDEST_FILE="$(ls -t "$BACKUP_LOCATION" | grep after-backup | tail -1)" + rm "$BACKUP_LOCATION"/"$OLDEST_FILE" + fi + tar -cJf "$TMP"/after-backup_"$DATE".tar.xz.new "$TMP"/after-backup_"$DATE" &> /dev/null + cp "$TMP"/after-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION" + rename after-backup_"$DATE".tar.xz.new after-backup_"$DATE".tar.xz "$BACKUP_LOCATION"/after-backup_"$DATE".tar.xz.new + rm -r "$TMP" } -# updates using a pacman-wrapper update_with_pacman_wrapper() { - "${pacman_wrapper}" - pacman -Q > "${TMP}"/pacman-after.txt + "$PACMAN_WRAPPER" + pacman -Q > "$TMP"/after-backup_"$DATE"/pacman-after.txt } -# updates using flatpak-update update_with_flatpak() { flatpak update -u --noninteractive - flatpak list > "${TMP}"/flatpak-after.txt + flatpak list > "$TMP"/after-backup_"$DATE"/flatpak-after.txt } -[[ $1 = --help ]] && Help && exit 0; +[[ $1 = --help ]] && Help && exit 0 +[[ $1 = --preview || $1 = -P ]] && sudo pacman -Sy &> /dev/null && sudo pacman -Qu && exit 0 while [ -f /var/lib/pacman/db.lck ]; do { echo -e "${IYELLOW}->${NC} /var/lib/pacman/db.lck exists" - echo -e "${IYELLOW}->${NC} there might be an instance of pacman running. exiting..." + echo -e "${IYELLOW}->${NC} there might be another instance of pacman running. exiting..." exit 1 } done before_backup && echo -e "${IGREEN}pre-backup complete${NC}" -[[ -z $1 ]] && update_with_pacman_wrapper && update_with_flatpak; +[[ -z $1 ]] && update_with_pacman_wrapper; while getopts 'fpagr' OPTIONS; do case "$OPTIONS" in f) @@ -77,11 +86,13 @@ while getopts 'fpagr' OPTIONS; do a) update_with_pacman_wrapper; update_with_flatpak ;; g) - [[ $1 = -g ]] && update_with_pacman_wrapper && update_with_flatpak; - after_backup && echo -e "${IGREEN}after-backup complete${NC}" && sleep 3s && shutdown now ;; + [[ $1 = -g ]] && update_with_pacman_wrapper; update_with_flatpak + after_backup && echo -e "${IGREEN}after-backup complete${NC}" && sleep 3s && shutdown now + exit 0;; r) - [[ $1 = -r ]] && update_with_pacman_wrapper && update_with_flatpak; - after_backup && echo -e "${IGREEN}after-backup complete${NC}" && sleep 3s && reboot ;; + [[ $1 = -r ]] && update_with_pacman_wrapper; update_with_flatpak + after_backup && echo -e "${IGREEN}after-backup complete${NC}" && sleep 3s && reboot + exit 0;; ?) Help exit 1;; diff --git a/update.conf b/update.conf index 22a9b63..8151e97 100644 --- a/update.conf +++ b/update.conf @@ -1,5 +1,8 @@ # Backup to this folder -backup_location=~/ +BACKUP_LOCATION=~/ # use this pacman-wrapper -pacman_wrapper=paru +PACMAN_WRAPPER=yay + +# how many different backups to keep +BACKUP_AMOUNT=2