Compare commits

..

36 Commits

Author SHA1 Message Date
0d3931d738 Fix cronjobs to use BASH for RANDOM variable. 2025-10-19 21:41:59 -07:00
7c93f25a5d Add output for why dev branch shows up twice during load-prod. 2025-10-15 06:45:49 -07:00
8445b64d6b Check if the dev branch exists before moving to it. 2025-10-15 06:42:16 -07:00
8dfb3fe2a6 Change rollback aliases to only go down 1 layer. Add reset aliases for going all the way back to main. 2025-10-15 06:38:50 -07:00
5b749d2100 Fix failed branch switch. Hardcode to dev! 2025-10-15 06:35:02 -07:00
57ca267aa5 Add back the display of the current local branch we're on. Add newlines for readability. 2025-10-15 06:32:28 -07:00
2ca308c287 Re-do changes lost between branch pointing. 2025-10-15 06:28:38 -07:00
40ee72b587 Only continue each commnd if they are successful. 2025-10-15 06:15:13 -07:00
13bc98d7ef Further enhancments to load-branch. Better output and branch choices. 2025-10-15 06:06:05 -07:00
d916637798 Complete the conditional branches. 2025-10-15 05:53:48 -07:00
71d236d0ba Add aliases to roll branches back to main, AKA the last official pull request. 2025-10-15 05:48:18 -07:00
d81c424878 Allow using load-prod and load-stage with any branch, and default to dev if nothing is provided. 2025-10-15 05:47:01 -07:00
73e527640f Add heads back onto the ls-remote command. 2025-10-15 05:41:12 -07:00
aa3075a9fc Do not do switch back to sender if did not switch to receiver. 2025-10-15 05:33:17 -07:00
89cdf8157f Add other missing then. 2025-10-15 05:19:57 -07:00
4e0ff86295 Add missing then. 2025-10-15 05:19:21 -07:00
305b8df6ff Ensure user is on dev branch if it exists. 2025-10-15 05:09:56 -07:00
6a78933875 Add helper for reorienting a git branch. 2025-10-15 04:52:15 -07:00
7095636915 Remove trailing s. 2025-10-14 15:03:43 -07:00
80bba3b21b Merge pull request 'Fixes & Enhancements' (#6) from dev into main
Reviewed-on: #6
2025-10-14 15:01:20 -07:00
d98bbb02a9 Quote the backup contents with an * so that backups get ignored properly. 2025-10-14 12:21:38 -07:00
00c87328ab Don't back up backup files. 2025-10-14 11:16:52 -07:00
b4cdea984e Add sleep to ansible-pull commands. Change weekly force to daily. Use explicit command rather than rarely used file. 2025-10-14 11:09:24 -07:00
4651726268 Do a full truncate of backup directory so that partial zips with weird hash names and no file extension get deleted as well. 2025-10-14 08:36:53 -07:00
3410cf4426 Ensure backup's TRASH folder gets created and used properly. 2025-10-14 08:35:21 -07:00
d5393f60f3 Add cleanup for backups. 2025-10-14 08:34:33 -07:00
5f882df33f Add ability to use aliases with sudo. 2025-10-14 08:22:16 -07:00
0dfbd0bd68 Add ls alias for only showing size, not all the file data. 2025-10-14 08:18:20 -07:00
0d5b095ad6 Remove directory from basename variable. 2025-10-14 08:14:47 -07:00
07e32decf8 Fixes and enhancements for system backups. 2025-10-14 08:12:19 -07:00
afc3b20785 Reference script version of backup with function and add aliases. 2025-10-14 07:44:52 -07:00
ed160da316 Move backup function into script so that cron can access it. 2025-10-14 07:44:26 -07:00
2f08b1414e Use more standardized backup directory, outside of RAM. 2025-10-14 07:07:28 -07:00
c279884da2 Fix extra '. Really need to permanently turn off autocompletion type stuff, it keeps coming back on. 2025-10-09 13:30:06 -07:00
df8b330893 Add and use functions for simpler and more readable logging. 2025-10-09 12:38:56 -07:00
08a7a22dde Add to TBD. 2025-10-09 12:27:46 -07:00
4 changed files with 175 additions and 34 deletions

57
files/scripts/backup_system.sh Executable file
View File

@@ -0,0 +1,57 @@
#!/usr/bin/env bash
## Variables ##
if [[ -z "$HOSTNAME" ]]; then
echo "*** Pulling Hostname From Command ***"
export HOSTNAME="`hostname`"
fi
if [[ -z "$HOSTNAME" ]]; then
echo "*** Pulling Hostname From /etc/ File ***"
export HOSTNAME="`cat /etc/hostname`"
fi
EXTRA="$1"
TAG="System"
DATE="`date "+%Y%m%d-%H%M%S"`"
BACKUP_DIR="/srv/backup"
BASENAME="Backup"
BACKUP="$BACKUP_DIR/$BASENAME.$DATE.$HOSTNAME.$TAG.zip"
## Initialization ##
echo "*** Creating Backup Directory ***"
sudo mkdir -pv "$BACKUP_DIR"
sudo mkdir -pv "$BACKUP_DIR/TRASH"
sudo chmod -Rv 775 "$BACKUP_DIR"
sudo chown -Rv root:root "$BACKUP_DIR"
## Cleanup ##
echo "*** Removing Old Backups ***"
cd "$BACKUP_DIR"
sudo mv -v "$BASENAME"*"$TAG"* TRASH/
sudo rm -v TRASH/*
## Main
echo "*** Creating backup at '$BACKUP' ***"
sudo zip -rv "$BACKUP" \
/etc /var/{log,mail,spool} /srv /boot \
/usr/local/etc $EXTRA \
-x "/srv/backup/*"
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! ***"
exit 1
fi
exit 0

View File

@@ -415,6 +415,7 @@
function clean() { function clean() {
clean-trash clean-trash
clean-code clean-code
clean-backups
} }
function_flatpak_usage: | function_flatpak_usage: |
function flatpak-usage() { function flatpak-usage() {
@@ -563,6 +564,16 @@
echo -e "\n*** `basename $project` ***" echo -e "\n*** `basename $project` ***"
cd $project cd $project
if [[ -d .git ]]; then if [[ -d .git ]]; then
git ls-remote --exit-code --heads origin dev
dev_exists="$?"
if [[ "$dev_exists" == 0 ]]; then
git switch dev
elif [[ "$dev_exists" == 2 ]]; then
git switch main
else
echo "ERROR: Unknown status for dev_exists, '$dev_exists'."
continue
fi
git pull git pull
git push git push
else else
@@ -767,6 +778,7 @@
alias_ls: | alias_ls: |
alias l='ls' alias l='ls'
alias ll='ls -alh' alias ll='ls -alh'
alias lh='ls -ash'
alias_progs: | alias_progs: |
alias progs='ps -ef' alias progs='ps -ef'
alias_nethogs: | alias_nethogs: |
@@ -782,45 +794,40 @@
} }
function_backup: | function_backup: |
function backup { function backup {
EXTRA="$1" /usr/local/bin/backup_system.sh
TAG="System" }
BASENAME="/tmp/Backup" alias backup-system="backup"
BACKUP="$BASENAME.`now`.$HOSTNAME.$TAG.zip" alias system-backup="backup"
echo "*** Removing Old Backups ***" alias check-backups="ll /srv/backup"
sh -c 'rm -fv "$BASENAME"*"$TAG"*'' function clean-backup {
echo "*** Creating backup at '$BACKUP' ***" sh -c "rm -rfv /srv/backup/*"
sudo zip -rv "$BACKUP" \ }
/etc /var/{log,mail,spool} /srv /boot \ alias clean-backups="clean-backup"
/usr/local/etc $EXTRA function_log: |
status="$?" function log {
if [[ "$status" != 0 ]]; then echo -e "$1"
echo "*** ERROR: Failed to create '$BACKUP', file may be incorrect. ***" }
fi function blog {
if [[ -e "$BACKUP" ]]; then echo -e "\n\n$1\n\n"
ls -alh "$BACKUP"
echo "*** '$BACKUP' created successfully! ***"
else
echo "*** '$BACKUP' not found! ***"
fi
} }
function_debian_upgrade: | function_debian_upgrade: |
function debian-upgrade { function debian-upgrade {
typeset -l OLD NEW typeset -l OLD NEW
OLD="$1" OLD="$1"
NEW="$2" NEW="$2"
echo "*** Upgrading from '$OLD' to '$NEW' ***" blog "*** Upgrading from '$OLD' to '$NEW' ***"
if [[ -z "$OLD" || -z "$NEW" ]]; then if [[ -z "$OLD" || -z "$NEW" ]]; then
echo -n "ERROR: Please pass the OLD and NEW version names" echo -n "ERROR: Please pass the OLD and NEW version names"
echo ", such as 'debian-upgrade bookworm trixie'. " echo ", such as 'debian-upgrade bookworm trixie'. "
return 1 return 1
fi fi
echo "*** Running 'backup' before starting upgrade. ***" blog "*** Running 'backup' before starting upgrade. ***"
sleep 1 sleep 1
backup backup
echo "*** Listing the current apt listing for '$OLD' ***" blog "*** Listing the current apt listing for '$OLD' ***"
sudo grep "$OLD" /etc/apt/sources.list /etc/apt/sources.list.d/* sudo grep "$OLD" /etc/apt/sources.list /etc/apt/sources.list.d/*
echo -en "\nAre these the entries you'd like changed to '$NEW'? (y/N) " echo -en "\nAre these the entries you'd like changed to '$NEW'? (y/N) "
@@ -828,12 +835,12 @@
read confirm_change read confirm_change
if [[ -z "$confirm_change" || "$confirm_change" == "N"* ]]; then if [[ -z "$confirm_change" || "$confirm_change" == "N"* ]]; then
echo "*** Aborting upgrade, change rejected. ***" blog "*** Aborting upgrade, change rejected. ***"
return 1 return 1
fi fi
unset confirm_change unset confirm_change
echo "*** Continuing with upgrade. ***" blog "*** Continuing with upgrade. ***"
sudo sed -i "s/$OLD/$NEW/g" /etc/apt/sources.list sudo sed -i "s/$OLD/$NEW/g" /etc/apt/sources.list
find /etc/apt/sources.list.d/ -name "*.list" \ find /etc/apt/sources.list.d/ -name "*.list" \
-exec sudo sed -i "s/$OLD/$NEW/g" {} \; -exec sudo sed -i "s/$OLD/$NEW/g" {} \;
@@ -848,27 +855,27 @@
read confirm_correct read confirm_correct
if [[ -z "$confirm_correct" || "$confirm_correct" == "N"* ]]; then if [[ -z "$confirm_correct" || "$confirm_correct" == "N"* ]]; then
echo "*** Aborting upgrade, confirmation rejected. ***" blog "*** Aborting upgrade, confirmation rejected. ***"
return 1 return 1
fi fi
unset confirm_correct unset confirm_correct
echo "*** Starting the upgrade. ***" blog "*** Starting the upgrade. ***"
sleep 1 sleep 1
sudo apt update sudo apt update
sudo apt upgrade --without-new-pkgs sudo apt upgrade --without-new-pkgs
echo "*** Upgrades complete, now add new packages. ***" blog -e "*** Upgrades complete, now add new packages. ***"
sleep 1 sleep 1
sudo apt full-upgrade sudo apt full-upgrade
echo "*** New packages complete, clean apt files. ***" blog "*** New packages complete, clean apt files. ***"
sleep 1 sleep 1
sudo apt autoremove --purge sudo apt autoremove --purge
sudo apt clean sudo apt clean
echo "*** All done! '$OLD' is now '$NEW'. Please reboot. ***" blog "*** All done! '$OLD' is now '$NEW'. Please reboot. ***"
sleep 1 sleep 1
unset OLD NEW unset OLD NEW
} }
@@ -881,6 +888,71 @@
} }
alias ebooks="ebook-convert" alias ebooks="ebook-convert"
alias convert="ebook-convert" alias convert="ebook-convert"
alias_sudo: |
# Allows for alias expansions when using sudo, such as doing "sudo ll".
alias sudo="sudo "
function_load_branch: |
# Change a branch to point at another.
function load-branch {
rcvr="$1" # branch which we want to modify
base="$2" # branch with the changes we want
if [[ "$rcvr" == "main" ]]; then
echo "*** ERROR: Are you nuts, fool!? Not main! Do it manually! ***"
exit 1
fi
if [[ -z "$base" ]]; then
if [[ "$rcvr" == "prod" ]]; then
base="stage"
elif [[ "$rcvr" == "stage" ]]; then
base="dev"
else
echo "* Unsure which branch to use for '$rcvr'. Please specify."
exit 1
fi
echo "* Base was not specified, using '$base' for '$rcvr'."
else
echo "* Requested pointing '$rcvr' branch to '$base'."
fi
echo -e "\n* Ensuring we are on base branch '$base'." &&
git switch "$base" &&
echo -e "\n* Changing pointer for '$rcvr' to '$base'." &&
git branch -f "$rcvr" "$base" &&
#echo -e "\n* Switching to branch '$rcvr'." &&
#git switch "$rcvr" &&
echo -e "\n* Forcing push on branch '$rcvr'." &&
git push --force --set-upstream origin "$rcvr" &&
echo -e "\n* Done! Displaying list of remote branches." &&
git ls-remote --heads | sort
if [[ "$base" != "dev" ]]; then
echo -e "\n* Checking if a dev branch exists."
git ls-remote --exit-code --heads origin dev
dev_exists="$?"
if [[ "$dev_exists" == 0 ]]; then
echo -e "\n* Switching back to dev."
git switch dev
elif [[ "$dev_exists" == 2 ]]; then
echo -e "\n* Switching back to main."
git switch main
else
echo "*** ERROR: Unknown status for dev_exists, '$dev_exists'. ***"
fi
fi
echo -e "\n* Verifying which branch we're on."
git branch
}
alias load-prod="load-branch prod"
alias load-stage="load-branch stage"
alias rollback-prod="load-branch prod main"
alias rollback-stage="load-branch stage prod"
alias rollback-dev="load-branch dev stage"
alias reset-prod="load-branch prod main"
alias reset-stage="load-branch stage main"
alias reset-dev="load-branch dev main"
- name: General | Account Management | Users | Files | Common Variable - name: General | Account Management | Users | Files | Common Variable
set_fact: set_fact:
@@ -952,8 +1024,11 @@
{{ alias_ansible_facts }} {{ alias_ansible_facts }}
{{ function_now }} {{ function_now }}
{{ function_backup }} {{ function_backup }}
{{ function_log }}
{{ function_debian_upgrade }} {{ function_debian_upgrade }}
{{ function_ebook_convert }} {{ function_ebook_convert }}
{{ alias_sudo }}
{{ function_load_branch }}
- name: General | Account Management | Users | Files | .bashrc - name: General | Account Management | Users | Files | .bashrc
blockinfile: blockinfile:

View File

@@ -1,12 +1,18 @@
--- ---
# Jobs relating to the ansible user. # Jobs relating to the ansible user.
# $RANDOM is anywhere from 0 to 32767 in BASH. This would be up to ~9 hours.
# $RANDOM / 10 can be up to about 3200 seconds, or ~55 minutes.
# $RANDOM / 20 can be up to about 1600 seconds, or ~27 minutes.
# $RANDOM / 50 can be up to about 650 seconds, or ~11 minutes.
# $RANDOM / 100 can be up to about 320 seconds, or ~5 minutes.
# This prevents multiple systems from hitting it at the exact same time.
- name: General | Cron | Ansible | Create Subscriber Job - name: General | Cron | Ansible | Create Subscriber Job
cron: cron:
user: ansible user: ansible
name: "Ansible Sync" name: "Ansible Sync"
minute: "*/30" minute: "*/30"
job: "sudo {{ ansible_pull_exec.stdout }} -v -o -U {{ git_repo_http }} --checkout {{ branch }}" job: "bash -c 'sleep $(( $RANDOM / 20 )); sudo {{ ansible_pull_exec.stdout }} -v -o -U {{ git_repo_http }} --checkout {{ branch }}'"
state: present state: present
disabled: "{{ 'yes' if no_telem else 'no' }}" disabled: "{{ 'yes' if no_telem else 'no' }}"
@@ -14,7 +20,7 @@
cron: cron:
user: ansible user: ansible
name: "Ansible Weekly Forced Sync" name: "Ansible Weekly Forced Sync"
special_time: weekly special_time: daily
job: "{{ user_root.home }}/bin/scm.sh" job: "bash -c 'sleep $(( $RANDOM / 1 )); sudo {{ ansible_pull_exec.stdout }} -v -U {{ git_repo_http }} --checkout {{ branch }}'"
state: present state: present
disabled: "{{ 'yes' if no_telem else 'no' }}" disabled: "{{ 'yes' if no_telem else 'no' }}"

View File

@@ -123,4 +123,7 @@
## Timezone ## ## Timezone ##
# TBD/TODO: Add a field to config file which gets applied via timedatectl and /etc/localtime. # TBD/TODO:
# Add a field to general.yml config file which gets applied via timedatectl and /etc/localtime.
# Example of what to do to file:
# /etc/localtime -> ../usr/share/zoneinfo/America/Phoenix