Compare commits

...

17 Commits

Author SHA1 Message Date
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 102 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! ***"s
else
echo "*** '$BACKUP' not found! ***"
exit 1
fi
exit 0

View File

@@ -415,6 +415,7 @@
function clean() {
clean-trash
clean-code
clean-backups
}
function_flatpak_usage: |
function flatpak-usage() {
@@ -767,6 +768,7 @@
alias_ls: |
alias l='ls'
alias ll='ls -alh'
alias lh='ls -ash'
alias_progs: |
alias progs='ps -ef'
alias_nethogs: |
@@ -782,45 +784,40 @@
}
function_backup: |
function backup {
EXTRA="$1"
TAG="System"
BASENAME="/tmp/Backup"
BACKUP="$BASENAME.`now`.$HOSTNAME.$TAG.zip"
echo "*** Removing Old Backups ***"
sh -c 'rm -fv "$BASENAME"*"$TAG"*''
echo "*** Creating backup at '$BACKUP' ***"
sudo zip -rv "$BACKUP" \
/etc /var/{log,mail,spool} /srv /boot \
/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
/usr/local/bin/backup_system.sh
}
alias backup-system="backup"
alias system-backup="backup"
alias check-backups="ll /srv/backup"
function clean-backup {
sh -c "rm -rfv /srv/backup/*"
}
alias clean-backups="clean-backup"
function_log: |
function log {
echo -e "$1"
}
function blog {
echo -e "\n\n$1\n\n"
}
function_debian_upgrade: |
function debian-upgrade {
typeset -l OLD NEW
OLD="$1"
NEW="$2"
echo "*** Upgrading from '$OLD' to '$NEW' ***"
blog "*** 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'. "
return 1
fi
echo "*** Running 'backup' before starting upgrade. ***"
blog "*** Running 'backup' before starting upgrade. ***"
sleep 1
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/*
echo -en "\nAre these the entries you'd like changed to '$NEW'? (y/N) "
@@ -828,12 +825,12 @@
read confirm_change
if [[ -z "$confirm_change" || "$confirm_change" == "N"* ]]; then
echo "*** Aborting upgrade, change rejected. ***"
blog "*** Aborting upgrade, change rejected. ***"
return 1
fi
unset confirm_change
echo "*** Continuing with upgrade. ***"
blog "*** 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" {} \;
@@ -848,27 +845,27 @@
read confirm_correct
if [[ -z "$confirm_correct" || "$confirm_correct" == "N"* ]]; then
echo "*** Aborting upgrade, confirmation rejected. ***"
blog "*** Aborting upgrade, confirmation rejected. ***"
return 1
fi
unset confirm_correct
echo "*** Starting the upgrade. ***"
blog "*** Starting the upgrade. ***"
sleep 1
sudo apt update
sudo apt upgrade --without-new-pkgs
echo "*** Upgrades complete, now add new packages. ***"
blog -e "*** Upgrades complete, now add new packages. ***"
sleep 1
sudo apt full-upgrade
echo "*** New packages complete, clean apt files. ***"
blog "*** New packages complete, clean apt files. ***"
sleep 1
sudo apt autoremove --purge
sudo apt clean
echo "*** All done! '$OLD' is now '$NEW'. Please reboot. ***"
blog "*** All done! '$OLD' is now '$NEW'. Please reboot. ***"
sleep 1
unset OLD NEW
}
@@ -881,6 +878,9 @@
}
alias ebooks="ebook-convert"
alias convert="ebook-convert"
alias_sudo: |
# Allows for alias expansions when using sudo, such as doing "sudo ll".
alias sudo="sudo "
- name: General | Account Management | Users | Files | Common Variable
set_fact:
@@ -952,8 +952,10 @@
{{ alias_ansible_facts }}
{{ function_now }}
{{ function_backup }}
{{ function_log }}
{{ function_debian_upgrade }}
{{ function_ebook_convert }}
{{ alias_sudo }}
- name: General | Account Management | Users | Files | .bashrc
blockinfile:

View File

@@ -1,12 +1,18 @@
---
# 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
cron:
user: ansible
name: "Ansible Sync"
minute: "*/30"
job: "sudo {{ ansible_pull_exec.stdout }} -v -o -U {{ git_repo_http }} --checkout {{ branch }}"
job: "sleep $(( $RANDOM / 20 )); sudo {{ ansible_pull_exec.stdout }} -v -o -U {{ git_repo_http }} --checkout {{ branch }}"
state: present
disabled: "{{ 'yes' if no_telem else 'no' }}"
@@ -14,7 +20,7 @@
cron:
user: ansible
name: "Ansible Weekly Forced Sync"
special_time: weekly
job: "{{ user_root.home }}/bin/scm.sh"
special_time: daily
job: "sleep $(( $RANDOM / 1 )); sudo {{ ansible_pull_exec.stdout }} -v -U {{ git_repo_http }} --checkout {{ branch }}"
state: present
disabled: "{{ 'yes' if no_telem else 'no' }}"

View File

@@ -123,4 +123,7 @@
## 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