refactor: more new session loading to anything under "load" subirectory (by default) still can add additional loading directories as before.

refactor of debug module using BASH_DEBUG as filename in user shell suddirectory  logs/

improved bindfs and btrfs modules
master
David Kebler 2023-02-22 11:39:34 -08:00
parent 99c01d4bb7
commit 0d4481e291
29 changed files with 78 additions and 70 deletions

View File

@ -11,5 +11,6 @@
LICENSE
*.sample
*archive*/
*.off/
off/
log*/

View File

@ -1,3 +0,0 @@
alias
env
functions

View File

@ -1 +0,0 @@
alias caddyr="ssh -t nas "/opt/scripts/caddyr""

View File

@ -1,7 +0,0 @@
# using firehol
alias fwstp="sudo firehol stop"
alias fws="sudo firehol start"
alias fwdf="/opt/firehol/scripts/default"
alias fwt="/opt/firehol/scripts/try"
alias fwdb="/opt/firehol/scripts/debug"
alias fwm="/opt/firehol/scripts/merge"

View File

@ -1,4 +0,0 @@
# run this on remote machine via ssh x11 forwarding
alias rmintupdate='XAUTHORITY=/home/sysadmin/.Xauthority sudo -E mintupdate'
alias rsources='XAUTHORITY=/home/sysadmin/.Xauthority sudo -E software-sources'
alias rsynaptic='XAUTHORITY=/home/sysadmin/.Xauthority sudo -E synaptic'

View File

@ -1,4 +0,0 @@
# nas
alias naslist="echo nasfm nasdisk nasgparted nasdata, caddyr, caddye"
alias nasha="ssh -t nas 'cd /usr/share/hassio/homeassistant ; bash'"
alias nascaddy="ssh -t nas 'cd /opt/caddy ; bash'"

View File

@ -2,7 +2,6 @@
# 00 will get loaded first
##
module_load systemd
user_reload() {
save=$PWD
@ -84,6 +83,7 @@ gpg -a --export $1 | sudo apt-key add -
}
osinfo () {
hostnamectl
echo kernel: $(uname -r)
echo machine: $(arch)
cat /etc/os-release

View File

View File

View File

View File

@ -25,8 +25,47 @@ make_base_subvols() {
alias btvl="sudo btrfs subvolume list"
snapshot_restore () {
local src=$1
local dest=$2
local name=${3:-$(echo $src| rev | cut -f 2- -d '.' | rev)}
local usesudo
echo copying $src at $PWD to $2 then renaming to $name
# TODO check for snapshot, strip volume from source path, remove extra snapshot when done
[[ $EUID -ne 0 ]] && usesudo=sudo
$usesudo btrfs send $src | $usesudo btrfs -q receive $dest
# cd $2 || return
$usesudo btrfs subvolume snapshot $dest/$src $dest/$name
# todo check for snapshot then delete transfered one
}
# ALL ALL = (root) NOPASSWD:/bin/btrfs
# btrfs send rootfs-nadal-$(date +"%d-%m-%Y") | btrfs receive /backup/images/snaps/rootfs
# btrfs subvolume snapshot -r / /backup/rootfs-nadal-$(date +"%d-%m-%Y")
# tar -cvpzf /backup/images/nadal-fs.tar.gz --one-file-system /
subvolume_size () {
local all;
[[ $1 == "-a" ]] && all=true
if [[ $all ]]; then
sudo btrfs qgroup show "${2:-$PWD}" --kbytes | tail -n +3
str=$(sudo btrfs qgroup show "${2:-$PWD}" --kbytes | tail -n +3);
else
sudo btrfs qgroup show "${2:-$PWD}" --kbytes | grep $1
str=$(sudo btrfs qgroup show "${2:-$PWD}" --kbytes | grep $1)
fi
# echo $str
subvolumeFolderSize=0;
while read line; do
FIELDS=( $line )
thisLineKb="${FIELDS[2]/'.00KiB'/''}";
# echo $thisLineKb
subvolumeFolderSize=$((subvolumeFolderSize+thisLineKb));
done <<< "$str"
[[ $all ]] && echo "size of entire filesystem at ${2:-$PWD}" || echo size of subvolumes/snapshots $1 at "${2:-$PWD}"
echo $subvolumeFolderSize KB
echo "~ $((subvolumeFolderSize/1024)) MB"
echo "~ $((subvolumeFolderSize/1024/1024)) GB"
}

View File

@ -1,14 +1,34 @@
#!/bin/bash
# TODO allow debug to have managment flags and levels
function debug () {
[[ $BASH_DEBUG ]] && echo -e "#### DEBUG ####\n $@ \n#####" >&2
if [[ $BASH_DEBUG ]]; then
if [[ $BASH_DEBUG == "stdout" ]]; then
echo -e "#### DEBUG ####\n $@ \n#####" >&2
else
local logfile=$BASH_SHELL_USER_DIR/logs/$BASH_DEBUG.log
[[ $1 == "-r" ]] && echo "$(date) Debugging log file, see debug.lib module" > $logfile && return
echo -e "$@" >&2 >> $logfile
fi
fi
# [[ $test ]] && return
}
# alias debug_on="sudo -i uncomment BASH_DEBUG /etc/bash.bashrc"
# alias debug_off="sudo -i comment BASH_DEBUG /etc/bash.bashrc"
function debug_reset () {
local logfile=$BASH_SHELL_USER_DIR/logs/$1.log
echo clearing $logfile
echo "$logfile at $(date)" > $logfile
shift
echo $@ >> $logfile
echo -e "#####################" >> $logfile
}
# *************** DEBUGGING ***********************
## *************** DEBUGGING ***********************
# module_load debug
# if [[ $? -ne 0 ]]; then
# echo "unable to load a 'debug' module using a noop for debug function"

View File

@ -7,7 +7,7 @@ shell_source_subdirs () {
local SUBDIRS
local IGNORE_FILE
DIR=$1
SUBDIRS=$([[ -f "$DIR/.bash-shell-include" ]] && cat "$DIR/.bash-shell-include" || cat "$BASH_SHELL_BASE/.bash-shell-include")
SUBDIRS="load $([[ -f "$DIR/.bash-shell-include" ]] && cat "$DIR/.bash-shell-include" || cat "$BASH_SHELL_BASE/.bash-shell-include")"
[[ $SSH_SESSION ]] && SUBDIRS+=" ssh/session"
IGNORE_FILE="$([[ -f "$DIR/.bash-shell-ignore" ]] && echo "$DIR" || echo "$BASH_SHELL_BASE")/.bash-shell-ignore"
# echo $DIR
@ -15,13 +15,7 @@ shell_source_subdirs () {
for SUBDIR in $SUBDIRS; do
if [ -e "$DIR/$SUBDIR" ]; then
# echo processing subdirectory $DIR/$SUBDIR
# must quote all globs to avoid bash glob expansion which is likely on
# TODO have default set of ignores plus check for shell-ignore
# source_dir -p "archive" -x '"*.off" "*.md" "*TODO*" "LICENSE" "*.sample"' -d 0 $DIR/$SUBDIR
# source_dir -p "archive" -x "$excludes" -d 0 $DIR/$SUBDIR
source_dir -f "$IGNORE_FILE" -d 0 $DIR/$SUBDIR
# else
# echo no subdirectory $DIR/$SUBDIR to process, ignorning
fi
done
}

View File

@ -1,33 +0,0 @@
#!/bin/bash
#bmount () {}
# sudo bindfs -o allow_other -o map=root/1001:@root/@1001 /var/lib/docker/volumes /data/dvols
dir_rebind_user () {
local usesudo; local dir;local user;local group
# $1 new owner
# $2 dir
[ $# -lt 2 ] && echo minimum args needed to rebind are user and directory && return 1
[[ $EUID -ne 0 ]] && usesudo=sudo
[[ $(id -u $1 2> /dev/null) ]] || { echo user $1 does not exist can not continue; return 2; }
dir=$(realpath $2)
user=$(stat -c '%U' $dir)
group=$(stat -c '%G' $dir)
$usesudo bindfs --force-user=$1 --force-group=$1 --create-for-user=$user --create-for-group=$group --chown-ignore --chgrp-ignore $dir $dir
# echo use \'dir_rebind_user_remove $dir\' to remove
}
dir_rebind_user_remove () {
local usersudo
[[ $EUID -ne 0 ]] && usesudo=sudo
$usesudo fusermount -u $1
}
if [ -v PS1 ]; then
alias dru="dir_rebind_user"
alias drur="dir_rebind_user_remove"
fi

View File

@ -48,8 +48,13 @@ function lines_2_str () {
echo $str
}
# find, superceeds find use `command find` to get the super
function _find () {
# BASH_DEBUG=find
# USAGE
# all option arguments that contain globs/wildcards must be quoted to avoid expansion
# f sets path and file excludes from a supplied file path
@ -196,14 +201,16 @@ return 0
}
source_dir () {
# echo passed: $*
# BASH_DEBUG=source_dir
debug source_dir passed arguments: $*
debug function: source_dir
local FILES
FILES=$(_find "$@") # find function
# echo $FILES >&2
[[ $? -ne 0 ]] && return 1
for f in $FILES; do
# echo sourcing: $f >&2
debug sourcing: $f >&2
source "$f"
done

View File

@ -1,7 +1,6 @@
#!/bin/bash
# must be json as a string, depends on jq
mounted () {
[[ ! $1 ]] && echo no mount point to test && return 2
mountpoint "$1" &> /dev/null && echo yes || return 1
}