refactoring

This commit is contained in:
AustrianToast 2023-11-30 23:43:32 +01:00
parent 97a0d2cd9c
commit 1d4feed7e5
No known key found for this signature in database
GPG Key ID: 5CD422268E489EB4
2 changed files with 41 additions and 44 deletions

View File

@ -41,7 +41,7 @@ no flag same as -p
--help displays this message --help displays this message
--preview shows a preview of which pkg's will be updates --preview shows a preview of which pkg's will be updates
--version prints out the version number --version prints out the version number
--backup just does the pre-backup without updating --backup just does the before-backup without updating
``` ```
## Contributing ## Contributing

83
update Normal file → Executable file
View File

@ -1,18 +1,14 @@
#!/usr/bin/bash #!/usr/bin/bash
# updates using a pacman-wrapper and flatpak-update with flags
source "$HOME"/.config/update.conf || exit 1 source "$HOME"/.config/update.conf || exit 1
[[ ! -d $BACKUP_LOCATION ]] && mkdir --parents $BACKUP_LOCATION [[ ! -d $BACKUP_LOCATION ]] && mkdir --parents "$BACKUP_LOCATION"
VER="2.0.8" VERSION="2.0.9"
IGREEN="\033[0;92m" # Intense Green IGREEN="\033[0;92m" # Intense Green
IYELLOW="\033[0;93m" # Intense Yellow IYELLOW="\033[0;93m" # Intense Yellow
NC="\033[0m" # Text Reset NO_COLOR="\033[0m" # Text Reset
TMP="/tmp/update" TMP="/tmp/update"
DATE="$(date +"%Y-%m-%d %H:%M:%S")" DATE="$(date +"%Y-%m-%dT%H:%M:%S%:z")" # RFC 3339 date-time
PRE_BACKUP_AMOUNT="$(ls -Ub "$BACKUP_LOCATION" | grep -c before-backup)"
POST_BACKUP_AMOUNT="$(ls -Ub "$BACKUP_LOCATION" | grep -c after-backup)"
FINAL_COMMAND="" FINAL_COMMAND=""
trap interrupt_function INT trap interrupt_function INT
@ -20,11 +16,11 @@ 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 > /dev/null 2>&1 [[ -f /var/lib/pacman/db.lck ]] && sudo rm /var/lib/pacman/db.lck > /dev/null 2>&1
rm -r "$TMP" > /dev/null 2>&1 rm --recursive "$TMP" > /dev/null 2>&1
exit 1 exit 1
} }
Help() { help() {
echo "Usage: update [OPTION]" echo "Usage: update [OPTION]"
echo echo
echo "options:" echo "options:"
@ -37,44 +33,44 @@ Help() {
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 which pkg's can be updated"
echo "--version prints out the version number" echo "--version prints out the version number"
echo "--backup just does the pre-backup without updating" echo "--backup just does the before-backup without updating"
} }
check_for_dblck() { lock_pacman_db() {
if [ -f /var/lib/pacman/db.lck ]; then if [ -f /var/lib/pacman/db.lck ]; then
echo -e "${IYELLOW}->${NC} /var/lib/pacman/db.lck exists" echo -e "${IYELLOW}->${NO_COLOR} /var/lib/pacman/db.lck exists"
echo -e "${IYELLOW}->${NC} there might be another instance of pacman running. exiting..." echo -e "${IYELLOW}->${NO_COLOR} there might be another instance of pacman running. exiting..."
exit 1 exit 1
fi fi
sudo touch /var/lib/pacman/db.lck
} }
delete_oldest_backup() { delete_oldest_backup() {
if [[ $1 -ge $BACKUP_AMOUNT ]]; then if [[ $(ls -Ub "$BACKUP_LOCATION" | grep -c $1) -ge $BACKUP_AMOUNT ]]; then
OLDEST_FILE="$(ls -t "$BACKUP_LOCATION" | grep $2 | tail -1)" OLDEST_FILE="$(ls -rt1 $BACKUP_LOCATION | grep $1 | head -1)" # sorts by time (oldes first) and gets the first item
rm "$BACKUP_LOCATION"/"$OLDEST_FILE" rm "$BACKUP_LOCATION"/"$OLDEST_FILE"
fi fi
} }
before_backup() { before_backup() {
check_for_dblck lock_pacman_db
delete_oldest_backup $PRE_BACKUP_AMOUNT before-backup delete_oldest_backup before-backup
rm -r "$TMP" > /dev/null 2>&1 [[ -d $TMP ]] && rm -r "$TMP"
mkdir "$TMP" && mkdir "$TMP"/before-backup_"$DATE" && mkdir "$TMP"/after-backup_"$DATE" mkdir --parents "$TMP"/before-backup_"$DATE" "$TMP"/after-backup_"$DATE"
pacman -Q > "$TMP"/before-backup_"$DATE"/pacman-pre.txt pacman -Q > "$TMP"/before-backup_"$DATE"/pacman-before.txt
[[ -f "$TMP"/before-backup_"$DATE"/pacman-pre.txt ]] || echo -e "$IYELLOW backup was unsuccessful" || exit 1 [[ -f /usr/bin/flatpak ]] && flatpak list --all --show-details > "$TMP"/before-backup_"$DATE"/flatpak-before.txt
[[ -f /usr/bin/flatpak ]] && flatpak list > "$TMP"/before-backup_"$DATE"/flatpak-pre.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
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 2>&1
sudo rm /var/lib/pacman/db.lck sudo rm /var/lib/pacman/db.lck
cp "$TMP"/before-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION" mv "$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 > /dev/null 2>&1 mv "$BACKUP_LOCATION"/before-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION"/before-backup_"$DATE".tar.xz
} }
after_backup() { after_backup() {
delete_oldest_backup $POST_BACKUP_AMOUNT after-backup delete_oldest_backup after-backup
tar -cJf "$TMP"/after-backup_"$DATE".tar.xz.new "$TMP"/after-backup_"$DATE" > /dev/null 2>&1 tar --create --xz --file "$TMP"/after-backup_"$DATE".tar.xz.new "$TMP"/after-backup_"$DATE" > /dev/null 2>&1
cp "$TMP"/after-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION" mv "$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 mv "$BACKUP_LOCATION"/after-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION"/after-backup_"$DATE".tar.xz
rm --recursive "$TMP"
} }
update_with_pacman_wrapper() { update_with_pacman_wrapper() {
@ -83,28 +79,29 @@ update_with_pacman_wrapper() {
} }
update_with_flatpak() { update_with_flatpak() {
flatpak update -u --noninteractive flatpak update --assumeyes
flatpak list > "$TMP"/after-backup_"$DATE"/flatpak-after.txt flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt
} }
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 > /dev/null 2>&1 "$PACMAN_WRAPPER" -Sy
"$PACMAN_WRAPPER" -Qu ;; "$PACMAN_WRAPPER" -Qu ;;
version) version)
echo "$VER" ;; echo "$VERSON" ;;
backup) backup)
before_backup ;; before_backup
rm --recursive "$TMP" ;;
?) ?)
Help && exit 1;; help; exit 1;;
esac esac
exit 0 exit 0
fi fi
before_backup && echo -e "${IGREEN}pre-backup complete${NC}" before_backup && echo -e "${IGREEN}pre-backup complete${NO_COLOR}"
[[ $1 ]] || update_with_pacman_wrapper [[ $1 ]] || update_with_pacman_wrapper
while getopts 'fpagr' OPTIONS; do while getopts 'fpagr' OPTIONS; do
@ -117,14 +114,14 @@ while getopts 'fpagr' OPTIONS; do
update_with_pacman_wrapper; update_with_flatpak ;; update_with_pacman_wrapper; update_with_flatpak ;;
g) g)
[[ $1 = -g ]] && update_with_pacman_wrapper [[ $1 = -g ]] && update_with_pacman_wrapper
FINAL_COMMAND="systemctl poweroff" ;; FINAL_COMMAND="systemctl --check-inhibitors=yes poweroff" ;;
r) r)
[[ $1 = -r ]] && update_with_pacman_wrapper [[ $1 = -r ]] && update_with_pacman_wrapper
FINAL_COMMAND="systemctl reboot" ;; FINAL_COMMAND="systemctl --check-inhibitors=yes reboot" ;;
?) ?)
Help && exit 1;; help; exit 1;;
esac esac
done done
after_backup && echo -e "${IGREEN}after-backup complete${NC}" after_backup && echo -e "${IGREEN}after-backup complete${NO_COLOR}"
$FINAL_COMMAND; exit 0 $FINAL_COMMAND; exit 0