refactor
too many to say
This commit is contained in:
parent
355abbf36b
commit
624371e8f1
85
update
85
update
@ -9,14 +9,15 @@ IYELLOW="\033[0;93m" # Intense Yellow
|
|||||||
NO_COLOR="\033[0m" # Text Reset
|
NO_COLOR="\033[0m" # Text Reset
|
||||||
TMP="/tmp/update"
|
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
|
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=""
|
FINAL_COMMAND="true"
|
||||||
|
|
||||||
trap interrupt_function INT
|
trap interrupt_function INT
|
||||||
|
|
||||||
interrupt_function() {
|
interrupt_function() {
|
||||||
echo "Interrupt has been detected"
|
echo "Interrupt has been detected"
|
||||||
[[ -f /var/lib/pacman/db.lck ]] && sudo rm /var/lib/pacman/db.lck
|
[[ -f /var/lib/pacman/db.lck ]] && sudo rm --force /var/lib/pacman/db.lck > /dev/null 2>&1
|
||||||
rm --recursive "$TMP"
|
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
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,7 +32,7 @@ help() {
|
|||||||
echo "-g shutdowns the computer afterwards"
|
echo "-g shutdowns the computer afterwards"
|
||||||
echo "-r reboots the computer afterwards"
|
echo "-r reboots the computer afterwards"
|
||||||
echo "--help displays this message"
|
echo "--help displays this message"
|
||||||
echo "--preview shows a preview of which pkg's can be updated"
|
echo "--preview shows a preview of pkg's and flatpaks which can be updated"
|
||||||
echo "--version prints out the version number"
|
echo "--version prints out the version number"
|
||||||
echo "--backup just does the before-backup without updating"
|
echo "--backup just does the before-backup without updating"
|
||||||
}
|
}
|
||||||
@ -46,58 +47,63 @@ lock_pacman_db() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
delete_oldest_backup() {
|
delete_oldest_backup() {
|
||||||
if [[ $(ls -Ub "$BACKUP_LOCATION" | grep -c $1) -ge $BACKUP_AMOUNT ]]; then
|
if [[ $(find $BACKUP_LOCATION -name '*$1*' -exec printf %c {} + | wc -c) -ge $BACKUP_AMOUNT ]]; then
|
||||||
OLDEST_FILE="$(ls -rt1 $BACKUP_LOCATION | grep $1 | head -1)" # sorts by time (oldes first) and gets the first item
|
rm --force $(find "$BACKUP_LOCATION" -name '*$1*' | sort -rn | head -1)
|
||||||
rm "$BACKUP_LOCATION"/"$OLDEST_FILE"
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
before_backup() {
|
before_backup() {
|
||||||
lock_pacman_db
|
lock_pacman_db
|
||||||
delete_oldest_backup before-backup
|
[[ -d $TMP ]] && rm --recursive --force "$TMP"
|
||||||
[[ -d $TMP ]] && rm -r "$TMP"
|
|
||||||
mkdir --parents "$TMP"/before-backup_"$DATE" "$TMP"/after-backup_"$DATE"
|
mkdir --parents "$TMP"/before-backup_"$DATE" "$TMP"/after-backup_"$DATE"
|
||||||
pacman -Q > "$TMP"/before-backup_"$DATE"/pacman-before.txt
|
|
||||||
[[ -x /usr/bin/flatpak ]] && flatpak list --all --show-details > "$TMP"/before-backup_"$DATE"/flatpak-before.txt
|
pacman --verbose --query > "$TMP"/before-backup_"$DATE"/pacman-before.txt
|
||||||
tar --create --xz --file "$TMP"/before-backup_"$DATE".tar.xz.new "$TMP"/before-backup_"$DATE" /var/lib/pacman/local > /dev/null 2>&1 # for some reason it needs the output suppresion
|
[[ -f /usr/bin/flatpak ]] && flatpak list --all --show-details > "$TMP"/before-backup_"$DATE"/flatpak-before.txt
|
||||||
sudo rm /var/lib/pacman/db.lck
|
|
||||||
mv "$TMP"/before-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION"
|
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 "$BACKUP_LOCATION"/before-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION"/before-backup_"$DATE".tar.xz
|
mv "$TMP"/before-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"
|
||||||
|
|
||||||
|
sudo rm --force /var/lib/pacman/db.lck
|
||||||
}
|
}
|
||||||
|
|
||||||
after_backup() {
|
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
|
delete_oldest_backup after-backup
|
||||||
tar --create --xz --file "$TMP"/after-backup_"$DATE".tar.xz.new "$TMP"/after-backup_"$DATE" > /dev/null 2>&1
|
mv "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst
|
||||||
mv "$TMP"/after-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION"
|
|
||||||
mv "$BACKUP_LOCATION"/after-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION"/after-backup_"$DATE".tar.xz
|
delete_oldest_backup before-backup
|
||||||
rm --recursive "$TMP"
|
mv "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst
|
||||||
|
|
||||||
|
rm --recursive --force "$TMP"
|
||||||
}
|
}
|
||||||
|
|
||||||
update_with_pacman_wrapper() {
|
update() {
|
||||||
"$PACMAN_WRAPPER"
|
"$PACMAN_WRAPPER"
|
||||||
pacman -Q > "$TMP"/after-backup_"$DATE"/pacman-after.txt
|
|
||||||
}
|
|
||||||
|
|
||||||
update_with_flatpak() {
|
|
||||||
if [[ -x /usr/bin/flatpak ]]; then
|
if [[ -x /usr/bin/flatpak ]]; then
|
||||||
flatpak update --assumeyes
|
flatpak update --assumeyes
|
||||||
flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt
|
flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
if [[ ${1:0:2} = -- ]]; then
|
if [[ ${1:0:2} == \-\- ]]; then
|
||||||
case "${1:2}" in
|
case "${1:2}" in
|
||||||
help)
|
help)
|
||||||
help ;;
|
help ;;
|
||||||
preview)
|
preview)
|
||||||
"$PACMAN_WRAPPER" -Sy
|
"$PACMAN_WRAPPER" -Syy
|
||||||
"$PACMAN_WRAPPER" -Qu
|
"$PACMAN_WRAPPER" --query --upgrades
|
||||||
[[ -x /usr/bin/flatpak ]] && flatpak remote-ls --updates ;;
|
[[ -x /usr/bin/flatpak ]] && flatpak remote-ls --updates ;;
|
||||||
version)
|
version)
|
||||||
echo "$VERSION" ;;
|
echo "$VERSION" ;;
|
||||||
backup)
|
backup)
|
||||||
before_backup
|
before_backup
|
||||||
rm --recursive "$TMP" ;;
|
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;;
|
help; exit 1;;
|
||||||
esac
|
esac
|
||||||
@ -106,26 +112,23 @@ fi
|
|||||||
|
|
||||||
before_backup && echo -e "${IGREEN}pre-backup complete${NO_COLOR}"
|
before_backup && echo -e "${IGREEN}pre-backup complete${NO_COLOR}"
|
||||||
|
|
||||||
if [[ -z $1 ]]; then
|
[[ -z $1 ]] && update
|
||||||
update_with_pacman_wrapper; update_with_flatpak
|
|
||||||
fi
|
|
||||||
while getopts 'fpagr' OPTIONS; do
|
while getopts 'fpagr' OPTIONS; do
|
||||||
case $OPTIONS in
|
case $OPTIONS in
|
||||||
f)
|
f)
|
||||||
update_with_flatpak ;;
|
if [[ -x /usr/bin/flatpak ]]; then
|
||||||
|
flatpak update --assumeyes
|
||||||
|
flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt
|
||||||
|
fi ;;
|
||||||
p)
|
p)
|
||||||
update_with_pacman_wrapper ;;
|
"$PACMAN_WRAPPER" ;;
|
||||||
a)
|
a)
|
||||||
update_with_pacman_wrapper; update_with_flatpak ;;
|
update ;;
|
||||||
g)
|
g)
|
||||||
if [ "$1" == "-g" ]; then
|
[[ $1 == \-\g ]] && update
|
||||||
update_with_pacman_wrapper; update_with_flatpak
|
|
||||||
fi
|
|
||||||
FINAL_COMMAND="systemctl --check-inhibitors=yes poweroff" ;;
|
FINAL_COMMAND="systemctl --check-inhibitors=yes poweroff" ;;
|
||||||
r)
|
r)
|
||||||
if [ "$1" == "-r" ]; then
|
[[ $1 == \-\r ]] && update
|
||||||
update_with_pacman_wrapper; update_with_flatpak
|
|
||||||
fi
|
|
||||||
FINAL_COMMAND="systemctl --check-inhibitors=yes reboot" ;;
|
FINAL_COMMAND="systemctl --check-inhibitors=yes reboot" ;;
|
||||||
?)
|
?)
|
||||||
help; exit 1;;
|
help; exit 1;;
|
||||||
@ -133,4 +136,4 @@ while getopts 'fpagr' OPTIONS; do
|
|||||||
done
|
done
|
||||||
|
|
||||||
after_backup && echo -e "${IGREEN}after-backup complete${NO_COLOR}"
|
after_backup && echo -e "${IGREEN}after-backup complete${NO_COLOR}"
|
||||||
$FINAL_COMMAND; exit
|
$FINAL_COMMAND && exit 0
|
||||||
|
Loading…
Reference in New Issue
Block a user