Made flatpak optional and some code cleanup #11

Merged
AustrianToast merged 6 commits from dev into main 2023-03-09 00:19:25 +01:00
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
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

View File

@ -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."

77
update
View File

@ -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)"
FUH22860 commented 2023-03-09 00:16:19 +01:00 (Migrated from github.com)
Review

Creates an error message when executing it because the directory cannot exist beforehand.

Creates an error message when executing it because the directory cannot exist beforehand.
FUH22860 commented 2023-03-09 00:18:06 +01:00 (Migrated from github.com)
Review

Error was addressed. Ready to merge.

Error was addressed. Ready to merge.
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;;

View File

@ -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