update/update
René Fuhry d916c2d0e6
fixed options
flatpak was not completly optional, but now should be fixed
2023-03-09 15:53:59 +01:00

103 lines
3.8 KiB
Bash

#!/bin/bash
# updates using a pacman-wrapper and flatpak-update with flags
# version 1.7
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)"
Help() {
echo "Usage: update [OPTION]"
echo
echo "options:"
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"
}
before_backup() {
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_"$DATE".tar.xz.new "$TMP"/before-backup_"$DATE" /var/lib/pacman/local &> /dev/null
sudo rm /var/lib/pacman/db.lck
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
}
after_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"
}
update_with_pacman_wrapper() {
"$PACMAN_WRAPPER"
pacman -Q > "$TMP"/after-backup_"$DATE"/pacman-after.txt
}
update_with_flatpak() {
flatpak update -u --noninteractive
flatpak list > "$TMP"/after-backup_"$DATE"/flatpak-after.txt
}
[[ $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 another instance of pacman running. exiting..."
exit 1
}
done
before_backup && echo -e "${IGREEN}pre-backup complete${NC}"
[[ -z $1 ]] && update_with_pacman_wrapper;
while getopts 'fpagr' OPTIONS; do
case "$OPTIONS" in
f)
update_with_flatpak ;;
p)
update_with_pacman_wrapper ;;
a)
update_with_pacman_wrapper; update_with_flatpak ;;
g)
[[ $1 = -g ]] && update_with_pacman_wrapper
after_backup && echo -e "${IGREEN}after-backup complete${NC}" && sleep 3s && shutdown now
exit 0;;
r)
[[ $1 = -r ]] && update_with_pacman_wrapper
after_backup && echo -e "${IGREEN}after-backup complete${NC}" && sleep 3s && reboot
exit 0;;
?)
Help
exit 1;;
esac
done
after_backup && echo -e "${IGREEN}after-backup complete${NC}"