Compare commits

..

2 Commits

Author SHA1 Message Date
91eb6be7c6
also lock pacman db in after_backup 2024-07-13 01:11:15 +02:00
c28fbf309e
switch from sudo to su
and moved some stuff around
2024-07-13 01:04:31 +02:00

36
update
View File

@ -1,6 +1,6 @@
#!/usr/bin/bash #!/usr/bin/bash
VERSION="3.0.0" VERSION="3.1"
IGREEN="\033[0;92m" # Intense Green IGREEN="\033[0;92m" # Intense Green
IYELLOW="\033[0;93m" # Intense Yellow IYELLOW="\033[0;93m" # Intense Yellow
IRED='\033[0;91m' # Red IRED='\033[0;91m' # Red
@ -22,16 +22,25 @@ check_for_valid_backup_location() {
fi fi
} }
[[ $BACKUP_LOCATION ]] && check_for_valid_backup_location "$BACKUP_LOCATION" || echo -e "${IRED}No BACKUP_LOCATION specified${NO_COLOR}" || exit 1 if [ $BACKUP_LOCATION ]; then
check_for_valid_backup_location "$BACKUP_LOCATION"
else
echo -e "${IRED}No BACKUP_LOCATION in $HOME/.config/update.conf specified${NO_COLOR}"
exit 1
fi
[[ $SECONDARY_BACKUP_LOCATION ]] && check_for_valid_backup_location "$SECONDARY_BACKUP_LOCATION" [[ $SECONDARY_BACKUP_LOCATION ]] && check_for_valid_backup_location "$SECONDARY_BACKUP_LOCATION"
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 --force /var/lib/pacman/db.lck > /dev/null 2>&1 if [ -f /var/lib/pacman/db.lck ]; then
rm --recursive --force "$TMP" > /dev/null 2>&1 echo -e "${IYELLOW}Trying to unlock the pacman db. Please enter your password.${NO_COLOR}"
rm --force "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst.new su --command="rm --force /var/lib/pacman/db.lck" > /dev/null 2>&1
fi
[[ -d $TMP ]] && rm --recursive --force "$TMP"
[[ -f "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new ]] && rm --force "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new
[[ -f "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst.new ]] && rm --force "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst.new
exit 1 exit 1
} }
@ -49,7 +58,14 @@ lock_pacman_db() {
echo -e "${IYELLOW}->${NO_COLOR} 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
echo -e "${IYELLOW}Trying to lock the pacman db. Please enter your password.${NO_COLOR}"
su --command="touch /var/lib/pacman/db.lck"
}
unlock_pacman_db() {
echo -e "${IYELLOW}Trying to unlock the pacman db. Please enter your password.${NO_COLOR}"
su --command="rm --force /var/lib/pacman/db.lck"
} }
delete_oldest_backup() { delete_oldest_backup() {
@ -60,6 +76,7 @@ delete_oldest_backup() {
before_backup() { before_backup() {
lock_pacman_db lock_pacman_db
[[ -d $TMP ]] && rm --recursive --force "$TMP" [[ -d $TMP ]] && rm --recursive --force "$TMP"
mkdir --parents "$TMP"/before-backup_"$DATE" "$TMP"/after-backup_"$DATE" mkdir --parents "$TMP"/before-backup_"$DATE" "$TMP"/after-backup_"$DATE"
@ -70,10 +87,12 @@ before_backup() {
mv "$TMP"/before-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION" mv "$TMP"/before-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"
[[ $SECONDARY_BACKUP_LOCATION ]] && mv "$TMP"/before-backup_"$DATE".tar.zst.new "$SECONDARY_BACKUP_LOCATION" [[ $SECONDARY_BACKUP_LOCATION ]] && mv "$TMP"/before-backup_"$DATE".tar.zst.new "$SECONDARY_BACKUP_LOCATION"
sudo rm --force /var/lib/pacman/db.lck unlock_pacman_db
} }
after_backup() { after_backup() {
lock_pacman_db
pacman --verbose --query > "$TMP"/after-backup_"$DATE"/pacman-after.txt pacman --verbose --query > "$TMP"/after-backup_"$DATE"/pacman-after.txt
[[ -x /usr/bin/flatpak ]] && flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt [[ -x /usr/bin/flatpak ]] && 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 tar --create --zstd --file "$TMP"/after-backup_"$DATE".tar.zst.new "$TMP"/after-backup_"$DATE" > /dev/null 2>&1
@ -88,6 +107,7 @@ after_backup() {
mv "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst mv "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst
[[ $SECONDARY_BACKUP_LOCATION ]] && mv "$SECONDARY_BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new "$SECONDARY_BACKUP_LOCATION"/before-backup_"$DATE".tar.zst [[ $SECONDARY_BACKUP_LOCATION ]] && mv "$SECONDARY_BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new "$SECONDARY_BACKUP_LOCATION"/before-backup_"$DATE".tar.zst
unlock_pacman_db
rm --recursive --force "$TMP" rm --recursive --force "$TMP"
} }
@ -115,6 +135,6 @@ if [[ ${1:0:2} == \-\- ]]; then
exit 0 exit 0
fi fi
before_backup && echo -e "${IGREEN}pre-backup complete${NO_COLOR}" before_backup && echo -e "${IGREEN}before-backup complete${NO_COLOR}"
update update
after_backup && echo -e "${IGREEN}after-backup complete${NO_COLOR}" after_backup && echo -e "${IGREEN}after-backup complete${NO_COLOR}"