From 86e3f40d365452caa01afa2ade4e63d066093058 Mon Sep 17 00:00:00 2001 From: Hyperling Date: Fri, 22 Aug 2025 13:43:40 -0700 Subject: [PATCH] Added and tested `now` and `backup`. Still need to test `debian-upgrade`. --- tasks/general/acct_mgmt/users.yml | 91 +++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/tasks/general/acct_mgmt/users.yml b/tasks/general/acct_mgmt/users.yml index fb65e74..4fa49e6 100644 --- a/tasks/general/acct_mgmt/users.yml +++ b/tasks/general/acct_mgmt/users.yml @@ -743,6 +743,94 @@ alias nethogs='sudo nethogs' alias_ansible_facts: | alias ansible-facts='ansible localhost -m setup --connection=local' + function_now: | + function now { + date "+%Y%m%d_%H%M%S" + } + function_backup: | + function backup { + EXTRA="$1" + BACKUP_DIR="/tmp" + BACKUP_FILENAME="Backup-`now`.zip" + BACKUP="$BACKUP_DIR/$BACKUP_FILENAME" + echo "*** Creating backup at '$BACKUP' ***" + sudo zip -rv "$BACKUP" \ + /etc/apt /var/{log,mail} \ + /etc/crontab /etc/cron.d /var/spool/cron \ + /usr/local/etc $EXTRA + status="$?" + if [[ "$status" != 0 ]]; then + echo "*** ERROR: Failed to create '$BACKUP', file may be incorrect. ***" + fi + if [[ -e "$BACKUP" ]]; then + ls -alh "$BACKUP" + echo "*** '$BACKUP' created successfully! ***" + else + echo "*** '$BACKUP' not found! ***" + fi + } + function_debian_upgrade: | + function debian-upgrade { + typeset -l OLD NEW + OLD="$1" + NEW="$2" + echo "*** Upgrading from '$OLD' to '$NEW' ***" + if [[ -z "$OLD" || -z "$NEW" ]]; then + echo -n "ERROR: Please pass the OLD and NEW version names" + echo ", such as 'debian-upgrade bookworm trixie'. " + fi + + echo "*** Doing an 'update' and 'backup' before starting upgrade. ***" + + update + + backup + + echo "*** Listing the current apt listing for '$OLD' ***" + sudo grep "$OLD" /apt/sources.list /apt/sources.list.d/* + + echo -en "\nAre these the entries you'd like changed to '$NEW'? (y/N) " + typeset -u confirm_change + read confirm_change + + if [[ -z "$confirm_change" || "$confirm_change" == "N"* ]]; then + echo "*** Aborting upgrade, change rejected. ***" + fi + unset confirm_change + + echo "*** Continuing with upgrade. ***" + sudo sed -i 's/$OLD/$NEW/g' /etc/apt/sources.list + find /etc/apt/sources.list.d -name "*.list" \ + -exec sudo sed -i 's/$OLD/$NEW/g' {} \; + + sudo grep $OLD /apt/sources.list /apt/sources.list.d/* + sudo grep $NEW /apt/sources.list /apt/sources.list.d/* + + echo -en "\nDo the source files look correct? (y/N) " + typeset -u confirm_correct + read confirm_correct + + if [[ -z "$confirm_correct" || "$confirm_correct" == "N"* ]]; then + echo "*** Aborting upgrade, confirmation rejected. ***" + fi + unset confirm_correct + + echo "*** Starting the upgrade. ***" + sleep 3 + + #sudo apt update + #sudo apt upgrade --without-new-pkgs + #sudo apt full-upgrade + + echo "*** Upgrade complete, cleaning apt files. ***" + + #sudo apt autoremove --purge + #sudo apt clean + + echo "*** All done! '$OLD' is now '$NEW'. Please reboot. ***" + unset OLD NEW + } + alias upgrade-debian="debian-upgrade" - name: General | Account Management | Users | Files | Common Variable set_fact: @@ -811,6 +899,9 @@ {{ alias_progs }} {{ alias_nethogs }} {{ alias_ansible_facts }} + {{ function_now }} + {{ function_backup }} + {{ function_debian_upgrade }} - name: General | Account Management | Users | Files | .bashrc blockinfile: