added os transfer scripts (btrfs snapshots)

added user session scripts and services
added keepassxc scripts
rearranged some modules
master
David Kebler 2024-02-12 14:16:57 -08:00
parent 3052888b9f
commit e42b9fe72a
17 changed files with 185 additions and 289 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
archive/ archive/
*.log

View File

@ -1,5 +1,6 @@
#!/bin/bash #!/bin/bash
_dir="$(dirname $(realpath "${BASH_SOURCE:-$0}"))" _dir="$(dirname $(realpath "${BASH_SOURCE:-$0}"))"
# echo at $@ >> ~/Desktop/ssh.log
if systemctl --user is-active ssh-agent; then if systemctl --user is-active ssh-agent; then
echo starting keepass unlock for $1 echo starting keepass unlock for $1
BASH_ENV=$HOME/ssh-agent.properties $_dir/keepassxc-unlock.sh "$@" BASH_ENV=$HOME/ssh-agent.properties $_dir/keepassxc-unlock.sh "$@"
@ -10,7 +11,7 @@ if systemctl --user is-active ssh-agent; then
sleep 2 sleep 2
cat $HOME/ssh-agent.properties cat $HOME/ssh-agent.properties
echo starting keepass unlock for $1 echo starting keepass unlock for $1
BASH_ENV=$HOME/ssh-agent.properties $_dir/keepassxc-unlock.sh "$@" SSH_AUTH_SOCK=$(cat $HOME/ssh-agent.properties) $_dir/keepassxc-unlock.sh "$@"
else else
echo unable to start ssh-agent echo unable to start ssh-agent
fi fi

View File

@ -4,12 +4,14 @@
database=${1:-$KEEPASS_DB_PATH} database=${1:-$KEEPASS_DB_PATH}
keyfile=${2:-$KEEPASS_DB_KEY_PATH} keyfile=${2:-$KEEPASS_DB_KEY_PATH}
unlock="dbus-send --print-reply --dest=org.keepassxc.KeePassXC.MainWindow /keepassxc org.keepassxc.KeePassXC.MainWindow.openDatabase string:$database string:"" string:$keyfile" unlock="dbus-send --print-reply --dest=org.keepassxc.KeePassXC.MainWindow /keepassxc org.keepassxc.KeePassXC.MainWindow.openDatabase string:$database string:"" string:$keyfile"
if ! qdbus | grep keepassxc; then if ! qdbus | grep keepassxc; then
echo starting keepassxc first
keepassxc& keepassxc&
sleep 1 sleep 1
if ! qdbus | grep keepassxc; then echo unable to start keepassxc; exit; fi if ! qdbus | grep keepassxc; then echo unable to start keepassxc; exit; fi
fi fi
if $unlock &> /dev/null; then env | grep SSH
if export SSH_AUTH_SOCK=$SSH_AUTH_SOCK; $unlock &> /dev/null; then
echo keepass database $database succesfully unlocked echo keepass database $database succesfully unlocked
else else
echo unable to unlock $database echo unable to unlock $database

View File

@ -1,6 +1,6 @@
#/bin/bash #/bin/bash
# echo https://wiki.archlinux.org/title/Pacman # https://wiki.archlinux.org/title/Pacman
alias pm="pacman" alias pm="pacman"
alias pms="sudo pacman" alias pms="sudo pacman"
alias pmi="sudo pacman -S --needed" alias pmi="sudo pacman -S --needed"
@ -14,6 +14,8 @@ alias pmlf="comm -23 <(pacman -Qqett | sort | uniq) <(pacman -Qqg base-devel | s
alias pkgst="pacman -Qi" alias pkgst="pacman -Qi"
alias pkgi="pacman -Si" alias pkgi="pacman -Si"
pmig () { pmig () {
pacman -Q | grep $1 | cut -d ' ' -f 1 pacman -Q | grep $1 | cut -d ' ' -f 1
} }

4
distro/debian/apt Normal file
View File

@ -0,0 +1,4 @@
add-repo-key () {
gpg --keyserver keyserver.ubuntu.com --recv-key $1
gpg -a --export $1 | sudo apt-key add -
}

View File

@ -1,101 +0,0 @@
#!/bin/bash
# echo loading bindfs
export BFS_MOUNT_DIR=${BFS_MOUNT_DIR:-/mnt/bfs}
if [ -v PS1 ]; then
alias rbfsu="dir_rebind_user"
alias rbfs="dir_rebind"
alias bfsu="dir_bind_user"
alias bfs="dir_bind"
alias bfsum="dir_bind_unmount"
alias bfse="bfs_vscode"
sudo mkdir $BFS_MOUNT_DIR 2> /dev/null
sudo chown $USER:$USER $BFS_MOUNT_DIR
fi
mounted () {
mountpoint "$1" &> /dev/null && echo yes || return 1
}
dir_bind_unmount () {
local usesudo
local mp=$1
[[ ! $(mounted $mp) ]] && echo no mountpoint at $mp && mp=$BFS_MOUNT_DIR/$(basename $mp)
[[ ! $(mounted $mp) ]] && echo no mountpoint at $mp either, aborting && return 1
[[ $EUID -ne 0 ]] && usesudo=sudo
if $usesudo fusermount -u $mp; then
echo unmounted $mp, removing empty mountpoint directory
rm -rf $mp
else
echo error, unable to unmount $mp
fi
}
dir_bind_user () {
local usesudo; local dir;local user;local group;local mp
if [ $# -lt 3 ]; then
echo "minimum 3 args needed to rebind <user? <source dir> <mountpoint>"
echo passed were $@
return 1
fi
[[ $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)
mp=$3
[[ $(mounted $mp) ]] && echo something already mounted at $mp, aborting && return 1
if ! $usesudo mkdir -p $mp; then echo unable to make mountpoint aborting; return 2; fi
$usesudo chown $1:$1 $mp
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 $mp
[[ $? -gt 0 ]] && echo error in call to bindfs
if [[ $(mounted $mp) ]]; then
echo $dir has been mounted at $mp for user $1
echo "to unmount use: dir_bind_unmount $mp or bfsum $mp"
else
echo unable to mount $dir at $mp as user $user
fi
}
dir_bind () {
mp=${2:-$BFS_MOUNT_DIR/$(basename $1)}
dir_bind_user $USER $1 $mp
if [ -v PS1 ]; then
echo enter \"u\" when you ready to unmount, otherwise any other key will leave mounted
read -n1 ans
echo -e "\n"
[[ $ans == "u" ]] && dir_bind_unmount $mp
fi
}
dir_rebind () {
dir_bind_user $USER $1 $1
}
dir_rebind_user () {
dir_bind_user $1 $2 $2
}
bfs_vscode () {
mp=${2:-$BFS_MOUNT_DIR/$(basename $1)}
dir_bind_user $USER $1 $mp
/opt/bin/vscode $mp
if [ -v PS1 ]; then
echo when you ready to unmount FIRST close your vscode window then enter \"u\"
echo otherwise any other key will leave mounted
read -n1 ans
echo -e "\n"
[[ $ans == "u" ]] && dir_bind_unmount $mp
fi
}
(return 0 2>/dev/null) || dir_bind_user $@

View File

@ -1,184 +0,0 @@
#!/bin/bash
# dry run by default
# >btrbk_run <conf name> <btrbk run options>
# with -e exectue
# >btrbk_run -e <conf name> <btrbk run options>
# to only create the links
# >btrbk_run -e <conf name> -n
btrbk_scripts_dir="$(dirname $(realpath "${BASH_SOURCE:-$0}"))"
# will try to find a conf file with out without .conf extension in a few places
btrbk_conf () {
local file=${1:-btrbk.conf}
[[ -f $file ]] && echo $file && return
[[ -f $file.conf ]] && echo $file.conf && return
BTRBK_CONF_DIR=${BTRBK_CONF_DIR:-/snapshots/conf}
# echo $btrbk_scripts_dir
# echo $BTRBK_CONF_DIR
[[ -f $BTRBK_CONF_DIR/$file ]] && echo $BTRBK_CONF_DIR/$file && return
[[ -f $BTRBK_CONF_DIR/$file.conf ]] && echo $BTRBK_CONF_DIR/$file.conf && return
[[ -f /etc/btrbk/$file ]] && echo /etc/btrbk/$file && return
[[ -f /etc/btrbk/$file.conf ]] && echo /etc/btrbk/$file.conf && return
[[ -f /etc/btrbk.conf ]] && echo /etc/btrbk.conf && return
return 1
}
btrbk_clean () {
local file=$(btrbk_conf $1)
[[ ! $file ]] && return 1
sudo btrbk -c $file clean $2
}
btrbk_src () {
local file=$(btrbk_conf $1)
[[ ! $file ]] && return 1
sudo btrbk -c $file list config --format col:h:source_subvolume
}
btrbk_dests () {
local file=$(btrbk_conf $1)
[[ ! $file ]] && return 1
local dests=$(sudo btrbk -c $file list config --format col:h:snapshot_path | tail -1)
dests+=" $(sudo btrbk -c $file list target --format col:h:target_path)"
echo $dests
}
btrbk_latest_links () {
local dry_run="echo"
[[ $1 == "-e" ]] && dry_run="" && shift
local file=$(btrbk_conf $1)
[[ ! $file ]] && echo unable to find conf file for ${1:-btrbk.conf} && return 1
shift
local latest=$(sudo btrbk -c $file list latest --format col:h:snapshot_subvolume | xargs -I % sh -c 'basename %' | sed '$!N; /^\(.*\)\n\1$/!P; D')
[[ ( ! $latest ) || $latest == "-" ]] && echo "no latest snapshots so can't make .latest links" && return 3
local dests=$(btrbk_dests $file)
if [[ $dry_run ]]; then
echo dry run for making latest links for following destinations, NOTE: use -e to actually make them
echo $dests
echo "-------------------"
fi
echo making .latest symlinks in each destination
for dest in $dests; do
for snap in $latest; do
# [[ -e $dest/${snap%%.*}.latest ]] && $dry_run_echo sudo rm $dest/${snap%%.*}.latest
local cmd="sudo ln -srfn $dest/$snap $dest/${snap%%.*}.latest"
echo $cmd
[[ ! $dry_run ]] && $cmd
done
done
}
btrbk_run () {
local dry_run="-n"
[[ $1 == "-e" ]] && dry_run="" && shift
local file=$(btrbk_conf $1)
[[ ! $file ]] && echo unable to find conf file for ${1:-btrbk.conf} && return 1
shift
local src=$(btrbk_src $file)
local snaps=$(sudo btrbk -c $file list source --format col:h:snapshot_name)
local dests=$(sudo btrbk -c $file list config --format col:h:snapshot_path | tail -1)
local dests+=" $(btrbk -c $file list target --format col:h:target_path)"
# todo distinguish local from remote destinations and deal with differently for latest link
# any pre snap tasks
# for dest in $dests; do
# if [[ ! -d $dest ]]; then
# if confirm directory $dest does not exist, create; then
# sudo mkdir -p $dest
# else
# echo destination $dest directory MUST exist aborting brtbk run
# return 2
# fi
# fi
# done
echo using configuration file: $file
echo creating snapshots: $snaps
echo from $src
echo at these destinations $dests
echo additional passed arguments: $@
# do snaps and backups
[[ $dry_run ]] && echo backup dry run || echo Taking snapshost and making backups now...
sudo btrbk -c $file run $dry_run --progress $@
# post snap/backup
btrbk_latest_links $([[ $dry_run ]] || printf "%s" -e) $file
}
(return 0 2>/dev/null) || btrbk_run $@
latest_clone () {
local src=$(realpath $1)
local dest=$(realpath $2)
shift 2
module_load confirm
if [[ ! -d $dest ]]; then
if confirm "destination directory $dest does not exist, create it"; then
sudo mkdir -p $dest
else
return 1
fi
fi
local snaps=$(ls $src | grep latest | sed 's/.\{1\}$//' | xargs -I % realpath $src/%)
[[ ! $snaps ]] && echo no latest snapshots in $src && ls -la $src && return 1
if confirm create snapshots for $snaps in $dest; then
local destsnap
for snap in $snaps; do
destsnap="$dest/$(basename $snap | cut -f 1 -d '.' )"
if [[ -d $destsnap ]]; then
if confirm -s snapshot $destsnap already exists do you want to over write it; then
sudo btrfs subvolume delete $destsnap
else
return 2
fi
fi
sudo btrfs subvolume snapshot $@ $snap $destsnap
done
echo $dest
ls -la $dest
fi
}
# use snapshot to then snap latest to another location, have it edit the fstab file
# #!/bin/bash
# [[ $# -lt 2 ]] && echo "need to supply a <root instance dir> and a snapshot name" && exit
# # echo sed 's/$name/'$name'/g' named.conf.tmpl > $subvoldir-$name.conf
# # echo sed -i 's/$subvoldir/'$subvoldir'/g' $subvoldir-$name.conf
# named="$2"
# src=/mnt/linuxpart
# dest=/mnt/linuxpart
# echo sudo mkdir -p $dest/$named
# echo sudo btrfs subvolume snapshot $src/$1/@root $dest/$named/@root
# echo sudo btrfs subvolume snapshot $src/$1/@opt $dest/$named/@opt
# echo sudo btrfs subvolume snapshot $src/$1/@home $dest/$named/@home
# echo sudo btrfs subvolume snapshot $src/@shell $dest/$named/@shell
# sudo mkdir -p $dest/$named
# sudo btrfs subvolume snapshot $src/$1/@root $dest/$named/@root
# sudo btrfs subvolume snapshot $src/$1/@opt $dest/$named/@opt
# sudo btrfs subvolume snapshot $src/$1/@home $dest/$named/@home
# sudo btrfs subvolume snapshot $src/@shell $dest/$named/@shell
# echo to edit: bfs_vscode $dest/$named

13
osxfer/example.env Normal file
View File

@ -0,0 +1,13 @@
### TARGET ###
# target device to mountpint -give a value for only one of these three
TUUID=
TLABEL=primary
TDEV=
# ----
# directory on target for subvolumes
TNAME=osname
### SOURCE ###
# mountpoint of source subvolumes
SMP=/mnt/pnystick
# directory on source mountpint for subvolumes
SNAME=basemanjaro

14
osxfer/os.conf.tmpl Normal file
View File

@ -0,0 +1,14 @@
timestamp_format short
snapshot_preserve_min all
target_preserve_min all
transaction_log /snapshots/log/<% $TNAME %>.log
incremental no
snapshot_dir <%% echo ${SDIR:-/snapshots/transfer/source} %>
target <%% echo ${TSNAPS:-${TDIR:-/snapshots/transfer/target}/tmp/tsnaps} %>
volume <% $SMP %>/<% $SNAME %>
subvolume @home
subvolume @root
subvolume @opt
subvolume @shell

86
osxfer/transfer.sh Executable file
View File

@ -0,0 +1,86 @@
#!/bin/bash
scripts_dir="$(dirname $(realpath "${BASH_SOURCE:-$0}"))"
efile=$1.env
if [[ -f $efile ]]; then
module_load confirm
echo "#### sourcing $efile ########"
source $efile
TDIR=${TDIR:-/snapshots/transfer/target}
TSNAPS=${TDIR:-/snapshots/transfer/target}/${TSNAPS:-tmp/tsnaps}
SDIR=${SDIR:-/snapshots/transfer/source}
CDIR=${CDIR:-/snapshots/conf}
# TODO make sure minimal environment is set
source <(
source $efile
bash $(module_find btpl) os.conf.tmpl
) >$CDIR/$TNAME.conf
echo "######### btrbk file: $TNAME.conf ##############"
cat "$CDIR/$TNAME.conf"
echo "#######################"
if ! confirm "conf file is ok?"; then exit; fi
module_load btrbk btrfs
mkdir -p $TDIR &>/dev/null
mkdir -p $SDIR &>/dev/null
echo $SDIR
ls -la /snapshots/transfer
[[ $TLABEL ]] && sudo mount -L $TLABEL $TDIR
[[ $TUUID ]] && sudo mount --UUID=$TUUID $TDIR
[[ $TDEV ]] && sudo mount /dev/$TDEV $TDIR
if mountpoint $TDIR &>/dev/null; then
sudo mkdir -p $TSNAPS
echo able to mount $TLABEL $TUUID $TDEV at $TDIR... continuing transfer
if btrbk_run $2 "$CDIR/$TNAME.conf"; then
echo successful transfer from $SMP/$SNAME to $TLABEL $TUUID $TDEV - $TSNAPS
sudo btrbk -c "$CDIR/$TNAME.conf" list latest
echo "########################"
if sudo mkdir -p $TDIR/$TNAME &>/dev/null; then
echo cloning transfered read only snapshots at $TSNAPS
ls -la $TSNAPS
echo to $TDIR/$TNAME
sudo btrbk -c "$CDIR/$TNAME.conf" list latest
latest=$(sudo btrbk -c "$CDIR/$TNAME.conf" list latest --format col:h:snapshot_subvolume | xargs -I % sh -c 'basename %' | sed '$!N; /^\(.*\)\n\1$/!P; D')
if [[ $latest ]]; then
for snap in $latest; do
# dests+=" $(btrbk -c $file list target --format col:h:target_path)"
bsnap=$(basename "${snap%.*}")
echo cloning $TSNAPS/$snap to $TDIR/$TNAME/$bsnap
if [[ -d $TDIR/$TNAME/$bsnap ]]; then
echo snapshot $TDIR/$TNAME/$bsnap already exists you must manually remove it if you want to overwrite
echo "'sudo btrfs sub del $TDIR/$TNAME/$bsnap' or 'sudo btrfs sub del $TDIR/$TNAME/*'"
else
sudo btrfs sub snap $TSNAPS/$snap $TDIR/$TNAME/$bsnap
echo $TDIR/$TNAME/$bsnap
ls -la $TDIR/$TNAME/$bsnap
fi
done
echo $TDIR/$TNAME
ls -la $TDIR/$TNAME
else
echo no transfered snapshots to clone
fi
else
echo ERROR unable to make directory $TDIR/$TNAME
fi
else
echo ERROR: snapshot transfer to $TLABEL $TUUID $TDEV - $TSNAPS failed
fi
echo deleting temporary transfer snapshots at $SDIR
sudo btrfs sub del $SDIR/*
sudo rm -rf $SDIR
echo deleting temporary transfer snapshots at $TSNAPS
sudo btrfs sub del $TSNAPS/*
sudo rm -rf $TSNAPS
echo unmounting $TDIR
if sudo umount -R $TDIR; then
if ! confirm "remove local mount directory $TDIR?"; then exit; fi
sudo rm -f $TDIR
else
echo WARNING: not able to unmount $TDIR
fi
else
echo unable to mount $TLABEL $TUUID $TDEV at $TDIR
fi
else
echo environment file $efile does not exisit
fi

28
session/dm.inst Executable file
View File

@ -0,0 +1,28 @@
#!/bin/bash
sdir="$(dirname $(realpath "${BASH_SOURCE:-$0}"))"
_dm=$(basename $(grep 'ExecStart=' /etc/systemd/system/display-manager.service))
echo Current Display Manager on this system is: $_dm
case $_dm in
lightdm)
if [[ -d /etc/lightdm ]]; then
echo adding startup/cleanup configuration
if sudo mkdir /etc/lightdm/lightdm.conf.d &> /dev/null; then
sudo cp $sdir/lightdm/lightdm.conf.d/* /etc/lightdm/lightdm.conf.d/
ls -la /etc/lightdm/lightdm.conf.d
else
echo could not make directory $sdir/lightdm/lightdm.conf.d
fi
else
echo lightdm configuration directory /etc/lightdm does not exist
echo unable to install user setup/cleanup scripts
fi
;;
*)
echo unknown display manager $_dm
;;
esac

View File

@ -0,0 +1,3 @@
[Seat:*]
session-cleanup-script=/etc/lightdm/lightdm.conf.d/lightdm-cleanup.sh
session-setup-script=/etc/lightdm/lightdm.conf.d/lightdm-setup.sh

View File

@ -0,0 +1,8 @@
#!/bin/bash
dir=${HOME}/${USER_SESSION_DIR:-.session}
file=${dir}/session_logout
echo "running as user $(id -u), cleanup for ${USER}, file:${file} $(date)" >> /opt/scripts/lightdm.log
log=${dir}/session.log
if [[ -e "$file" ]]; then
/bin/bash $file 1>>${log} 2>>${log} || true
fi

View File

@ -0,0 +1,8 @@
#!/bin/bash
dir=${HOME}/${USER_SESSION_DIR:-.session}
file=${dir}/session_login
echo "running as user $(id -u), setup for ${USER}, file:${file} $(date)" >> /opt/scripts/lightdm.log
log=${dir}/session.log
if [[ -e "$file" ]]; then
/bin/bash $file 1>>${log} 2>>${log} || true
fi

0
session/readme.md Normal file
View File

View File

@ -0,0 +1,11 @@
[Unit]
Requires=home.mount
After=home.mount
[Service]
ExecStart=/bin/mount %h/%I
ExecStop=/bin/umount %h/%I
RemainAfterExit=yes
[Install]
WantedBy=default.target