Code cleanup
Improved Code by using extraction and I reduced the amount of redundant code
This commit is contained in:
parent
6341541289
commit
dd7cc62027
52
update
52
update
@ -1,7 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# updates using a pacman-wrapper and flatpak-update with flags
|
# updates using a pacman-wrapper and flatpak-update with flags
|
||||||
# version 1.7
|
# version 1.8
|
||||||
|
|
||||||
source "$HOME"/.config/update.conf
|
source "$HOME"/.config/update.conf
|
||||||
|
|
||||||
@ -24,14 +24,28 @@ Help() {
|
|||||||
echo "-g shutdowns the computer afterwards (needs to be the last or only option to work properly)"
|
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 "-r reboots the computer afterwards (needs to be the last or only option to work properly)"
|
||||||
echo "--help displays this message"
|
echo "--help displays this message"
|
||||||
echo "-P, --preview shows a preview of which pkg's will be updates"
|
echo "-P, --preview shows a preview of which pkg's can be updated"
|
||||||
|
}
|
||||||
|
|
||||||
|
check_for_dblck() {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
delete_oldest_backup() {
|
||||||
|
if [[ $1 > $BACKUP_AMOUNT ]]; then
|
||||||
|
OLDEST_FILE="$(ls -t "$BACKUP_LOCATION" | grep $2 | tail -1)"
|
||||||
|
rm "$BACKUP_LOCATION"/"$OLDEST_FILE"
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
before_backup() {
|
before_backup() {
|
||||||
if [[ $PRE_BACKUP_AMOUNT > $BACKUP_AMOUNT ]]; then
|
check_for_dblck
|
||||||
OLDEST_FILE="$(ls -t "$BACKUP_LOCATION" | grep before-backup | tail -1)"
|
delete_oldest_backup $PRE_BACKUP_AMOUNT before-backup
|
||||||
rm "$BACKUP_LOCATION"/"$OLDEST_FILE"
|
|
||||||
fi
|
|
||||||
rm -r "$TMP" &> /dev/null
|
rm -r "$TMP" &> /dev/null
|
||||||
mkdir "$TMP" && mkdir "$TMP"/before-backup_"$DATE" && mkdir "$TMP"/after-backup_"$DATE"
|
mkdir "$TMP" && mkdir "$TMP"/before-backup_"$DATE" && mkdir "$TMP"/after-backup_"$DATE"
|
||||||
pacman -Q > "$TMP"/before-backup_"$DATE"/pacman-pre.txt
|
pacman -Q > "$TMP"/before-backup_"$DATE"/pacman-pre.txt
|
||||||
@ -44,10 +58,7 @@ before_backup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
after_backup() {
|
after_backup() {
|
||||||
if [[ $POST_BACKUP_AMOUNT > $BACKUP_AMOUNT ]]; then
|
delete_oldest_backup $POST_BACKUP_AMOUNT after-backup
|
||||||
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
|
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"
|
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
|
rename after-backup_"$DATE".tar.xz.new after-backup_"$DATE".tar.xz "$BACKUP_LOCATION"/after-backup_"$DATE".tar.xz.new
|
||||||
@ -55,8 +66,10 @@ after_backup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
update_with_pacman_wrapper() {
|
update_with_pacman_wrapper() {
|
||||||
|
before_backup && echo -e "${IGREEN}pre-backup complete${NC}"
|
||||||
"$PACMAN_WRAPPER"
|
"$PACMAN_WRAPPER"
|
||||||
pacman -Q > "$TMP"/after-backup_"$DATE"/pacman-after.txt
|
pacman -Q > "$TMP"/after-backup_"$DATE"/pacman-after.txt
|
||||||
|
after_backup && echo -e "${IGREEN}after-backup complete${NC}"
|
||||||
}
|
}
|
||||||
|
|
||||||
update_with_flatpak() {
|
update_with_flatpak() {
|
||||||
@ -67,15 +80,6 @@ update_with_flatpak() {
|
|||||||
[[ $1 = --help ]] && Help && exit 0
|
[[ $1 = --help ]] && Help && exit 0
|
||||||
[[ $1 = --preview || $1 = -P ]] && sudo pacman -Sy &> /dev/null && sudo pacman -Qu && 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;
|
[[ -z $1 ]] && update_with_pacman_wrapper;
|
||||||
while getopts 'fpagr' OPTIONS; do
|
while getopts 'fpagr' OPTIONS; do
|
||||||
case "$OPTIONS" in
|
case "$OPTIONS" in
|
||||||
@ -84,19 +88,15 @@ while getopts 'fpagr' OPTIONS; do
|
|||||||
p)
|
p)
|
||||||
update_with_pacman_wrapper ;;
|
update_with_pacman_wrapper ;;
|
||||||
a)
|
a)
|
||||||
update_with_pacman_wrapper; update_with_flatpak ;;
|
update_with_flatpak; update_with_pacman_wrapper ;;
|
||||||
g)
|
g)
|
||||||
[[ $1 = -g ]] && update_with_pacman_wrapper
|
[[ $1 = -g ]] && update_with_pacman_wrapper
|
||||||
after_backup && echo -e "${IGREEN}after-backup complete${NC}" && sleep 3s && shutdown now
|
sleep 3s && shutdown now && exit 0;;
|
||||||
exit 0;;
|
|
||||||
r)
|
r)
|
||||||
[[ $1 = -r ]] && update_with_pacman_wrapper
|
[[ $1 = -r ]] && update_with_pacman_wrapper
|
||||||
after_backup && echo -e "${IGREEN}after-backup complete${NC}" && sleep 3s && reboot
|
sleep 3s && reboot && exit 0;;
|
||||||
exit 0;;
|
|
||||||
?)
|
?)
|
||||||
Help
|
Help
|
||||||
exit 1;;
|
exit 1;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
after_backup && echo -e "${IGREEN}after-backup complete${NC}"
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user