Compare commits

..

55 Commits

Author SHA1 Message Date
0920493634 Create the config directory so that tee can create the files. 2025-10-25 15:46:52 -07:00
03b8169401 Try adding the output to the files in a different way. 2025-10-25 15:38:12 -07:00
cfc2694737 Add that the file contains test data. Add output of the function for testing. 2025-10-25 15:30:40 -07:00
fca20b31bd Change wording so that user does not expect files to exist right away. 2025-10-25 15:20:59 -07:00
1f4abd2d6c Create the filepaths for the config files. Output the files and their contents after creation. 2025-10-25 15:18:55 -07:00
a42d1d05df Fix the parameter switches. 2025-10-25 15:16:50 -07:00
07d1adc926 Create the proper contents in each test file. 2025-10-25 15:15:30 -07:00
71ca410123 Add ability to spawn basic configurations for testing. 2025-10-25 15:12:03 -07:00
c46c78519b Do not mention using the default branch if using local. 2025-10-25 14:58:03 -07:00
816a0d4f69 Do not check the kernelfor the OS when inside of Docker containers. 2025-10-25 14:43:14 -07:00
f8a60b4287 Change final backup check to use du again, not ls -l. 2025-10-24 15:26:40 -07:00
a7f395a62a Remove test output. 2025-10-24 15:18:44 -07:00
f02aec2cdb Fix case of DOCKER value. 2025-10-24 15:16:57 -07:00
a94959bcf9 Add more verbosity. Change "BACKUP" to "DOCKER". 2025-10-24 15:15:05 -07:00
5004250f85 Add aliases for reloading shell like in Termux environment. 2025-10-24 14:55:11 -07:00
4c4d4bbef7 Additional enhancements to pulling Production backups. 2025-10-24 14:53:36 -07:00
d32b636a99 Add more output. 2025-10-23 20:02:52 -07:00
a7083733cc Add check on whether any zips were successful. 2025-10-23 19:59:56 -07:00
97ddcc4573 Properly handle removing the failed files. 2025-10-23 19:50:57 -07:00
dc2ae57bd6 Add command to pull production backups while making sure that failed downloads are easily identified. 2025-10-23 19:42:50 -07:00
82ab31b9ea Add Grayjay to GNOME favorites. 2025-10-20 19:33:09 -07:00
49ababedc5 Add the flatpak Grayjay. 2025-10-20 19:32:49 -07:00
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
6 changed files with 294 additions and 25 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

@@ -9,13 +9,18 @@ LOCAL=$DIR/local.yml
URL="https://git.hyperling.com/me/env-ansible"
BRANCH="main"
config_dir="/usr/local/etc/hyperling-scm"
general_config="$config_dir/general.ini"
workstation_config="$config_dir/workstation.ini"
server_config="$config_dir/server.ini"
## Functions ##
# Accepts 1 parameter, it is used as the exit status.
function usage {
cat <<- EOF
$PROG [-l] [-b branch_name] [-h]
$PROG [-l] [-b branch_name] [-g] [-w] [-s] [-h]
Program to initialize synchronization with Hyperling's Ansible configuration.
$URL
@@ -23,7 +28,10 @@ function usage {
-l : Run the local playbook associated with this $PROG.
This is helpful for development or just saving bandwidth.
It also provides prettier colors than the plaintext from ansible-pull. ;)
-b branch_name: Download and run a specific branch. Default is $BRANCH.
-b branch_name : Download and run a specific branch. Default is $BRANCH.
-g : Enable the General config with test contents.
-w : Enable the Workstation config with test contents.
-s : Enable the Server config with test contents.
-h : Display this help text
EOF
@@ -32,10 +40,13 @@ function usage {
## Parameter Parsing ##
while getopts ":lb:h" arg; do
while getopts ":lb:gwsh" arg; do
case $arg in
l) local="Y" && echo "Running $LOCAL as the playbook." ;;
b) branch="$OPTARG" && echo "Using branch $branch instead of $BRANCH." ;;
g) create_general="Y" && echo "Requested '$general_config'." ;;
w) create_workstation="Y" && echo "Requested '$workstation_config'." ;;
s) create_server="Y" && echo "Requested '$server_config'." ;;
h) usage ;;
*) echo "ERROR: Parameter $OPTARG was not recognized." && usage 1 ;;
esac
@@ -47,15 +58,32 @@ if [[ ! -z $1 && $1 != "-"* ]]; then
usage 1
fi
if [[ $branch == "" ]]; then
if [[ -z "$local" && "$branch" == "" ]]; then
echo "Using default branch $BRANCH."
branch="$BRANCH"
fi
if [[ -n "$create_general" && -f "$general_config" ]]; then
echo "WARNING: General configuration already exists, will not overwrite."
ls -lh "$general"
fi
if [[ -n "$create_workstation" && -f "$workstation_config" ]]; then
echo "WARNING: Workstation configuration already exists, will not overwrite."
ls -lh "$workstation_config"
fi
if [[ -n "$create_server" && -f "$server_config" ]]; then
echo "WARNING: Server configuration already exists, will not overwrite."
ls -lh "$server_config"
fi
## Main ##
os="$(cat /etc/os-release)"
os="$os $(uname -a)"
if [[ ! -f /.dockerenv ]]; then
# If we are not in a Docker container, also check what the kernel says.
os="$os $(uname -a)"
# Docker containers use the host kernel which gives an incorrect reading.
fi
echo "Making sure all necessary packages are installed..."
if [[ `which ansible > /dev/null; echo $?` != 0 ]]; then
@@ -101,6 +129,53 @@ echo "Installed!"
#ansible-galaxy collection install community.general
#echo "Added!"
# Create basic layouts if configs do not exist and are requested.
if [[ -n "$create_general"
|| -n "$create_workstation"
|| -n "$create_server" ]]
then
sudo mkdir -pv "$config_dir"
fi
if [[ -n "$create_general" && ! -f "$general_config" ]]; then
function print_general_contents {
cat <<- EOF
; TEST DATA
enable=true
user=test
user_desc=Test
branch=$branch
; TEST DATA
EOF
}
print_general_contents | sudo tee "$general_config"
ls -lh "$general_config"
fi
if [[ -n "$create_workstation" && ! -f "$workstation_config" ]]; then
function print_workstation_contents {
cat <<- EOF
; TEST DATA
enable=true
coding=true
editing=false
gaming=false
; TEST DATA
EOF
}
print_workstation_contents | sudo tee "$workstation_config"
ls -lh "$workstation_config"
fi
if [[ -n "$create_server" && ! -f "$server_config" ]]; then
function print_server_contents {
cat <<- EOF
; TEST DATA
enable=true
; TEST DATA
EOF
}
print_server_contents | sudo tee "$server_config"
ls -lh "$server_config"
fi
echo "Provisioning Ansible..."
if [[ $local == "Y" ]]; then
sudo ansible-playbook $LOCAL

View File

@@ -415,6 +415,7 @@
function clean() {
clean-trash
clean-code
clean-backups
}
function_flatpak_usage: |
function flatpak-usage() {
@@ -563,6 +564,16 @@
echo -e "\n*** `basename $project` ***"
cd $project
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 push
else
@@ -767,6 +778,7 @@
alias_ls: |
alias l='ls'
alias ll='ls -alh'
alias lh='ls -ash'
alias_progs: |
alias progs='ps -ef'
alias_nethogs: |
@@ -781,28 +793,70 @@
date "+%Y%m%d"
}
function_backup: |
export BACKUP_DIR="/srv/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. ***"
/usr/local/bin/backup_system.sh
}
alias backup-system="backup"
alias system-backup="backup"
alias check-backups="ll $BACKUP_DIR"
function clean-backup {
sh -c "rm -rfv $BACKUP_DIR/*"
}
alias clean-backups="clean-backup"
function_pull_prod_backups: |
export DOCKER="Docker"
export SYSTEM="System"
function pull_prod_backup {
if [[ -z "$1" || ("$1" != "$DOCKER" && "$1" != "$SYSTEM") ]]; then
echo "ERROR: Parameter 1 not correct, expecting '$DOCKER' or '$SYSTEM'."
return 1
fi
if [[ -e "$BACKUP" ]]; then
ls -alh "$BACKUP"
echo "*** '$BACKUP' created successfully! ***"
backup_type="$1"
echo "Looking for '*${backup_type}*.zip'"
ls -lh *${backup_type}*.zip 2>/dev/null
status="$?"
if [[ "$status" == "0" ]]; then
echo "File already downloaded, skipping."
return 0
else
echo "*** '$BACKUP' not found! ***"
echo "File still needed, downloading to local tmp/."
fi
mkdir -pv tmp
scp -P {{ prod_port }} \
{{ prod_user }}@{{ prod_host }}:"$BACKUP_DIR/*${backup_type}*.zip" \
tmp/
if [[ "$?" == "0" ]]; then
echo "Succeeded!"
mv -v tmp/*.zip ./
rmdir -v tmp
else
echo "Failed!"
mv tmp ~/TRASH/"tmp_`now`"
fi
}
function pull_prod_backups {
dir="ProductionBackups-Pulled`today`"
mkdir -pv "$dir"
cd "$dir"
pull_prod_backup "$DOCKER"
pull_prod_backup "$SYSTEM"
du -h *.zip | sort -h
status="$?"
cd ..
if [[ "$status" != 0 ]]; then
echo "Failed to find zip files, removing folder."
rmdir -v "$dir"
return 1
fi
echo "Done!"
}
alias pull-prod-backups="pull_prod_backups"
alias pull-backups="pull_prod_backups"
alias prod-backups="pull_prod_backups"
alias prod-backup="pull_prod_backups"
alias pull-prod="pull_prod_backups"
alias prod-pull="pull_prod_backups"
function_log: |
function log {
echo -e "$1"
@@ -888,6 +942,77 @@
}
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 "
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"
alias_reload_bash: |
alias reload-bash="source ~/.bashrc"
alias bash-reload="reload-bash"
alias shell-reload="reload-bash"
alias reload-shell="reload-bash"
alias reload="reload-bash"
- name: General | Account Management | Users | Files | Common Variable
set_fact:
@@ -959,9 +1084,13 @@
{{ alias_ansible_facts }}
{{ function_now }}
{{ function_backup }}
{{ function_pull_prod_backups }}
{{ function_log }}
{{ function_debian_upgrade }}
{{ function_ebook_convert }}
{{ alias_sudo }}
{{ function_load_branch }}
{{ alias_reload_bash }}
- 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: "bash -c '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: "bash -c '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

@@ -43,6 +43,7 @@
- { app: "org.gimp.GIMP", name: "gimp", extra: "" }
- { app: "org.libreoffice.LibreOffice", name: "office", extra: "" }
- { app: "com.transmissionbt.Transmission", name: "transmission", extra: "" }
- { app: "app.grayjay.Grayjay", name: "grayjay", extra: "" }
flatpaks_coding:
- { app: "com.vscodium.codium", name: "codium-flatpak", extra: "" }
- { app: "com.google.AndroidStudio", name: "android-studio", extra: "" }

View File

@@ -19,6 +19,7 @@
, 'codium.desktop'
, 'org.shotcut.Shotcut.desktop'
, 'io.lmms.LMMS.desktop'
, 'app.grayjay.Grayjay.desktop'
, 'io.lbry.lbry-app.desktop', 'lbry.desktop'
, 'com.valvesoftware.Steam.desktop'
, 'net.lutris.Lutris.desktop'