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>
This commit is contained in:
René Fuhry 2023-03-09 00:19:24 +01:00 committed by GitHub
parent 0ec225d2fc
commit c7134e7919
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 39 deletions

21
PKGBUILD Normal file
View File

@ -0,0 +1,21 @@
# Maintainer: AustrianToast <rene dot fuhry at gmail dot com>
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
}

View File

@ -13,18 +13,23 @@ Before installing, please edit the config and configure it to your liking.
```bash ```bash
make install make install
``` ```
You will likely have to
## Usage ## Usage
``` ```
Usage: update [OPTION] Usage: update [OPTION]
options: options:
no flag same as -a no flag same as -p
-f updates using flatpak update only -f updates using flatpak update only
-p updates using a pacman-wrapper only -p updates using a pacman-wrapper only
-a updates using flatpak update and a pacman-wrapper -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) -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) -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 ## Contributing

View File

@ -1,12 +1,12 @@
VER = "v1.4" VER = "v1.7"
install: install:
@echo "==> Installing update $(VER)..." @echo "==> Installing update $(VER) into /usr/local/bin"
@sudo install -Dm755 update /usr/local/bin/update @sudo install -Dm755 update /usr/local/bin/update
@cp update.conf ${HOME}/.config/ @cp update.conf ${HOME}/.config/
@echo "==> Finished." @echo "==> Finished."
uninstall: uninstall:
@echo "==> Uninstalling update $(VER)..." @echo "==> Uninstalling update $(VER) from /usr/local/bin"
@sudo rm /usr/local/bin/update ${HOME}/.config/update.conf @sudo rm /usr/local/bin/update ${HOME}/.config/update.conf
@echo "==> Finished." @echo "==> Finished."

73
update
View File

@ -1,73 +1,82 @@
#!/bin/bash #!/bin/bash
# updates using a pacman-wrapper and flatpak-update with flags # updates using a pacman-wrapper and flatpak-update with flags
# version 1.4 # version 1.7
source "$HOME"/.config/update.conf
IGREEN="\033[0;92m" # Intense Green IGREEN="\033[0;92m" # Intense Green
IYELLOW="\033[0;93m" # Intense Red IYELLOW="\033[0;93m" # Intense Red
NC="\033[0m" # Text Reset NC="\033[0m" # Text Reset
TMP="/tmp/backup" 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)"
source "${HOME}"/.config/update.conf
# output if wrong flag is used
Help() { Help() {
echo "Usage: update [OPTION]" echo "Usage: update [OPTION]"
echo echo
echo "options:" echo "options:"
echo "no flag same as -a" echo "no flag same as -p"
echo "-f updates using flatpak update only" echo "-f updates using flatpak update only"
echo "-p updates using a pacman-wrapper only" echo "-p updates using a pacman-wrapper only"
echo "-a updates using flatpak update and a pacman-wrapper" 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 "-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 "-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() { before_backup() {
[[ ! -d /tmp/backup ]] && mkdir /tmp/backup if [[ $PRE_BACKUP_AMOUNT > $BACKUP_AMOUNT ]]; then
pacman -Q > "${TMP}"/pacman-pre.txt OLDEST_FILE="$(ls -t "$BACKUP_LOCATION" | grep before-backup | tail -1)"
flatpak list > "${TMP}"/flatpak-pre.txt 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 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 sudo rm /var/lib/pacman/db.lck
rsync "${TMP}"/before-backup.tar.xz.new "${backup_location}" cp "$TMP"/before-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION"
rename before-backup.tar.xz.new before-backup.tar.xz "${backup_location}"/before-backup.tar.xz.new 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() { after_backup() {
tar -cJf "${TMP}"/after-backup.tar.xz.new "${TMP}"/pacman-after.txt "${TMP}"/flatpak-after.txt &> /dev/null if [[ $POST_BACKUP_AMOUNT > $BACKUP_AMOUNT ]]; then
rsync "${TMP}"/after-backup.tar.xz.new "${backup_location}" OLDEST_FILE="$(ls -t "$BACKUP_LOCATION" | grep after-backup | tail -1)"
rename after-backup.tar.xz.new after-backup.tar.xz "${backup_location}"/after-backup.tar.xz.new rm "$BACKUP_LOCATION"/"$OLDEST_FILE"
rm -r /tmp/backup 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() { update_with_pacman_wrapper() {
"${pacman_wrapper}" "$PACMAN_WRAPPER"
pacman -Q > "${TMP}"/pacman-after.txt pacman -Q > "$TMP"/after-backup_"$DATE"/pacman-after.txt
} }
# updates using flatpak-update
update_with_flatpak() { update_with_flatpak() {
flatpak update -u --noninteractive 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 { while [ -f /var/lib/pacman/db.lck ]; do {
echo -e "${IYELLOW}->${NC} /var/lib/pacman/db.lck exists" 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 exit 1
} }
done done
before_backup && echo -e "${IGREEN}pre-backup complete${NC}" 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 while getopts 'fpagr' OPTIONS; do
case "$OPTIONS" in case "$OPTIONS" in
f) f)
@ -77,11 +86,13 @@ while getopts 'fpagr' OPTIONS; do
a) a)
update_with_pacman_wrapper; update_with_flatpak ;; update_with_pacman_wrapper; update_with_flatpak ;;
g) g)
[[ $1 = -g ]] && update_with_pacman_wrapper && update_with_flatpak; [[ $1 = -g ]] && update_with_pacman_wrapper; update_with_flatpak
after_backup && echo -e "${IGREEN}after-backup complete${NC}" && sleep 3s && shutdown now ;; after_backup && echo -e "${IGREEN}after-backup complete${NC}" && sleep 3s && shutdown now
exit 0;;
r) r)
[[ $1 = -r ]] && update_with_pacman_wrapper && update_with_flatpak; [[ $1 = -r ]] && update_with_pacman_wrapper; update_with_flatpak
after_backup && echo -e "${IGREEN}after-backup complete${NC}" && sleep 3s && reboot ;; after_backup && echo -e "${IGREEN}after-backup complete${NC}" && sleep 3s && reboot
exit 0;;
?) ?)
Help Help
exit 1;; exit 1;;

View File

@ -1,5 +1,8 @@
# Backup to this folder # Backup to this folder
backup_location=~/ BACKUP_LOCATION=~/
# use this pacman-wrapper # use this pacman-wrapper
pacman_wrapper=paru PACMAN_WRAPPER=yay
# how many different backups to keep
BACKUP_AMOUNT=2