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 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
Here is what is required.
```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/>
It requires sudo, but if you use something else, then just create a softlink for this script to work.
Doing that would look like this.
```bash
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
```
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
```bash
just install
make
```
## Usage
@ -44,6 +40,5 @@ no flag same as -p
--backup just does the before-backup without updating
```
## Contributing
## Contributing
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."

146
update
View File

@ -1,39 +1,61 @@
#!/usr/bin/bash
source "$HOME"/.config/update.conf || exit 1
[[ ! -d $BACKUP_LOCATION ]] && mkdir --parents "$BACKUP_LOCATION"
VERSION="2.0.9"
VERSION="2.1.1"
IGREEN="\033[0;92m" # Intense Green
IYELLOW="\033[0;93m" # Intense Yellow
NO_COLOR="\033[0m" # Text Reset
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
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
interrupt_function() {
echo "Interrupt has been detected"
[[ -f /var/lib/pacman/db.lck ]] && sudo rm /var/lib/pacman/db.lck > /dev/null 2>&1
rm --recursive "$TMP" > /dev/null 2>&1
[[ -f /var/lib/pacman/db.lck ]] && sudo rm --force /var/lib/pacman/db.lck > /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
}
help() {
echo "Usage: update [OPTION]"
echo
echo "options:"
echo "no flag same as -p"
echo "-f updates using flatpak update only"
echo "-p updates using a pacman-wrapper only"
echo "-a updates using flatpak update and a pacman-wrapper"
echo "-g shutdowns the computer afterwards"
echo "-r reboots the computer afterwards"
echo "--help displays this message"
echo "--preview shows a preview of which pkg's can be updated"
echo "--version prints out the version number"
echo "--backup just does the before-backup without updating"
echo "Usage: update [OPTION]"
echo
echo "options:"
echo "no option same as -p"
echo "-f updates using flatpak update only"
echo "-p updates using a pacman-wrapper only"
echo "-a updates using flatpak update and a pacman-wrapper"
echo "-g shutdowns the computer afterwards"
echo "-r reboots the computer afterwards"
echo "--help displays this message"
echo "--preview shows a preview of pkg's and flatpaks which can be updated"
echo "--version prints out the version number"
echo "--backup just does the before-backup without updating"
}
lock_pacman_db() {
@ -46,55 +68,68 @@ lock_pacman_db() {
}
delete_oldest_backup() {
if [[ $(ls -Ub "$BACKUP_LOCATION" | grep -c $1) -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 "$BACKUP_LOCATION"/"$OLDEST_FILE"
if [[ $(find $BACKUP_LOCATION -name '*$1*' -exec printf %c {} + | wc -c) -ge $BACKUP_AMOUNT ]]; then
rm --force $(find "$BACKUP_LOCATION" -name '*$1*' | sort -rn | head -1)
fi
}
before_backup() {
lock_pacman_db
delete_oldest_backup before-backup
[[ -d $TMP ]] && rm -r "$TMP"
[[ -d $TMP ]] && rm --recursive --force "$TMP"
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
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 rm /var/lib/pacman/db.lck
mv "$TMP"/before-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION"
mv "$BACKUP_LOCATION"/before-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION"/before-backup_"$DATE".tar.xz
pacman --verbose --query > "$TMP"/before-backup_"$DATE"/pacman-before.txt
[[ -x /usr/bin/flatpak ]] && flatpak list --all --show-details > "$TMP"/before-backup_"$DATE"/flatpak-before.txt
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 "$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() {
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
tar --create --xz --file "$TMP"/after-backup_"$DATE".tar.xz.new "$TMP"/after-backup_"$DATE" > /dev/null 2>&1
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
rm --recursive "$TMP"
mv "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst
[[ $SECONDARY_BACKUP_LOCATION ]] && mv "$SECONDARY_BACKUP_LOCATION"/after-backup_"$DATE".tar.zst.new "$SECONDARY_BACKUP_LOCATION"/after-backup_"$DATE".tar.zst
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 -Q > "$TMP"/after-backup_"$DATE"/pacman-after.txt
if [[ -x /usr/bin/flatpak ]]; then
flatpak update --assumeyes
flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt
fi
}
update_with_flatpak() {
flatpak update --assumeyes
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
help)
help ;;
preview)
"$PACMAN_WRAPPER" -Sy
"$PACMAN_WRAPPER" -Qu ;;
"$PACMAN_WRAPPER" -Syy
"$PACMAN_WRAPPER" --query --upgrades
[[ -x /usr/bin/flatpak ]] && flatpak remote-ls --updates ;;
version)
echo "$VERSON" ;;
echo "$VERSION" ;;
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;;
esac
@ -103,20 +138,23 @@ fi
before_backup && echo -e "${IGREEN}pre-backup complete${NO_COLOR}"
[[ $1 ]] || update_with_pacman_wrapper
[[ -z $1 ]] && update
while getopts 'fpagr' OPTIONS; do
case $OPTIONS in
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)
update_with_pacman_wrapper ;;
"$PACMAN_WRAPPER" ;;
a)
update_with_pacman_wrapper; update_with_flatpak ;;
update ;;
g)
[[ $1 = -g ]] && update_with_pacman_wrapper
[[ $1 == \-\g ]] && update
FINAL_COMMAND="systemctl --check-inhibitors=yes poweroff" ;;
r)
[[ $1 = -r ]] && update_with_pacman_wrapper
[[ $1 == \-\r ]] && update
FINAL_COMMAND="systemctl --check-inhibitors=yes reboot" ;;
?)
help; exit 1;;
@ -124,4 +162,4 @@ while getopts 'fpagr' OPTIONS; do
done
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_LOCATION=~/
# backup to this folder
BACKUP_LOCATION=~
# optional secondary backup loaction
#SECONDARY_BACKUP_LOCATION=~
# use this pacman-wrapper
PACMAN_WRAPPER=yay