QOL Improvement (#22)

Added some sanity checks inside the script. Rewrote some parts in the README.md. Completely removed the makefile, because I don't fell like supporting it anymore.

Co-authored-by: ProfessionalUwU <andre.fuhry@hopeless-cloud.xyz>
Reviewed-on: #22
Reviewed-by: ProfessionalUwU <andre.fuhry@hopeless-cloud.xyz>
Co-authored-by: AustrianToast <austriantoast@hopeless-cloud.xyz>
Co-committed-by: AustrianToast <austriantoast@hopeless-cloud.xyz>
This commit is contained in:
AustrianToast 2023-12-01 17:13:10 +01:00 committed by Gitea
parent 5d3cefb4ac
commit ea112d4ceb
Signed by: Gitea
GPG Key ID: FCACD432A6DD0FB5
4 changed files with 63 additions and 68 deletions

View File

@ -1,21 +1,28 @@
# Update # Update
Update script written in bash for Arch. 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/aur pkg's and flatpaks. As well as your fstab and the makepkg.conf to the location that you specify in the config. 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.
## 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/>
Doing that would look like this.
```bash
ln -s /usr/bin/your_program /usr/bin/sudo
```
## Installation ## Installation
```bash ```bash
git clone https://github.com/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. Before installing, please edit the config and configure it to your liking.<br/>
Then install using Then install using
```bash ```bash
make install
```
or
```bash
just install just install
``` ```
@ -34,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

View File

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

View File

@ -1,10 +0,0 @@
install:
@echo "==> Installing update into /usr/local/bin"
@sudo install -Dm755 update /usr/local/bin/update
@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."

94
update Normal file → Executable file
View File

@ -1,29 +1,26 @@
#!/usr/bin/bash #!/usr/bin/bash
# updates using a pacman-wrapper and flatpak-update with flags source "$HOME"/.config/update.conf || exit 1
[[ ! -d $BACKUP_LOCATION ]] && mkdir --parents "$BACKUP_LOCATION"
source "$HOME"/.config/update.conf VERSION="2.0.9"
VER="2.0.7"
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 https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
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
interrupt_function() { interrupt_function() {
echo "Interrupt has been detected" echo "Interrupt has been detected"
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:"
@ -36,74 +33,75 @@ 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 -Qq > "$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() {
"$PACMAN_WRAPPER" "$PACMAN_WRAPPER"
pacman -Qq > "$TMP"/after-backup_"$DATE"/pacman-after.txt pacman -Q > "$TMP"/after-backup_"$DATE"/pacman-after.txt
} }
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)
sudo pacman -Sy > /dev/null 2>&1 && sudo pacman -Qu ;; "$PACMAN_WRAPPER" -Sy
"$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
@ -116,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="shutdown now" ;; FINAL_COMMAND="systemctl --check-inhibitors=yes poweroff" ;;
r) r)
[[ $1 = -r ]] && update_with_pacman_wrapper [[ $1 = -r ]] && update_with_pacman_wrapper
FINAL_COMMAND="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