update/update

159 lines
5.4 KiB
Bash
Executable File

#!/usr/bin/bash
VERSION="2.1.1"
IGREEN="\033[0;92m" # Intense Green
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="true"
source "$HOME"/.config/update.conf || exit 1
if [ ! -d $BACKUP_LOCATION ]; then
echo -e "${IYELLOW}Backup location doesn't exist${NO_COLOR}"
read -p "Do you want to create the path and continue? [y/N]" input
case $input in
[Yy]) mkdir --parents "$BACKUP_LOCATION" ;;
[Nn]) exit 0;;
* ) exit 0 ;;
esac
fi
if [ ! -d $SECONDARY_BACKUP_LOCATION ]; then
echo -e "${IYELLOW}Secondary Backup location doesn't exist${NO_COLOR}"
read -p "Do you want to create the path and continue? [y/N]" input
case $input in
[Yy]) mkdir --parents "$SECONDARY_BACKUP_LOCATION" ;;
[Nn]) exit 0;;
* ) exit 0 ;;
esac
fi
trap interrupt_function INT
interrupt_function() {
echo "Interrupt has been detected"
[[ -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
}
help() {
echo "Usage: update [OPTION]"
echo
echo "options:"
echo "no option 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"
echo "-r reboots the computer afterwards"
echo "--help displays this message"
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"
}
lock_pacman_db() {
if [ -f /var/lib/pacman/db.lck ]; then
echo -e "${IYELLOW}->${NO_COLOR} /var/lib/pacman/db.lck exists"
echo -e "${IYELLOW}->${NO_COLOR} there might be another instance of pacman running. exiting..."
exit 1
fi
sudo touch /var/lib/pacman/db.lck
}
delete_oldest_backup() {
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
[[ -d $TMP ]] && rm --recursive --force "$TMP"
mkdir --parents "$TMP"/before-backup_"$DATE" "$TMP"/after-backup_"$DATE"
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
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() {
"$PACMAN_WRAPPER"
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
case "${1:2}" in
help)
help ;;
preview)
"$PACMAN_WRAPPER" -Syy
"$PACMAN_WRAPPER" --query --upgrades
[[ -x /usr/bin/flatpak ]] && flatpak remote-ls --updates ;;
version)
echo "$VERSION" ;;
backup)
before_backup
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
exit 0
fi
before_backup && echo -e "${IGREEN}pre-backup complete${NO_COLOR}"
[[ -z $1 ]] && update
while getopts 'fpagr' OPTIONS; do
case $OPTIONS in
f)
if [[ -x /usr/bin/flatpak ]]; then
flatpak update --assumeyes
flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt
fi ;;
p)
"$PACMAN_WRAPPER" ;;
a)
update ;;
g)
[[ $1 == \-\g ]] && update
FINAL_COMMAND="systemctl --check-inhibitors=yes poweroff" ;;
r)
[[ $1 == \-\r ]] && update
FINAL_COMMAND="systemctl --check-inhibitors=yes reboot" ;;
?)
help; exit 1;;
esac
done
after_backup && echo -e "${IGREEN}after-backup complete${NO_COLOR}"
$FINAL_COMMAND && exit 0