Implemented secondary backup location #25

This commit is contained in:
ProfessionalUwU 2024-07-07 23:34:27 +02:00
commit 3c2ecc72ae
Signed by: ProfessionalUwU
GPG Key ID: 9F28CB1645C4BFB5
4 changed files with 112 additions and 66 deletions

View File

@ -1,14 +1,10 @@
# Update # Update
Update script written in Bash for Arch Linux only. Keeps all your pacman and aur packages as well as your flatpaks up to date with one simple script. It also backups a list of all your pacman and aur packages and flatpaks. Update script written in Bash for Arch Linux only. Keeps all your official, aur packages and your flatpaks up to date with one simple script. It also backups a list of all your installed packages and flatpaks.
## Requirements ## Requirements
Here is what is required. It requires sudo, but if you use something else, then just create a softlink for this script to work.
```bash
pacman -S just
```
It also technically requires sudo, but if you use something else, then just create a softlink for this script to work.<br/>
Doing that would look like this. Doing that would look like this.
```bash ```bash
ln -s /usr/bin/your_program /usr/bin/sudo ln -s /usr/bin/your_program /usr/bin/sudo
@ -20,10 +16,10 @@ ln -s /usr/bin/your_program /usr/bin/sudo
git clone https://gitea.hopeless-cloud.xyz/AustrianToast/update.git && cd update git clone https://gitea.hopeless-cloud.xyz/AustrianToast/update.git && cd update
``` ```
Before installing, please edit the config and configure it to your liking.<br/> Before installing, please edit the config and configure it to your liking.
Then install using Then install using
```bash ```bash
just install make
``` ```
## Usage ## Usage
@ -45,5 +41,4 @@ no flag same as -p
``` ```
## Contributing ## Contributing
Contributions are always welcome! Contributions are always welcome!

10
makefile Normal file
View File

@ -0,0 +1,10 @@
install:
@echo "==> Installing update into /usr/local/bin"
@sudo install -Dm755 update /usr/local/bin/update
@[[ -f ${HOME}/.config/update.conf ]] || cp update.conf ${HOME}/.config/
@echo "==> Finished."
uninstall:
@echo "==> Uninstalling update from /usr/local/bin"
@sudo rm /usr/local/bin/update ${HOME}/.config/update.conf
@echo "==> Finished."

120
update
View File

@ -1,22 +1,44 @@
#!/usr/bin/bash #!/usr/bin/bash
source "$HOME"/.config/update.conf || exit 1 VERSION="2.1.1"
[[ ! -d $BACKUP_LOCATION ]] && mkdir --parents "$BACKUP_LOCATION"
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
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"
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 [ $SECONDARY_BACKUP_LOCATION ]; then
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
fi
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 > /dev/null 2>&1 [[ -f /var/lib/pacman/db.lck ]] && sudo rm --force /var/lib/pacman/db.lck > /dev/null 2>&1
rm --recursive "$TMP" > /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 exit 1
} }
@ -24,14 +46,14 @@ help() {
echo "Usage: update [OPTION]" echo "Usage: update [OPTION]"
echo echo
echo "options:" echo "options:"
echo "no flag same as -p" echo "no option same as -p"
echo "-f updates using flatpak update only" echo "-f updates using flatpak update only"
echo "-p updates using a pacman-wrapper only" echo "-p updates using a pacman-wrapper only"
echo "-a updates using flatpak update and a pacman-wrapper" echo "-a updates using flatpak update and a pacman-wrapper"
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,55 +68,68 @@ 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
[[ -f /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 [[ -x /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"
[[ $SECONDARY_BACKUP_LOCATION ]] && mv "$TMP"/before-backup_"$DATE".tar.zst.new "$SECONDARY_BACKUP_LOCATION"
sudo rm --force /var/lib/pacman/db.lck
} }
after_backup() { after_backup() {
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
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"
[[ $SECONDARY_BACKUP_LOCATION ]] && mv "$TMP"/before-backup_"$DATE".tar.zst.new "$SECONDARY_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" [[ $SECONDARY_BACKUP_LOCATION ]] && mv "$SECONDARY_BACKUP_LOCATION"/after-backup_"$DATE".tar.zst.new "$SECONDARY_BACKUP_LOCATION"/after-backup_"$DATE".tar.zst
mv "$BACKUP_LOCATION"/after-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION"/after-backup_"$DATE".tar.xz
rm --recursive "$TMP" delete_oldest_backup before-backup
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
rm --recursive --force "$TMP"
} }
update_with_pacman_wrapper() { update() {
"$PACMAN_WRAPPER" "$PACMAN_WRAPPER"
pacman -Q > "$TMP"/after-backup_"$DATE"/pacman-after.txt if [[ -x /usr/bin/flatpak ]]; then
}
update_with_flatpak() {
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
} }
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 ;;
version) version)
echo "$VERSON" ;; 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
[[ $SECONDARY_BACKUP_LOCATION ]] && mv "$SECONDARY_BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new "$SECONDARY_BACKUP_LOCATION"/before-backup_"$DATE".tar.zst
rm --recursive --force "$TMP" ;;
?) ?)
help; exit 1;; help; exit 1;;
esac esac
@ -103,20 +138,23 @@ fi
before_backup && echo -e "${IGREEN}pre-backup complete${NO_COLOR}" before_backup && echo -e "${IGREEN}pre-backup complete${NO_COLOR}"
[[ $1 ]] || update_with_pacman_wrapper [[ -z $1 ]] && update
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)
[[ $1 = -g ]] && update_with_pacman_wrapper [[ $1 == \-\g ]] && update
FINAL_COMMAND="systemctl --check-inhibitors=yes poweroff" ;; FINAL_COMMAND="systemctl --check-inhibitors=yes poweroff" ;;
r) r)
[[ $1 = -r ]] && update_with_pacman_wrapper [[ $1 == \-\r ]] && update
FINAL_COMMAND="systemctl --check-inhibitors=yes reboot" ;; FINAL_COMMAND="systemctl --check-inhibitors=yes reboot" ;;
?) ?)
help; exit 1;; help; exit 1;;
@ -124,4 +162,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

View File

@ -1,5 +1,8 @@
# Backup to this folder # backup to this folder
BACKUP_LOCATION=~/ BACKUP_LOCATION=~
# optional secondary backup loaction
#SECONDARY_BACKUP_LOCATION=~
# use this pacman-wrapper # use this pacman-wrapper
PACMAN_WRAPPER=yay PACMAN_WRAPPER=yay