From b1e1065e4c667eee1c537792ed38d42b8ff54421 Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Mon, 19 Apr 2021 17:26:32 -0700 Subject: [PATCH 01/13] use return not exit to avoid ending an ssh session --- all/app/awscli | 2 +- all/app/bashly | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/all/app/awscli b/all/app/awscli index 637d74e..e48e402 100644 --- a/all/app/awscli +++ b/all/app/awscli @@ -1,5 +1,5 @@ #!/bin/bash -command -v docker >/dev/null 2>&1 || exit +command -v docker >/dev/null 2>&1 || return aws_docker () { local cred diff --git a/all/app/bashly b/all/app/bashly index 7ba02ce..417c153 100644 --- a/all/app/bashly +++ b/all/app/bashly @@ -1,4 +1,4 @@ -command -v docker >/dev/null 2>&1 || exit +command -v docker >/dev/null 2>&1 || return alias bashly='docker run --rm -it --volume "$PWD:/app" dannyben/bashly' alias bashly-update='docker pull dannyben/bashly:latest' From b5901d1d6f6fa264f9bf17d68737aa11bdda82e5 Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Mon, 5 Jul 2021 09:37:32 -0700 Subject: [PATCH 02/13] fix using pyenv --- all/lang/go | 6 +++--- all/lang/node | 2 +- all/lang/python | 28 +++++++++++++++------------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/all/lang/go b/all/lang/go index 60e8ad8..5618995 100644 --- a/all/lang/go +++ b/all/lang/go @@ -2,7 +2,7 @@ # --- Go Language Environment BEGIN --- export GOROOT=/opt/go - export PATH=$PATH:/opt/go/bin - export GOPATH=/opt/go/apps - export PATH=$PATH:/opt/go/apps/bin +export PATH=$PATH:/opt/go/bin +export GOPATH=/opt/go/apps +export PATH=$PATH:/opt/go/apps/bin # --- Go Language Environment END --- diff --git a/all/lang/node b/all/lang/node index 5e83e0e..dec79ba 100644 --- a/all/lang/node +++ b/all/lang/node @@ -44,7 +44,7 @@ alias npma="npm-add-dependencies" # alias npmpub="npm publish --access public" alias npmlrepub="npm publish --registry http://localhost:4873 --force --access restricted" alias npmlpub="npm publish --registry http://localhost:4873 --access restricted" -alias npmkrepub="npm version patch && npm publish --registry https://npm.kebler.net --access restricted" +alias npmkrepub="npm publish --registry https://npm.kebler.net --force --access restricted" alias npmkpub="npm publish --registry https://npm.kebler.net --access restricted" alias npmpatch="npm version patch" alias npmmajor="npm version major" diff --git a/all/lang/python b/all/lang/python index 51e4110..4b06a36 100644 --- a/all/lang/python +++ b/all/lang/python @@ -1,4 +1,14 @@ #!/bin/bash + +use_pyenv () { + if command -v pyenv >/dev/null 2>&1; then + export PYENV_ROOT="/opt/python/pyenv" + export PYENV_VERSION=${PYENV_VERSION:-3.8.7} + eval "$(pyenv init -)" + eval "$(pyenv virtualenv-init -)" + fi +} + # TODO make alias generating function for versions of python alias python3x="sudo -H python3.8 -m" alias spip="sudo -H python3.8 -m pip" @@ -14,24 +24,15 @@ alias pipi="pipx install" alias pipl="pipx list" alias piplv="pipx list --verbose" -if command -v pyenv >/dev/null 2>&1; then - -# pyenv environment path_append "/opt/python/bin" -# eval "$(pyenv init -)" -# eval "$(pyenv virtualenv-init -)" - -export PYENV_ROOT="/opt/python/pyenv" -export PYENV_VERSION=3.8.7 -eval "$(pyenv init -)" -eval "$(pyenv virtualenv-init -)" - -fi - +if command -v pipx >/dev/null 2>&1; then path_append "/opt/python/apps/bin" export PIPX_HOME=/opt/python/apps export PIPX_BIN_DIR=$PIPX_HOME/bin +fi + +[[ $USE_PYENV ]] && use_pyenv spipua () { module_load confirm @@ -44,3 +45,4 @@ echo "$List" confirm "ATTENTION: upgrade all these packages at once??" || return 0 echo "$List" | cut -d' ' -f1 | xargs -n1 sudo -H python$Ver -m pip install -U } + From b1fd4a6dc639b8ab3bf828d4b55318c58094bdc8 Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Sun, 29 Aug 2021 08:01:40 -0700 Subject: [PATCH 03/13] backup script refactored to use restic --- all/app/backup | 8 +- all/dev/backup/dbackup | 603 +++++++++++++++++++++++++++++ all/dev/backup/src/bashly.yml | 133 ++++--- all/dev/backup/src/initialize.sh | 1 + all/dev/backup/src/root_command.sh | 111 +++++- all/dev/backup/test | 2 +- 6 files changed, 787 insertions(+), 71 deletions(-) create mode 100755 all/dev/backup/dbackup diff --git a/all/app/backup b/all/app/backup index 16a71ff..a202519 100644 --- a/all/app/backup +++ b/all/app/backup @@ -1,7 +1,7 @@ #!/bin/bash backup () { -DIR=$(dirname $(cd "$(dirname "$BASH_SOURCE")" >/dev/null 2>&1 ; pwd -P )) -$DIR/dev/backup/backup "$@" -#module_load backup -#run "$@" + DIR=$(dirname $(cd "$(dirname "$BASH_SOURCE")" >/dev/null 2>&1 ; pwd -P )) + $DIR/dev/backup/dbackup "$@" + #module_load backup + #run "$@" } \ No newline at end of file diff --git a/all/dev/backup/dbackup b/all/dev/backup/dbackup new file mode 100755 index 0000000..11931f5 --- /dev/null +++ b/all/dev/backup/dbackup @@ -0,0 +1,603 @@ +#!/usr/bin/env bash +# This script was generated by bashly (https://github.com/DannyBen/bashly) +# Modifying it manually is not recommended + +# :command.root_command +root_command() { + # :src/root_command.sh + #!/bin/bash + + echo "running root command" + + inspect_args + + # module_load ssh + module_load confirm + module_load path + + local settings=${args[--settings]} + + if [[ -f $settings ]]; then + echo loading settings file $settings + module_load yaml + eval $(parse_yaml $settings "s_") + echo $s_source + echo $s_target + + fi + + if [[ $s_server_host ]]; then + s_server="rest:http$([[ $s_server_secure ]] && echo "s")://${s_server_host}$([[ $s_server_port ]] && echo :${s_server_port})" + fi + + local password=${args[--password]:-$BACKUP_PASSWORD} + password=${password:-$s_password} + [[ ! $password ]] && echo restic requires a backup repository password, exiting && return 2 + password="RESTIC_PASSWORD=${password}" + + local server=${args[--server]:-$BACKUP_SERVER} + server=${server:-$s_server} + + local backup_dir=${args[--backup_dir]:-$BACKUP_DIR} + backup_dir=${backup_dir:-$s_backup_dir} + backup_dir=${backup_dir:-"/backup"} + + local source="${args[source]:-$s_source}" + source=$(echo "${source:-$PWD}" | tr -s /) + + local target=${args[target]:-$s_target} + target="$(echo $target | tr -s /)" + if [[ ${args[--dir]:-$s_dir} ]]; then + target="${target}$(echo $source | tr -s / | sed -e "s#^[.]##")" + fi + if [[ $(isAbsPath $target) ]]; then + echo absolute target directory + else + target="/${HOSTNAME}/${target}" + if [[ $server ]]; then + target="${server}${target}" + else + target=${backup_dir}${target} + fi + fi + + + local exclude=${args[--exclude_file]:-$BACKUP_EXCLUDE} + exclude=${exclude:-$s_exclude} + exclude=${exclude:-"$source/exclude.bac"} + + + local shost=$([[ ${args[--shost]} ]] && echo ${args[--shost]}::) + local suser=$([[ ${args[--suser]} ]] && echo ${args[--suser]}@) + local thost=$([[ ${args[--host]} ]] && echo ${args[--host]}::) + local tuser=$([[ ${args[--user]} ]] && echo ${args[--user]}@) + + local options=$(echo ${args[--options]} | awk '{gsub(/\\/," ")}1') + + local bin=$(command -v restic) + + + local cmd=${args[cmd]:-"backup"} + + + echo before exists exclude: $exclude + + exclude=$([[ -f $exclude ]] && echo "--iexclude-file $exclude" || echo "") + + echo source: $source + echo target $target + echo exclude: $exclude + + + # local ssh="--remote-schema \"ssh -C %s /home/sysadmin/.local/bin/rdiff-backup --server\"" + + #cmd="$sudo rdiff-backup $options $exclude $ssh ${suser}${shost}$source ${tuser}${thost}$target" + + + cmd="sudo $password $bin -r $target backup $source $exclude" + + if [[ ${args[--init]} ]]; then cmd="sudo $password $bin -r $target init"; fi + if [[ ${args[--snap]} ]]; then cmd="sudo $password $bin -r $target snapshots"; fi + if [[ ${args[--prune]} ]]; then cmd="sudo password $bin -r $target prune"; fi + if [[ ${args[--view]} || ${args[--view-path]} ]]; then + mount=${args[--view-path]:-$BACKUP_MOUNT} + mount=${mount:-"/opt/backup/view/"} + echo view mount point $mount + if [[ -e ${mount} ]]; then + cmd="$password $bin -r $target mount $mount"; + echo browse files at $mount/snapshots/latest${source} + else + echo $mount: directory for mounting snapshot for viewing does not exist. Create and try again + return 3 + fi + fi + + echo $cmd + confirm run this command? || return 1 + eval $cmd + + # sudo chown -R $USER:$USER $target + # sudo chown -R $USER:$USER /home/$USER/.cache/restic + # sudo chmod -R g+rwX /home/$USER/.cache/restic +} + +# :command.version_command +version_command() { + echo "$version" +} + +# :command.usage +dbackup_usage() { + if [[ -n $long_usage ]]; then + printf "dbackup - differential backup using restic\n" + echo + else + printf "dbackup - differential backup using restic\n" + echo + fi + + printf "Usage:\n" + printf " dbackup [SOURCE] [TARGET] [options]\n" + printf " dbackup --help\n" + printf " dbackup --version\n" + echo + + if [[ -n $long_usage ]]; then + printf "Options:\n" + # :command.usage_fixed_flags + echo " --help" + printf " Show this help\n" + echo + echo " --version" + printf " Show version number\n" + echo + # :command.usage_flags + # :flag.usage + echo " --password, -p PASSWORD" + printf " repo password (or file path) for backup repository\n" + echo + + # :flag.usage + echo " --remote, -r" + printf " backup to a remote machine\n" + echo + + # :flag.usage + echo " --server URL" + printf " url of restic rest server\n" + echo + + # :flag.usage + echo " --init" + printf " initialize repo (default is backup)\n" + echo + + # :flag.usage + echo " --snap" + printf " list repo snapshots\n" + echo + + # :flag.usage + echo " --view, -v" + printf " mount snapshot for viewing (default is BACKUP_MOUNT or /opt/backup/view)\n" + echo + + # :flag.usage + echo " --view-path PATH" + printf " set custom mount path for viewing of snapshot, --view not required if set\n" + echo + + # :flag.usage + echo " --prune PRUNE" + printf " prune repo (default is backup). true for default prune or path to prune\n settings\n" + echo + + # :flag.usage + echo " --password, -p PASSWORD" + printf " repo password (or file path) for backup repository\n" + echo + + # :flag.usage + echo " --settings, -s SYAML" + printf " path to settings file (yaml). Keys are same as long\n" + echo + + # :flag.usage + echo " --host, -h THOST" + printf " host on remote to target to receive backup\n" + echo + + # :flag.usage + echo " --shost SHOST" + printf " remote to host of source\n" + echo + + # :flag.usage + echo " --user, -u TUSER" + printf " user on remote host\n" + echo + + # :flag.usage + echo " --suser SUSER" + printf " remote user on source host\n" + echo + + # :flag.usage + echo " --sshcfg SSHCFG" + printf " path to sshcfg file\n" + echo + + # :flag.usage + echo " --options, -o OPTIONS" + printf " additional options (restic or rsync)\n" + echo + + # :flag.usage + echo " --include_file, -i INCLUDE" + printf " include file\n" + echo + + # :flag.usage + echo " --exclude_file, -e EXCLUDE" + printf " exclude file\n" + echo + + # :flag.usage + echo " --dir, -d" + printf " append source directory path to target directory\n" + echo + # :command.usage_args + printf "Arguments:\n" + + # :argument.usage + echo " SOURCE" + printf " source directory to be backed up, default is $PWD\n" + echo + + # :argument.usage + echo " TARGET" + printf " target directory for backup\n" + echo + # :command.usage_environment_variables + printf "Environment Variables:\n" + + # :environment_variable.usage + echo " BACKUP_EXCLUDE" + printf " path to file of excludes\n" + echo + + # :environment_variable.usage + echo " BACKUP_INCLUDE" + printf " path to directory of includes\n" + echo + + # :environment_variable.usage + echo " BACKUP_SETTINGS" + printf " path to default settings file\n" + echo + + # :environment_variable.usage + echo " BACKUP_PASSWORD" + printf " path to default settings file\n" + echo + + # :environment_variable.usage + echo " BACKUP_SERVER" + printf " URL of Restic rest server\n" + echo + + # :environment_variable.usage + echo " BACKUP_DIR" + printf " Backup Directory\n" + echo + # :command.usage_examples + printf "Examples:\n" + + printf " backup -p password . /target/dir\n" + printf " backup -s /path/to/settings/yaml/file\n" + echo + + fi +} + +# :command.inspect_args +inspect_args() { + readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort) + if (( ${#args[@]} )); then + echo args: + for k in "${sorted_keys[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done + else + echo args: none + fi + + if (( ${#other_args[@]} )); then + echo + echo other_args: + echo "- \${other_args[*]} = ${other_args[*]}" + for i in "${!other_args[@]}"; do + echo "- \${other_args[$i]} = ${other_args[$i]}" + done + fi +} + +# :command.command_functions + +# :command.parse_requirements +parse_requirements() { + # :command.fixed_flag_filter + case "$1" in + --version ) + version_command + exit + ;; + + --help ) + long_usage=yes + dbackup_usage + exit 1 + ;; + + esac + # :command.environment_variables_filter + # :command.dependencies_filter + # :command.command_filter + action="root" + # :command.required_args_filter + # :command.required_flags_filter + # :command.parse_requirements_while + while [[ $# -gt 0 ]]; do + key="$1" + case "$key" in + # :flag.case + --password | -p ) + if [[ $2 ]]; then + args[--password]="$2" + shift + shift + else + printf "%s\n" "--password requires an argument: --password, -p PASSWORD" + exit 1 + fi + ;; + + # :flag.case + --remote | -r ) + args[--remote]=1 + shift + ;; + + # :flag.case + --server ) + if [[ $2 ]]; then + args[--server]="$2" + shift + shift + else + printf "%s\n" "--server requires an argument: --server URL" + exit 1 + fi + ;; + + # :flag.case + --init ) + args[--init]=1 + shift + ;; + + # :flag.case + --snap ) + args[--snap]=1 + shift + ;; + + # :flag.case + --view | -v ) + args[--view]=1 + shift + ;; + + # :flag.case + --view-path ) + if [[ $2 ]]; then + args[--view-path]="$2" + shift + shift + else + printf "%s\n" "--view-path requires an argument: --view-path PATH" + exit 1 + fi + ;; + + # :flag.case + --prune ) + if [[ $2 ]]; then + args[--prune]="$2" + shift + shift + else + printf "%s\n" "--prune requires an argument: --prune PRUNE" + exit 1 + fi + ;; + + # :flag.case + --password | -p ) + if [[ $2 ]]; then + args[--password]="$2" + shift + shift + else + printf "%s\n" "--password requires an argument: --password, -p PASSWORD" + exit 1 + fi + ;; + + # :flag.case + --settings | -s ) + if [[ $2 ]]; then + args[--settings]="$2" + shift + shift + else + printf "%s\n" "--settings requires an argument: --settings, -s SYAML" + exit 1 + fi + ;; + + # :flag.case + --host | -h ) + if [[ $2 ]]; then + args[--host]="$2" + shift + shift + else + printf "%s\n" "--host requires an argument: --host, -h THOST" + exit 1 + fi + ;; + + # :flag.case + --shost ) + if [[ $2 ]]; then + args[--shost]="$2" + shift + shift + else + printf "%s\n" "--shost requires an argument: --shost SHOST" + exit 1 + fi + ;; + + # :flag.case + --user | -u ) + if [[ $2 ]]; then + args[--user]="$2" + shift + shift + else + printf "%s\n" "--user requires an argument: --user, -u TUSER" + exit 1 + fi + ;; + + # :flag.case + --suser ) + if [[ $2 ]]; then + args[--suser]="$2" + shift + shift + else + printf "%s\n" "--suser requires an argument: --suser SUSER" + exit 1 + fi + ;; + + # :flag.case + --sshcfg ) + if [[ $2 ]]; then + args[--sshcfg]="$2" + shift + shift + else + printf "%s\n" "--sshcfg requires an argument: --sshcfg SSHCFG" + exit 1 + fi + ;; + + # :flag.case + --options | -o ) + if [[ $2 ]]; then + args[--options]="$2" + shift + shift + else + printf "%s\n" "--options requires an argument: --options, -o OPTIONS" + exit 1 + fi + ;; + + # :flag.case + --include_file | -i ) + if [[ $2 ]]; then + args[--include_file]="$2" + shift + shift + else + printf "%s\n" "--include_file requires an argument: --include_file, -i INCLUDE" + exit 1 + fi + ;; + + # :flag.case + --exclude_file | -e ) + if [[ $2 ]]; then + args[--exclude_file]="$2" + shift + shift + else + printf "%s\n" "--exclude_file requires an argument: --exclude_file, -e EXCLUDE" + exit 1 + fi + ;; + + # :flag.case + --dir | -d ) + args[--dir]=1 + shift + ;; + + + -* ) + printf "invalid option: %s\n" "$key" + exit 1 + ;; + + * ) + # :command.parse_requirements_case + if [[ ! ${args[source]} ]]; then + args[source]=$1 + shift + elif [[ ! ${args[target]} ]]; then + args[target]=$1 + shift + else + printf "invalid argument: %s\n" "$key" + exit 1 + fi + ;; + + esac + done + # :command.default_assignments + # :command.whitelist_filter +} + +# :command.initialize +initialize() { + version="0.1.0" + long_usage='' + set -e + + # :src/initialize.sh + # Code here runs inside the initialize() function + # Use it for anything that you need to run before any other function, like + # setting environment vairables: + # CONFIG_FILE=settings.ini + # + # Feel free to empty (but not delete) this file. + echo running initialize function +} + +# :command.run +run() { + declare -A args + declare -a other_args + parse_requirements "$@" + + if [[ $action == "root" ]]; then + root_command + fi +} + +initialize +run "$@" diff --git a/all/dev/backup/src/bashly.yml b/all/dev/backup/src/bashly.yml index a870b78..5ee693f 100644 --- a/all/dev/backup/src/bashly.yml +++ b/all/dev/backup/src/bashly.yml @@ -1,59 +1,92 @@ -name: backup -help: backup/mirror a directory using rdiff or rsync +name: dbackup +help: differential backup using restic version: 0.1.0 environment_variables: -- name: BACKUP_EXCLUDE - help: path to file of excludes -- name: BACKUP_INCLUDE - help: path to directory of includes + - name: BACKUP_EXCLUDE + help: path to file of excludes + - name: BACKUP_INCLUDE + help: path to directory of includes + - name: BACKUP_SETTINGS + help: path to default settings file + - name: BACKUP_PASSWORD + help: path to default settings file + - name: BACKUP_SERVER + help: URL of Restic rest server + - name: BACKUP_DIR + help: Backup Directory args: -- name: source - # required: 1 - help: source directory to be backed up, default is $PWD -- name: target - help: "target directory for backup" + - name: source + help: source directory to be backed up, default is $PWD + - name: target + help: "target directory for backup" flags: -- long: --host - short: -h - arg: thost - help: host on remote to target to receive backup -- long: --shost - arg: shost - help: remote to host of source -- long: --user - short: -u - arg: tuser - help: user on remote host -- long: --suser - arg: suser - help: remote user on source host -- long: --sshcfg - arg: sshcfg - help: path to sshcfg file -- long: --options - short : -o - arg: options - help: rdiff options -- long: --include_file - short : -i - arg: options - help: rdiff options -- long: --exclude_file - short : -e - arg: options - help: rdiff options -- long: --mirror - short: -m - help: make a mirror copy instead of a differential snapshot -- long: --sudo - short: -s - help: run the backup as sudo -- long: --no-dir - short: -n - help: don't make a directory of same name within the target, merge sub directories + - long: --password + short: -p + arg: password + help: repo password (or file path) for backup repository + - long: --remote + short: -r + help: backup to a remote machine + - long: --server + arg: url + help: url of restic rest server + - long: --init + help: initialize repo (default is backup) + - long: --snap + help: list repo snapshots + - long: --view + short: -v + help: mount snapshot for viewing (default is BACKUP_MOUNT or /opt/backup/view) + - long: --view-path + help: set custom mount path for viewing of snapshot, --view not required if set + arg: path + - long: --prune + arg: prune + help: prune repo (default is backup). true for default prune or path to prune settings + - long: --password + short: -p + arg: password + help: repo password (or file path) for backup repository + - long: --settings + short: -s + arg: syaml + help: path to settings file (yaml). Keys are same as long + - long: --host + short: -h + arg: thost + help: host on remote to target to receive backup + - long: --shost + arg: shost + help: remote to host of source + - long: --user + short: -u + arg: tuser + help: user on remote host + - long: --suser + arg: suser + help: remote user on source host + - long: --sshcfg + arg: sshcfg + help: path to sshcfg file + - long: --options + short: -o + arg: options + help: additional options (restic or rsync) + - long: --include_file + short: -i + arg: include + help: include file + - long: --exclude_file + short: -e + arg: exclude + help: exclude file + - long: --dir + short: -d + help: append source directory path to target directory examples: -- backup -m -n -h thost --shost shost -u tuser --suser suser . /target/dir + - backup -p password . /target/dir + - backup -s /path/to/settings/yaml/file diff --git a/all/dev/backup/src/initialize.sh b/all/dev/backup/src/initialize.sh index f2dbc52..c806d9a 100644 --- a/all/dev/backup/src/initialize.sh +++ b/all/dev/backup/src/initialize.sh @@ -4,3 +4,4 @@ # CONFIG_FILE=settings.ini # # Feel free to empty (but not delete) this file. +echo running initialize function \ No newline at end of file diff --git a/all/dev/backup/src/root_command.sh b/all/dev/backup/src/root_command.sh index c477fcf..b0b1ded 100644 --- a/all/dev/backup/src/root_command.sh +++ b/all/dev/backup/src/root_command.sh @@ -1,35 +1,114 @@ -echo Running Backup Command +#!/bin/bash + +echo "running root command" inspect_args # module_load ssh module_load confirm +module_load path + +local settings=${args[--settings]} + +if [[ -f $settings ]]; then + echo loading settings file $settings + module_load yaml + eval $(parse_yaml $settings "s_") + echo $s_source + echo $s_target + +fi + +if [[ $s_server_host ]]; then + s_server="rest:http$([[ $s_server_secure ]] && echo "s")://${s_server_host}$([[ $s_server_port ]] && echo :${s_server_port})" +fi + +local password=${args[--password]:-$BACKUP_PASSWORD} +password=${password:-$s_password} +[[ ! $password ]] && echo restic requires a backup repository password, exiting && return 2 +password="RESTIC_PASSWORD=${password}" + +local server=${args[--server]:-$BACKUP_SERVER} +server=${server:-$s_server} + +local backup_dir=${args[--backup_dir]:-$BACKUP_DIR} +backup_dir=${backup_dir:-$s_backup_dir} +backup_dir=${backup_dir:-"/backup"} + +local source="${args[source]:-$s_source}" +source=$(echo "${source:-$PWD}" | tr -s /) + +local target=${args[target]:-$s_target} +target="$(echo $target | tr -s /)" +if [[ ${args[--dir]:-$s_dir} ]]; then + target="${target}$(echo $source | tr -s / | sed -e "s#^[.]##")" +fi +if [[ $(isAbsPath $target) ]]; then + echo absolute target directory +else + target="/${HOSTNAME}/${target}" + if [[ $server ]]; then + target="${server}${target}" + else + target=${backup_dir}${target} + fi +fi + + +local exclude=${args[--exclude_file]:-$BACKUP_EXCLUDE} +exclude=${exclude:-$s_exclude} +exclude=${exclude:-"$source/exclude.bac"} + -local source=${args[source]} -local target=${args[target]} local shost=$([[ ${args[--shost]} ]] && echo ${args[--shost]}::) local suser=$([[ ${args[--suser]} ]] && echo ${args[--suser]}@) local thost=$([[ ${args[--host]} ]] && echo ${args[--host]}::) local tuser=$([[ ${args[--user]} ]] && echo ${args[--user]}@) -local mirror=${args[--mirror]} + local options=$(echo ${args[--options]} | awk '{gsub(/\\/," ")}1') -local sudo=$([[ ${args[--sudo]} ]] && echo sudo) -local exclude=${args[--exclude_file]} -if [[ ! $exclude ]]; then -echo checking local excludes -[[ -f $source/exclude.bac ]] && exclude="$source/exclude.bac" -# ls $source -# [[ $EXCLUDE_BACKUP ]] && exclude="$BACKUP_EXCLUDE" -fi +local bin=$(command -v restic) -exclude=$([[ $exclude ]] && echo --exclude-globbing-filelist $exclude) -echo exclude file: $exclude +local cmd=${args[cmd]:-"backup"} + + +echo before exists exclude: $exclude + +exclude=$([[ -f $exclude ]] && echo "--iexclude-file $exclude" || echo "") + +echo source: $source +echo target $target +echo exclude: $exclude + # local ssh="--remote-schema \"ssh -C %s /home/sysadmin/.local/bin/rdiff-backup --server\"" -cmd="$sudo rdiff-backup $options $exclude $ssh ${suser}${shost}$source ${tuser}${thost}$target" +#cmd="$sudo rdiff-backup $options $exclude $ssh ${suser}${shost}$source ${tuser}${thost}$target" + + +cmd="sudo $password $bin -r $target backup $source $exclude" + +if [[ ${args[--init]} ]]; then cmd="sudo $password $bin -r $target init"; fi +if [[ ${args[--snap]} ]]; then cmd="sudo $password $bin -r $target snapshots"; fi +if [[ ${args[--prune]} ]]; then cmd="sudo password $bin -r $target prune"; fi +if [[ ${args[--view]} || ${args[--view-path]} ]]; then + mount=${args[--view-path]:-$BACKUP_MOUNT} + mount=${mount:-"/opt/backup/view/"} + echo view mount point $mount + if [[ -e ${mount} ]]; then + cmd="$password $bin -r $target mount $mount"; + echo browse files at $mount/snapshots/latest${source} + else + echo $mount: directory for mounting snapshot for viewing does not exist. Create and try again + return 3 + fi +fi + echo $cmd confirm run this command? || return 1 -eval $cmd \ No newline at end of file +eval $cmd + +# sudo chown -R $USER:$USER $target +# sudo chown -R $USER:$USER /home/$USER/.cache/restic +# sudo chmod -R g+rwX /home/$USER/.cache/restic \ No newline at end of file diff --git a/all/dev/backup/test b/all/dev/backup/test index ca61632..2d13313 100755 --- a/all/dev/backup/test +++ b/all/dev/backup/test @@ -1,4 +1,4 @@ shopt -s expand_aliases source ../../app/bashly bashly generate -./backup "$@" \ No newline at end of file +./dbackup -s /backup/local/jobs/remote/hacking.yml $1 \ No newline at end of file From b6e3f1bb2b987d83b9d134c7a01065c210a6f3ca Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Sun, 29 Aug 2021 09:30:12 -0700 Subject: [PATCH 04/13] fix: server string --- all/dev/backup/dbackup | 12 +++++++++--- all/dev/backup/src/root_command.sh | 12 +++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/all/dev/backup/dbackup b/all/dev/backup/dbackup index 11931f5..edd5141 100755 --- a/all/dev/backup/dbackup +++ b/all/dev/backup/dbackup @@ -23,13 +23,16 @@ root_command() { eval $(parse_yaml $settings "s_") echo $s_source echo $s_target + echo $s_host fi if [[ $s_server_host ]]; then - s_server="rest:http$([[ $s_server_secure ]] && echo "s")://${s_server_host}$([[ $s_server_port ]] && echo :${s_server_port})" + s_server="http$([[ $s_server_secure ]] && echo "s")://${s_server_host}$([[ $s_server_port ]] && echo :${s_server_port} || echo "")" fi + + local password=${args[--password]:-$BACKUP_PASSWORD} password=${password:-$s_password} [[ ! $password ]] && echo restic requires a backup repository password, exiting && return 2 @@ -38,6 +41,9 @@ root_command() { local server=${args[--server]:-$BACKUP_SERVER} server=${server:-$s_server} + echo server $s_server + echo server $server + local backup_dir=${args[--backup_dir]:-$BACKUP_DIR} backup_dir=${backup_dir:-$s_backup_dir} backup_dir=${backup_dir:-"/backup"} @@ -53,9 +59,9 @@ root_command() { if [[ $(isAbsPath $target) ]]; then echo absolute target directory else - target="/${HOSTNAME}/${target}" + target="/${s_host:-$HOSTNAME}/${target}" if [[ $server ]]; then - target="${server}${target}" + target="rest:${server}${target}" else target=${backup_dir}${target} fi diff --git a/all/dev/backup/src/root_command.sh b/all/dev/backup/src/root_command.sh index b0b1ded..7590110 100644 --- a/all/dev/backup/src/root_command.sh +++ b/all/dev/backup/src/root_command.sh @@ -16,13 +16,16 @@ if [[ -f $settings ]]; then eval $(parse_yaml $settings "s_") echo $s_source echo $s_target + echo $s_host fi if [[ $s_server_host ]]; then - s_server="rest:http$([[ $s_server_secure ]] && echo "s")://${s_server_host}$([[ $s_server_port ]] && echo :${s_server_port})" + s_server="http$([[ $s_server_secure ]] && echo "s")://${s_server_host}$([[ $s_server_port ]] && echo :${s_server_port} || echo "")" fi + + local password=${args[--password]:-$BACKUP_PASSWORD} password=${password:-$s_password} [[ ! $password ]] && echo restic requires a backup repository password, exiting && return 2 @@ -31,6 +34,9 @@ password="RESTIC_PASSWORD=${password}" local server=${args[--server]:-$BACKUP_SERVER} server=${server:-$s_server} +echo server $s_server +echo server $server + local backup_dir=${args[--backup_dir]:-$BACKUP_DIR} backup_dir=${backup_dir:-$s_backup_dir} backup_dir=${backup_dir:-"/backup"} @@ -46,9 +52,9 @@ fi if [[ $(isAbsPath $target) ]]; then echo absolute target directory else - target="/${HOSTNAME}/${target}" + target="/${s_host:-$HOSTNAME}/${target}" if [[ $server ]]; then - target="${server}${target}" + target="rest:${server}${target}" else target=${backup_dir}${target} fi From 2a3f2ed19144f45d86892a25caa8fdb43cfb2ea8 Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Sun, 29 Aug 2021 11:23:15 -0700 Subject: [PATCH 05/13] make sudo optional, set a prefix command common to all --- all/dev/backup/dbackup | 13 +++++++------ all/dev/backup/src/root_command.sh | 13 +++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/all/dev/backup/dbackup b/all/dev/backup/dbackup index edd5141..5d850ca 100755 --- a/all/dev/backup/dbackup +++ b/all/dev/backup/dbackup @@ -98,19 +98,20 @@ root_command() { # local ssh="--remote-schema \"ssh -C %s /home/sysadmin/.local/bin/rdiff-backup --server\"" #cmd="$sudo rdiff-backup $options $exclude $ssh ${suser}${shost}$source ${tuser}${thost}$target" + local sudo="" + local pcmd="${sudo} ${password} ${bin} -r ${target}" + local cmd="${pcmd} backup ${source} ${exclude}" - cmd="sudo $password $bin -r $target backup $source $exclude" - - if [[ ${args[--init]} ]]; then cmd="sudo $password $bin -r $target init"; fi - if [[ ${args[--snap]} ]]; then cmd="sudo $password $bin -r $target snapshots"; fi - if [[ ${args[--prune]} ]]; then cmd="sudo password $bin -r $target prune"; fi + if [[ ${args[--init]} ]]; then cmd="${pcmd} init"; fi + if [[ ${args[--snap]} ]]; then cmd="${pcmd} snapshots"; fi + if [[ ${args[--prune]} ]]; then cmd="${pcmd} prune"; fi if [[ ${args[--view]} || ${args[--view-path]} ]]; then mount=${args[--view-path]:-$BACKUP_MOUNT} mount=${mount:-"/opt/backup/view/"} echo view mount point $mount if [[ -e ${mount} ]]; then - cmd="$password $bin -r $target mount $mount"; + cmd="${pcmd} mount $mount"; echo browse files at $mount/snapshots/latest${source} else echo $mount: directory for mounting snapshot for viewing does not exist. Create and try again diff --git a/all/dev/backup/src/root_command.sh b/all/dev/backup/src/root_command.sh index 7590110..37ef0fb 100644 --- a/all/dev/backup/src/root_command.sh +++ b/all/dev/backup/src/root_command.sh @@ -91,19 +91,20 @@ echo exclude: $exclude # local ssh="--remote-schema \"ssh -C %s /home/sysadmin/.local/bin/rdiff-backup --server\"" #cmd="$sudo rdiff-backup $options $exclude $ssh ${suser}${shost}$source ${tuser}${thost}$target" +local sudo="" +local pcmd="${sudo} ${password} ${bin} -r ${target}" +local cmd="${pcmd} backup ${source} ${exclude}" -cmd="sudo $password $bin -r $target backup $source $exclude" - -if [[ ${args[--init]} ]]; then cmd="sudo $password $bin -r $target init"; fi -if [[ ${args[--snap]} ]]; then cmd="sudo $password $bin -r $target snapshots"; fi -if [[ ${args[--prune]} ]]; then cmd="sudo password $bin -r $target prune"; fi +if [[ ${args[--init]} ]]; then cmd="${pcmd} init"; fi +if [[ ${args[--snap]} ]]; then cmd="${pcmd} snapshots"; fi +if [[ ${args[--prune]} ]]; then cmd="${pcmd} prune"; fi if [[ ${args[--view]} || ${args[--view-path]} ]]; then mount=${args[--view-path]:-$BACKUP_MOUNT} mount=${mount:-"/opt/backup/view/"} echo view mount point $mount if [[ -e ${mount} ]]; then - cmd="$password $bin -r $target mount $mount"; + cmd="${pcmd} mount $mount"; echo browse files at $mount/snapshots/latest${source} else echo $mount: directory for mounting snapshot for viewing does not exist. Create and try again From 5dd0dbc469e38ee5ac5c5c989237a9894a847deb Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Tue, 31 Aug 2021 11:14:15 -0700 Subject: [PATCH 06/13] add host arg --- all/dev/backup/dbackup | 9 +++------ all/dev/backup/src/root_command.sh | 9 +++------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/all/dev/backup/dbackup b/all/dev/backup/dbackup index 5d850ca..ac1c796 100755 --- a/all/dev/backup/dbackup +++ b/all/dev/backup/dbackup @@ -31,8 +31,6 @@ root_command() { s_server="http$([[ $s_server_secure ]] && echo "s")://${s_server_host}$([[ $s_server_port ]] && echo :${s_server_port} || echo "")" fi - - local password=${args[--password]:-$BACKUP_PASSWORD} password=${password:-$s_password} [[ ! $password ]] && echo restic requires a backup repository password, exiting && return 2 @@ -41,8 +39,8 @@ root_command() { local server=${args[--server]:-$BACKUP_SERVER} server=${server:-$s_server} - echo server $s_server - echo server $server + local host=${args[--host]:-$s_host} + if [[ $host ]]; then host="-H $host"; fi local backup_dir=${args[--backup_dir]:-$BACKUP_DIR} backup_dir=${backup_dir:-$s_backup_dir} @@ -94,14 +92,13 @@ root_command() { echo target $target echo exclude: $exclude - # local ssh="--remote-schema \"ssh -C %s /home/sysadmin/.local/bin/rdiff-backup --server\"" #cmd="$sudo rdiff-backup $options $exclude $ssh ${suser}${shost}$source ${tuser}${thost}$target" local sudo="" local pcmd="${sudo} ${password} ${bin} -r ${target}" - local cmd="${pcmd} backup ${source} ${exclude}" + local cmd="${pcmd} ${host} backup ${source} ${exclude}" if [[ ${args[--init]} ]]; then cmd="${pcmd} init"; fi if [[ ${args[--snap]} ]]; then cmd="${pcmd} snapshots"; fi diff --git a/all/dev/backup/src/root_command.sh b/all/dev/backup/src/root_command.sh index 37ef0fb..9ee6366 100644 --- a/all/dev/backup/src/root_command.sh +++ b/all/dev/backup/src/root_command.sh @@ -24,8 +24,6 @@ if [[ $s_server_host ]]; then s_server="http$([[ $s_server_secure ]] && echo "s")://${s_server_host}$([[ $s_server_port ]] && echo :${s_server_port} || echo "")" fi - - local password=${args[--password]:-$BACKUP_PASSWORD} password=${password:-$s_password} [[ ! $password ]] && echo restic requires a backup repository password, exiting && return 2 @@ -34,8 +32,8 @@ password="RESTIC_PASSWORD=${password}" local server=${args[--server]:-$BACKUP_SERVER} server=${server:-$s_server} -echo server $s_server -echo server $server +local host=${args[--host]:-$s_host} +if [[ $host ]]; then host="-H $host"; fi local backup_dir=${args[--backup_dir]:-$BACKUP_DIR} backup_dir=${backup_dir:-$s_backup_dir} @@ -87,14 +85,13 @@ echo source: $source echo target $target echo exclude: $exclude - # local ssh="--remote-schema \"ssh -C %s /home/sysadmin/.local/bin/rdiff-backup --server\"" #cmd="$sudo rdiff-backup $options $exclude $ssh ${suser}${shost}$source ${tuser}${thost}$target" local sudo="" local pcmd="${sudo} ${password} ${bin} -r ${target}" -local cmd="${pcmd} backup ${source} ${exclude}" +local cmd="${pcmd} ${host} backup ${source} ${exclude}" if [[ ${args[--init]} ]]; then cmd="${pcmd} init"; fi if [[ ${args[--snap]} ]]; then cmd="${pcmd} snapshots"; fi From e548e543383bfb49933711f98f9045fb92d43226 Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Sun, 5 Sep 2021 08:24:53 -0700 Subject: [PATCH 07/13] fix: make rclone install script usable --- all/modules/install/rclone-install.func | 196 ++++-------------------- 1 file changed, 27 insertions(+), 169 deletions(-) diff --git a/all/modules/install/rclone-install.func b/all/modules/install/rclone-install.func index af1073b..1ba88d2 100644 --- a/all/modules/install/rclone-install.func +++ b/all/modules/install/rclone-install.func @@ -1,171 +1,29 @@ -#!/bin/bash - -rclone_install () { - -local INSTALL_DIR="/opt" -local BIN_DIR="/opt/bin" -local FORCE=false -local VERSION=current - -declare OPTION -declare OPTARG -declare OPTIND - -while getopts 'i:b:v:f' OPTION; do - case "$OPTION" in - i) - INSTALL_DIR=$OPTARG - ;; - b) - BIN_DIR=$OPTARG - ;; - - v) - echo "Installing Beta" - VERSION=$OPTARG - ;; - f) - echo "FORCING download/overwrite" - FORCE=true - ;; - esac -done - -shift $(( OPTIND - 1 )) - -# error codes -# 0 - exited without problems -# 1 - parameters not supported were used or some unexpected error occurred -# 2 - OS or Architecture not supported by this script -# 3 - installed version of rclone is up to date -# 4 - supported unzip tools are not available -# 5 - unable to install without sudo permission - -[[ ! -w "$INSTALL_DIR" ]] && echo $INSTALL is not writable by user $USER, can\'t install there && exit 5 -[[ ! -w "$BIN_DIR" ]] && echo $BIN_DIR is not writable by user $USER, won\'t be able to link binary there && exit 5 -INSTALL_DIR=$INSTALL_DIR/rclone -# if [[ $EUID != 0 ]]; then -# sudo "$0" "$@" -# exit $? -# fi - -#detect the platform -set +e - -OS="`uname`" -case $OS in - Linux) - OS='linux' - ;; - *) - echo 'Only Linux OS not supported by this script' - exit 2 - ;; -esac - -OS_type="`uname -m`" -case $OS_type in - x86_64|amd64) - OS_type='amd64' - ;; - i?86|x86) - OS_type='386' - ;; - arm*) - OS_type='arm' - ;; - aarch64) - OS_type='arm64' - ;; - *) - echo 'OS architecture not supported' - exit 2 - ;; -esac - -set -e - -#when adding a tool to the list make sure to also add its corresponding command further in the script - -# exit if no unzip tools available -[[ -z "unzip" ]] && printf "Please install unzip and try again.\n\n" && exit 4 - -# Make sure we don't create a root owned .config/rclone directory #2127 -# export XDG_CONFIG_HOME=config - -#check installed version of rclone to determine if update is necessary -# installed_version=`rclone --version 2>>errors | head -n 1` -# if [ -z "${install_beta}" ]; then -# current_version=`curl https://downloads.rclone.org/version.txt` -# else -# current_version=`curl https://beta.rclone.org/version.txt` -# fi -# -# if [ "$version" = "$current_version" ]; then -# printf "\nThe latest ${install_beta}version of rclone ${version} is already installed.\n\n" -# exit 3 -# fi - -echo Installing to $INSTALL_DIR -mkdir -p $INSTALL_DIR -cd $INSTALL_DIR - -# mv rclone.bin rclone.bin.prev - -#create tmp directory and move to it with macOS compatibility fallback -# mkdir $INSTALL_DIR/zip -# $(mktemp -d -f rclone-zip) -# cd $zip_dir - -# echo made temp -#download and unzip -if [[ $VERSION = "current" ]]; then - download_link="https://downloads.rclone.org/rclone-current-$OS-$OS_type.zip" - rclone_zip="rclone-current-$OS-$OS_type.zip" -else - download_link="https://beta.rclone.org/rclone-beta-latest-$OS-$OS_type.zip" - rclone_zip="rclone-beta-latest-$OS-$OS_type.zip" -fi - -echo downloading rclone binary archive $download_link -# curl -O $download_link -echo expanding archive -unzip -j -o -a $rclone_zip -mkdir -p $INSTALL_DIR/docs 2> /dev/null || true -rm $INSTALL_DIR/docs/*.* 2> /dev/null || true -echo moving documentation files to docs/ -mv *.1 *.txt README.* $INSTALL_DIR/docs/ 2> /dev/null || true -# rm *.zip -echo preparing customized rclone command -rm -rf rclone.bin -mv rclone rclone.bin -chown :sudo rclone.bin -chmod 775 rclone.bin -touch $INSTALL_DIR/rclone -touch $INSTALL_DIR/rclone.conf -mkdir $INSTALL_DIR/cache 2> /dev/null || true -echo '#!/bin/bash' > $INSTALL_DIR/rclone -CMD=$(echo $INSTALL_DIR/rclone.bin \ - --config=$INSTALL_DIR/rclone.conf \ - --cache-dir=$INSTALL_DIR/cache \ - '$@' \ +sudo rclone.bin + chmod 775 rclone.bin + touch $INSTALL_DIR/rclone + touch $INSTALL_DIR/rclone.conf + mkdir $INSTALL_DIR/cache 2> /dev/null || true + echo '#!/bin/bash' > $INSTALL_DIR/rclone + CMD=$(echo $INSTALL_DIR/rclone.bin \ + --config=$INSTALL_DIR/rclone.conf \ + --cache-dir=$INSTALL_DIR/cache \ + '$@' \ ) -echo "$CMD" >> $INSTALL_DIR/rclone -chown :sudo rclone -chmod 775 rclone -rm $BIN_DIR/rclone -ln -s $INSTALL_DIR/rclone $BIN_DIR/rclone -echo -e "rclone customized command can be launched from echo where: $(command -v rclone) \n $CMD" -cd .. - - - - -#update version variable post install -version=`rclone --version 2>>errors | head -n 1` - -printf "\n${version} has successfully installed." -printf '\nNow run "rclone config" for setup. Check https://rclone.org/docs/ for more details.\n\n' -exit 0 - + echo "$CMD" >> $INSTALL_DIR/rclone + chown :sudo rclone + chmod 775 rclone + rm $BIN_DIR/rclone + ln -s $INSTALL_DIR/rclone $BIN_DIR/rclone + echo -e "rclone customized command can be launched from $(command -v rclone) \n $CMD" + echo -e "uncustomized rclone can be run from $INSTALL_DIR/rclone.bin" + cd .. + + + #update version variable post install + version=`rclone --version 2>>errors | head -n 1` + + printf "\n${last_version} has successfully installed." + printf '\nNow run "rclone config" to set up remotes. Check https://rclone.org/docs/ for more details.\n\n' + return 0 + } From a04f0cb2d9162b9c94ac86f8a60f079878757e1e Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Mon, 6 Sep 2021 10:54:54 -0700 Subject: [PATCH 08/13] clean hugo install script --- all/dev/backup/dbackup | 62 ++++-- all/dev/backup/src/bashly.yml | 4 +- all/dev/backup/src/root_command.sh | 54 ++++-- all/modules/install/hugo-install.func | 268 +++++++++++++------------- 4 files changed, 222 insertions(+), 166 deletions(-) diff --git a/all/dev/backup/dbackup b/all/dev/backup/dbackup index ac1c796..877a173 100755 --- a/all/dev/backup/dbackup +++ b/all/dev/backup/dbackup @@ -39,32 +39,55 @@ root_command() { local server=${args[--server]:-$BACKUP_SERVER} server=${server:-$s_server} - local host=${args[--host]:-$s_host} - if [[ $host ]]; then host="-H $host"; fi + local hostname=${args[--hostname]:-$s_hostname} + hostname=${hostname:-$s_host} + hostname=${hostname:-$HOSTNAME} local backup_dir=${args[--backup_dir]:-$BACKUP_DIR} backup_dir=${backup_dir:-$s_backup_dir} backup_dir=${backup_dir:-"/backup"} + local smount=${args[--source_mount]:-$BACKUP_SOURCE_MOUNT} + smount=${smount:-$s_source_mount} + + echo smount: $smount $s_source_mount + + + local tmount=${args[--target_mount]:-$BACKUP_TARGET_MOUNT} + tmount=${tmount:-$s_target_mount} + + echo tmount: $tmount $s_target_mount + local source="${args[source]:-$s_source}" + source="${source:-$s_source_path}" source=$(echo "${source:-$PWD}" | tr -s /) + echo yaml source $s_source_path $s_source_mount + echo source $source + + echo target $s_target + + echo target path $s_target_path + local target=${args[target]:-$s_target} - target="$(echo $target | tr -s /)" - if [[ ${args[--dir]:-$s_dir} ]]; then - target="${target}$(echo $source | tr -s / | sed -e "s#^[.]##")" - fi - if [[ $(isAbsPath $target) ]]; then - echo absolute target directory + target=${target:-$s_target_path} + target=${target:-$(echo "${source}" | tr -s / | sed -e "s#^[.]##")} + target="$(echo "${target}" | tr -s /)" + echo "target> $target" + if [[ ${tmount} ]]; then + target="${tmount}${target}" else - target="/${s_host:-$HOSTNAME}/${target}" - if [[ $server ]]; then - target="rest:${server}${target}" - else - target=${backup_dir}${target} - fi + target="/${hostname}${target}" fi + if [[ $server ]]; then + target="rest:${server}${target}" + fi + + if [[ $smount ]]; then + setpath="--set-path ${source}" + source=${smount}${source} + fi local exclude=${args[--exclude_file]:-$BACKUP_EXCLUDE} exclude=${exclude:-$s_exclude} @@ -95,10 +118,12 @@ root_command() { # local ssh="--remote-schema \"ssh -C %s /home/sysadmin/.local/bin/rdiff-backup --server\"" #cmd="$sudo rdiff-backup $options $exclude $ssh ${suser}${shost}$source ${tuser}${thost}$target" + + local sudo="" local pcmd="${sudo} ${password} ${bin} -r ${target}" - local cmd="${pcmd} ${host} backup ${source} ${exclude}" + local cmd="${pcmd} -H ${hostname} ${setpath} backup ${source} ${exclude}" if [[ ${args[--init]} ]]; then cmd="${pcmd} init"; fi if [[ ${args[--snap]} ]]; then cmd="${pcmd} snapshots"; fi @@ -188,7 +213,7 @@ dbackup_usage() { # :flag.usage echo " --view-path PATH" - printf " set custom mount path for viewing of snapshot, --view not required if set\n" + printf " set custom mount point path for viewing of snapshot, --view not required if\n set\n" echo # :flag.usage @@ -294,6 +319,11 @@ dbackup_usage() { echo " BACKUP_DIR" printf " Backup Directory\n" echo + + # :environment_variable.usage + echo " BACKUP_MOUNT_POINT" + printf " mount point of source if mounted external to host\n" + echo # :command.usage_examples printf "Examples:\n" diff --git a/all/dev/backup/src/bashly.yml b/all/dev/backup/src/bashly.yml index 5ee693f..30c0757 100644 --- a/all/dev/backup/src/bashly.yml +++ b/all/dev/backup/src/bashly.yml @@ -15,6 +15,8 @@ environment_variables: help: URL of Restic rest server - name: BACKUP_DIR help: Backup Directory + - name: BACKUP_MOUNT_POINT + help: mount point of source if mounted external to host args: - name: source @@ -41,7 +43,7 @@ flags: short: -v help: mount snapshot for viewing (default is BACKUP_MOUNT or /opt/backup/view) - long: --view-path - help: set custom mount path for viewing of snapshot, --view not required if set + help: set custom mount point path for viewing of snapshot, --view not required if set arg: path - long: --prune arg: prune diff --git a/all/dev/backup/src/root_command.sh b/all/dev/backup/src/root_command.sh index 9ee6366..280df2b 100644 --- a/all/dev/backup/src/root_command.sh +++ b/all/dev/backup/src/root_command.sh @@ -32,32 +32,55 @@ password="RESTIC_PASSWORD=${password}" local server=${args[--server]:-$BACKUP_SERVER} server=${server:-$s_server} -local host=${args[--host]:-$s_host} -if [[ $host ]]; then host="-H $host"; fi +local hostname=${args[--hostname]:-$s_hostname} +hostname=${hostname:-$s_host} +hostname=${hostname:-$HOSTNAME} local backup_dir=${args[--backup_dir]:-$BACKUP_DIR} backup_dir=${backup_dir:-$s_backup_dir} backup_dir=${backup_dir:-"/backup"} +local smount=${args[--source_mount]:-$BACKUP_SOURCE_MOUNT} +smount=${smount:-$s_source_mount} + +echo smount: $smount $s_source_mount + + +local tmount=${args[--target_mount]:-$BACKUP_TARGET_MOUNT} +tmount=${tmount:-$s_target_mount} + +echo tmount: $tmount $s_target_mount + local source="${args[source]:-$s_source}" +source="${source:-$s_source_path}" source=$(echo "${source:-$PWD}" | tr -s /) +echo yaml source $s_source_path $s_source_mount +echo source $source + +echo target $s_target + +echo target path $s_target_path + local target=${args[target]:-$s_target} -target="$(echo $target | tr -s /)" -if [[ ${args[--dir]:-$s_dir} ]]; then - target="${target}$(echo $source | tr -s / | sed -e "s#^[.]##")" -fi -if [[ $(isAbsPath $target) ]]; then - echo absolute target directory +target=${target:-$s_target_path} +target=${target:-$(echo "${source}" | tr -s / | sed -e "s#^[.]##")} +target="$(echo "${target}" | tr -s /)" +echo "target> $target" +if [[ ${tmount} ]]; then + target="${tmount}${target}" else - target="/${s_host:-$HOSTNAME}/${target}" - if [[ $server ]]; then - target="rest:${server}${target}" - else - target=${backup_dir}${target} - fi + target="/${hostname}${target}" fi +if [[ $server ]]; then + target="rest:${server}${target}" +fi + +if [[ $smount ]]; then + setpath="--set-path ${source}" + source=${smount}${source} +fi local exclude=${args[--exclude_file]:-$BACKUP_EXCLUDE} exclude=${exclude:-$s_exclude} @@ -88,10 +111,11 @@ echo exclude: $exclude # local ssh="--remote-schema \"ssh -C %s /home/sysadmin/.local/bin/rdiff-backup --server\"" #cmd="$sudo rdiff-backup $options $exclude $ssh ${suser}${shost}$source ${tuser}${thost}$target" + local sudo="" local pcmd="${sudo} ${password} ${bin} -r ${target}" -local cmd="${pcmd} ${host} backup ${source} ${exclude}" +local cmd="${pcmd} -H ${hostname} ${setpath} backup ${source} ${exclude}" if [[ ${args[--init]} ]]; then cmd="${pcmd} init"; fi if [[ ${args[--snap]} ]]; then cmd="${pcmd} snapshots"; fi diff --git a/all/modules/install/hugo-install.func b/all/modules/install/hugo-install.func index 93033df..6ffaa2e 100755 --- a/all/modules/install/hugo-install.func +++ b/all/modules/install/hugo-install.func @@ -1,139 +1,139 @@ #!/bin/bash hugo_install () { -# inspried from this forum post https://discourse.gohugo.io/t/script-to-install-latest-hugo-release-on-macos-and-ubuntu/14774/10 -# if you have run into github api anonymous access limits which happens during debugging/dev then add user and token here or sourced from a separate file -# . ~/githubapitoken -#GITHUB_USER="" -#GITHUB_TOKEN="" - -if [ "$GITHUB_TOKEN" != "" ]; then -echo using access token with script -echo $GITHUB_USER $GITHUB_TOKEN -fi - -EXTENDED=false -FORCE=false -EFILE="" - - -# options -# e - download and install the extended version -# c - use 'hugoe' as the install command for extended version otherwise 'hugo' will launch extended version -# f - force download/overwrite of same version - -declare OPTION -declare OPTARG -declare OPTIND - -while getopts 'ecf' OPTION; do - case "$OPTION" in - e) - echo "installing extended hugo" - EXTENDED=true - ;; - c) + # inspried from this forum post https://discourse.gohugo.io/t/script-to-install-latest-hugo-release-on-macos-and-ubuntu/14774/10 + # if you have run into github api anonymous access limits which happens during debugging/dev then add user and token here or sourced from a separate file + # . ~/githubapitoken + #GITHUB_USER="" + #GITHUB_TOKEN="" + + if [ "$GITHUB_TOKEN" != "" ]; then + echo using access token with script + echo $GITHUB_USER $GITHUB_TOKEN + fi + + EXTENDED=false + FORCE=false + EFILE="" + + + # options + # e - download and install the extended version + # c - use 'hugoe' as the install command for extended version otherwise 'hugo' will launch extended version + # f - force download/overwrite of same version + + declare OPTION + declare OPTARG + declare OPTIND + + while getopts 'ecf' OPTION; do + case "$OPTION" in + e) + echo "installing extended hugo" + EXTENDED=true + ;; + c) + if [ $EXTENDED = true ]; then + EFILE="e" + echo using hugoe for extended command + fi + ;; + f) + echo "FORCING download/overwrite" + FORCE=true + ;; + esac + done + + shift $(( OPTIND - 1 )) + + DEFAULT_BIN_DIR="/usr/local/bin" + # Single optional argument is directory in which to install hugo + BIN_DIR=${1:-"$DEFAULT_BIN_DIR"} + + BIN_PATH="$(which hugo$EFILE)" + declare -A ARCHES + ARCHES=( ["arm64"]="ARM64" ["aarch64"]="ARM64" ["x86_64"]="64bit" ["arm32"]="ARM" ["armhf"]="ARM" ) + ARCH=$(arch) + + if [ -z "${ARCHES[$ARCH]}" ]; then + echo Your machine kernel architecture $ARCH is not supported by this script, aborting + exit 1 + fi + + + INSTALLED="$(hugo$EFILE version 2>/dev/null | cut -d'v' -f2 | cut -c 1-6)" + CUR_VERSION=${INSTALLED:-"None"} + echo $(curl -u $GITHUB_USER:$GITHUB_TOKEN -s https://api.github.com/repos/gohugoio/hugo/releases/latest | grep tag_name) + NEW_VERSION="$(curl -u $GITHUB_USER:$GITHUB_TOKEN -s https://api.github.com/repos/gohugoio/hugo/releases/latest \ + | grep tag_name \ + | cut -d'v' -f2 | cut -c 1-6)" + + echo "Hugo `[ $EXTENDED == true ] && echo "Extended"`: Current Version : $CUR_VERSION => New Version: $NEW_VERSION" + + if [ -z "$NEW_VERSION" ]; then + echo Unable to retrieve new version number - Likely you have reached github anonymous limit + echo set environment variable `$GITHUB_USER` and `$GITHUB_TOKEN` and try again + exit 1 + fi + + [[ $NEW_VERSION = $CUR_VERSION ]] && [[ $FORCE = false ]] && echo Latest version already installed at $BIN_PATH && return 0 + + pushd /tmp/ > /dev/null + + URL=$(curl -u $GITHUB_USER:$GITHUB_TOKEN -s https://api.github.com/repos/gohugoio/hugo/releases/latest \ + | grep "browser_download_url.*hugo.*._Linux-${ARCHES[$ARCH]}\.tar\.gz" \ + | \ if [ $EXTENDED = true ]; then - EFILE="e" - echo using hugoe for extended command - fi - ;; - f) - echo "FORCING download/overwrite" - FORCE=true - ;; - esac -done - -shift $(( OPTIND - 1 )) - -DEFAULT_BIN_DIR="/usr/local/bin" -# Single optional argument is directory in which to install hugo -BIN_DIR=${1:-"$DEFAULT_BIN_DIR"} - -BIN_PATH="$(which hugo$EFILE)" -declare -A ARCHES -ARCHES=( ["arm64"]="ARM64" ["aarch64"]="ARM64" ["x86_64"]="64bit" ["arm32"]="ARM" ["armhf"]="ARM" ) -ARCH=$(arch) - -if [ -z "${ARCHES[$ARCH]}" ]; then - echo Your machine kernel architecture $ARCH is not supported by this script, aborting - exit 1 -fi - - -INSTALLED="$(hugo$EFILE version 2>/dev/null | cut -d'v' -f2 | cut -c 1-6)" -CUR_VERSION=${INSTALLED:-"None"} -echo $(curl -u $GITHUB_USER:$GITHUB_TOKEN -s https://api.github.com/repos/gohugoio/hugo/releases/latest | grep tag_name) -NEW_VERSION="$(curl -u $GITHUB_USER:$GITHUB_TOKEN -s https://api.github.com/repos/gohugoio/hugo/releases/latest \ - | grep tag_name \ - | cut -d'v' -f2 | cut -c 1-6)" - -echo "Hugo `[ $EXTENDED == true ] && echo "Extended"`: Current Version : $CUR_VERSION => New Version: $NEW_VERSION" - -if [ -z "$NEW_VERSION" ]; then - echo Unable to retrieve new version number - Likely you have reached github anonymous limit - echo set environment variable `$GITHUB_USER` and `$GITHUB_TOKEN` and try again - exit 1 -fi - -[[ $NEW_VERSION = $CUR_VERSION ]] && [[ $FORCE = false ]] && echo Latest version already installed at $BIN_PATH && return 0 - -pushd /tmp/ > /dev/null - -URL=$(curl -u $GITHUB_USER:$GITHUB_TOKEN -s https://api.github.com/repos/gohugoio/hugo/releases/latest \ -| grep "browser_download_url.*hugo.*._Linux-${ARCHES[$ARCH]}\.tar\.gz" \ -| \ -if [ $EXTENDED = true ]; then - grep "_extended" -else - grep -v "_extended" -fi \ -| cut -d ":" -f 2,3 \ -| tr -d \" \ -) - -echo $URL - -echo "Installing version $NEW_VERSION `[ $EXTENDED == true ] && echo "Extended"` " -echo "This machine's architecture is $ARCH" -echo "Downloading Tarball $URL" - - wget --user=-u $GITHUB_USER --password=$GITHUB_TOKEN -q $URL - - TARBALL=$(basename $URL) -# TARBALL="$(find . -name "*Linux-${ARCHES[$ARCH]}.tar.gz" 2>/dev/null)" -echo Expanding Tarball, $TARBALL -tar -xzf $TARBALL hugo - -chmod +x hugo - -if [ -w $BIN_DIR ]; then - echo "Installing hugo to $BIN_DIR" - mv hugo -f $BIN_DIR/hugo$EFILE -else - echo "installing hugo to $BIN_DIR (sudo)" - sudo mv -f hugo $BIN_DIR/hugo$EFILE -fi - -rm $TARBALL - -popd > /dev/null - -echo Installing hugo `[ $EXTENDED == true ] && echo "extended"` as hugo$EFILE - -BIN_PATH="$(which hugo$EFILE)" - -if [ -z "$BIN_PATH" ]; then - printf "WARNING: Installed Hugo Binary in $BIN_DIR is not in your environment path\nPATH=$PATH\n" -else - if [ "$BIN_DIR/hugo$EFILE" != "$BIN_PATH" ]; then - echo "WARNING: Just installed Hugo binary hugo$EFILE to, $BIN_DIR , conflicts with existing Hugo in $BIN_PATH" - echo "add $BIN_DIR to path and delete $BIN_PATH" - else - echo "--- Installation Confirmation ---" - printf "New Hugo binary version at $BIN_PATH is\n $($BIN_PATH version)\n" - fi -fi - + grep "_extended" + else + grep -v "_extended" + fi \ + | cut -d ":" -f 2,3 \ + | tr -d \" \ + ) + + echo $URL + + echo "Installing version $NEW_VERSION `[ $EXTENDED == true ] && echo "Extended"` " + echo "This machine's architecture is $ARCH" + echo "Downloading Tarball $URL" + + wget --user=-u $GITHUB_USER --password=$GITHUB_TOKEN -q $URL + + TARBALL=$(basename $URL) + # TARBALL="$(find . -name "*Linux-${ARCHES[$ARCH]}.tar.gz" 2>/dev/null)" + echo Expanding Tarball, $TARBALL + tar -xzf $TARBALL hugo + + chmod +x hugo + + if [ -w $BIN_DIR ]; then + echo "Installing hugo to $BIN_DIR" + mv hugo -f $BIN_DIR/hugo$EFILE + else + echo "installing hugo to $BIN_DIR (sudo)" + sudo mv -f hugo $BIN_DIR/hugo$EFILE + fi + + rm $TARBALL + + popd > /dev/null + + echo Installing hugo `[ $EXTENDED == true ] && echo "extended"` as hugo$EFILE + + BIN_PATH="$(which hugo$EFILE)" + + if [ -z "$BIN_PATH" ]; then + printf "WARNING: Installed Hugo Binary in $BIN_DIR is not in your environment path\nPATH=$PATH\n" + else + if [ "$BIN_DIR/hugo$EFILE" != "$BIN_PATH" ]; then + echo "WARNING: Just installed Hugo binary hugo$EFILE to, $BIN_DIR , conflicts with existing Hugo in $BIN_PATH" + echo "add $BIN_DIR to path and delete $BIN_PATH" + else + echo "--- Installation Confirmation ---" + printf "New Hugo binary version at $BIN_PATH is\n $($BIN_PATH version)\n" + fi + fi + } From 2302bfb5a87c18fb2522d84b598a46564aa1dd1f Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Mon, 6 Sep 2021 10:55:57 -0700 Subject: [PATCH 09/13] feat: add restic options, add jq pipe processing --- all/dev/backup/dbackup | 152 ++++++++--------------------- all/dev/backup/src/bashly.yml | 45 +++++---- all/dev/backup/src/root_command.sh | 33 +++---- 3 files changed, 77 insertions(+), 153 deletions(-) diff --git a/all/dev/backup/dbackup b/all/dev/backup/dbackup index 877a173..47d6169 100755 --- a/all/dev/backup/dbackup +++ b/all/dev/backup/dbackup @@ -43,9 +43,8 @@ root_command() { hostname=${hostname:-$s_host} hostname=${hostname:-$HOSTNAME} - local backup_dir=${args[--backup_dir]:-$BACKUP_DIR} - backup_dir=${backup_dir:-$s_backup_dir} - backup_dir=${backup_dir:-"/backup"} + local options="${args[--options]:-$BACKUP_OPTIONS}" + options="${options:-$s_options}" local smount=${args[--source_mount]:-$BACKUP_SOURCE_MOUNT} smount=${smount:-$s_source_mount} @@ -85,7 +84,7 @@ root_command() { fi if [[ $smount ]]; then - setpath="--set-path ${source}" + setpath="--set-path ${source} --tag mount-point:${smount}" source=${smount}${source} fi @@ -94,34 +93,24 @@ root_command() { exclude=${exclude:-"$source/exclude.bac"} - local shost=$([[ ${args[--shost]} ]] && echo ${args[--shost]}::) - local suser=$([[ ${args[--suser]} ]] && echo ${args[--suser]}@) - local thost=$([[ ${args[--host]} ]] && echo ${args[--host]}::) - local tuser=$([[ ${args[--user]} ]] && echo ${args[--user]}@) + # local shost=$([[ ${args[--shost]} ]] && echo ${args[--shost]}::) + # local suser=$([[ ${args[--suser]} ]] && echo ${args[--suser]}@) + # local thost=$([[ ${args[--host]} ]] && echo ${args[--host]}::) + # local tuser=$([[ ${args[--user]} ]] && echo ${args[--user]}@) local options=$(echo ${args[--options]} | awk '{gsub(/\\/," ")}1') local bin=$(command -v restic) - - local cmd=${args[cmd]:-"backup"} - - - echo before exists exclude: $exclude - exclude=$([[ -f $exclude ]] && echo "--iexclude-file $exclude" || echo "") echo source: $source echo target $target echo exclude: $exclude - # local ssh="--remote-schema \"ssh -C %s /home/sysadmin/.local/bin/rdiff-backup --server\"" + local sudo=$([[ ${args[--sudo]} || $s_sudo || $BACKUP_SUDO ]] && echo "sudo" || echo "") - #cmd="$sudo rdiff-backup $options $exclude $ssh ${suser}${shost}$source ${tuser}${thost}$target" - - - local sudo="" - local pcmd="${sudo} ${password} ${bin} -r ${target}" + local pcmd="${sudo} ${password} ${bin} ${options} -r ${target}" local cmd="${pcmd} -H ${hostname} ${setpath} backup ${source} ${exclude}" @@ -141,6 +130,11 @@ root_command() { fi fi + echo jq argument ${args[--jq]} + if [[ ${args[--jq]} ]];then + cmd="${cmd} | jq ${args[--jq]}" + fi + echo $cmd confirm run this command? || return 1 eval $cmd @@ -167,14 +161,14 @@ dbackup_usage() { printf "Usage:\n" printf " dbackup [SOURCE] [TARGET] [options]\n" - printf " dbackup --help\n" + printf " dbackup --help | -h\n" printf " dbackup --version\n" echo if [[ -n $long_usage ]]; then printf "Options:\n" # :command.usage_fixed_flags - echo " --help" + echo " --help, -h" printf " Show this help\n" echo echo " --version" @@ -231,34 +225,9 @@ dbackup_usage() { printf " path to settings file (yaml). Keys are same as long\n" echo - # :flag.usage - echo " --host, -h THOST" - printf " host on remote to target to receive backup\n" - echo - - # :flag.usage - echo " --shost SHOST" - printf " remote to host of source\n" - echo - - # :flag.usage - echo " --user, -u TUSER" - printf " user on remote host\n" - echo - - # :flag.usage - echo " --suser SUSER" - printf " remote user on source host\n" - echo - - # :flag.usage - echo " --sshcfg SSHCFG" - printf " path to sshcfg file\n" - echo - # :flag.usage echo " --options, -o OPTIONS" - printf " additional options (restic or rsync)\n" + printf " additional options to pass ot underlying command (restic), \"quote\"\n" echo # :flag.usage @@ -272,8 +241,13 @@ dbackup_usage() { echo # :flag.usage - echo " --dir, -d" - printf " append source directory path to target directory\n" + echo " --sudo" + printf " run the backup as sudo\n" + echo + + # :flag.usage + echo " --jq JQ" + printf " pipe to jq command\n" echo # :command.usage_args printf "Arguments:\n" @@ -365,7 +339,7 @@ parse_requirements() { exit ;; - --help ) + --help | -h ) long_usage=yes dbackup_usage exit 1 @@ -478,66 +452,6 @@ parse_requirements() { fi ;; - # :flag.case - --host | -h ) - if [[ $2 ]]; then - args[--host]="$2" - shift - shift - else - printf "%s\n" "--host requires an argument: --host, -h THOST" - exit 1 - fi - ;; - - # :flag.case - --shost ) - if [[ $2 ]]; then - args[--shost]="$2" - shift - shift - else - printf "%s\n" "--shost requires an argument: --shost SHOST" - exit 1 - fi - ;; - - # :flag.case - --user | -u ) - if [[ $2 ]]; then - args[--user]="$2" - shift - shift - else - printf "%s\n" "--user requires an argument: --user, -u TUSER" - exit 1 - fi - ;; - - # :flag.case - --suser ) - if [[ $2 ]]; then - args[--suser]="$2" - shift - shift - else - printf "%s\n" "--suser requires an argument: --suser SUSER" - exit 1 - fi - ;; - - # :flag.case - --sshcfg ) - if [[ $2 ]]; then - args[--sshcfg]="$2" - shift - shift - else - printf "%s\n" "--sshcfg requires an argument: --sshcfg SSHCFG" - exit 1 - fi - ;; - # :flag.case --options | -o ) if [[ $2 ]]; then @@ -575,11 +489,23 @@ parse_requirements() { ;; # :flag.case - --dir | -d ) - args[--dir]=1 + --sudo ) + args[--sudo]=1 shift ;; + # :flag.case + --jq ) + if [[ $2 ]]; then + args[--jq]="$2" + shift + shift + else + printf "%s\n" "--jq requires an argument: --jq JQ" + exit 1 + fi + ;; + -* ) printf "invalid option: %s\n" "$key" diff --git a/all/dev/backup/src/bashly.yml b/all/dev/backup/src/bashly.yml index 30c0757..e1e8f0c 100644 --- a/all/dev/backup/src/bashly.yml +++ b/all/dev/backup/src/bashly.yml @@ -56,27 +56,10 @@ flags: short: -s arg: syaml help: path to settings file (yaml). Keys are same as long - - long: --host - short: -h - arg: thost - help: host on remote to target to receive backup - - long: --shost - arg: shost - help: remote to host of source - - long: --user - short: -u - arg: tuser - help: user on remote host - - long: --suser - arg: suser - help: remote user on source host - - long: --sshcfg - arg: sshcfg - help: path to sshcfg file - long: --options short: -o arg: options - help: additional options (restic or rsync) + help: additional options to pass ot underlying command (restic), "quote" - long: --include_file short: -i arg: include @@ -85,9 +68,29 @@ flags: short: -e arg: exclude help: exclude file - - long: --dir - short: -d - help: append source directory path to target directory + - long: --sudo + help: run the backup as sudo + - long: --jq + arg: jq + help: pipe to jq command + + # - long: --host + # short: -h + # arg: thost + # help: host on remote to target to receive backup + # - long: --shost + # arg: shost + # help: remote to host of source + # - long: --user + # short: -u + # arg: tuser + # help: user on remote host + # - long: --suser + # arg: suser + # help: remote user on source host + # - long: --sshcfg + # arg: sshcfg + # help: path to sshcfg file examples: - backup -p password . /target/dir diff --git a/all/dev/backup/src/root_command.sh b/all/dev/backup/src/root_command.sh index 280df2b..0f84cd8 100644 --- a/all/dev/backup/src/root_command.sh +++ b/all/dev/backup/src/root_command.sh @@ -36,9 +36,8 @@ local hostname=${args[--hostname]:-$s_hostname} hostname=${hostname:-$s_host} hostname=${hostname:-$HOSTNAME} -local backup_dir=${args[--backup_dir]:-$BACKUP_DIR} -backup_dir=${backup_dir:-$s_backup_dir} -backup_dir=${backup_dir:-"/backup"} +local options="${args[--options]:-$BACKUP_OPTIONS}" +options="${options:-$s_options}" local smount=${args[--source_mount]:-$BACKUP_SOURCE_MOUNT} smount=${smount:-$s_source_mount} @@ -78,7 +77,7 @@ if [[ $server ]]; then fi if [[ $smount ]]; then - setpath="--set-path ${source}" + setpath="--set-path ${source} --tag mount-point:${smount}" source=${smount}${source} fi @@ -87,33 +86,24 @@ exclude=${exclude:-$s_exclude} exclude=${exclude:-"$source/exclude.bac"} -local shost=$([[ ${args[--shost]} ]] && echo ${args[--shost]}::) -local suser=$([[ ${args[--suser]} ]] && echo ${args[--suser]}@) -local thost=$([[ ${args[--host]} ]] && echo ${args[--host]}::) -local tuser=$([[ ${args[--user]} ]] && echo ${args[--user]}@) +# local shost=$([[ ${args[--shost]} ]] && echo ${args[--shost]}::) +# local suser=$([[ ${args[--suser]} ]] && echo ${args[--suser]}@) +# local thost=$([[ ${args[--host]} ]] && echo ${args[--host]}::) +# local tuser=$([[ ${args[--user]} ]] && echo ${args[--user]}@) local options=$(echo ${args[--options]} | awk '{gsub(/\\/," ")}1') local bin=$(command -v restic) - -local cmd=${args[cmd]:-"backup"} - - -echo before exists exclude: $exclude - exclude=$([[ -f $exclude ]] && echo "--iexclude-file $exclude" || echo "") echo source: $source echo target $target echo exclude: $exclude -# local ssh="--remote-schema \"ssh -C %s /home/sysadmin/.local/bin/rdiff-backup --server\"" +local sudo=$([[ ${args[--sudo]} || $s_sudo || $BACKUP_SUDO ]] && echo "sudo" || echo "") -#cmd="$sudo rdiff-backup $options $exclude $ssh ${suser}${shost}$source ${tuser}${thost}$target" - -local sudo="" -local pcmd="${sudo} ${password} ${bin} -r ${target}" +local pcmd="${sudo} ${password} ${bin} ${options} -r ${target}" local cmd="${pcmd} -H ${hostname} ${setpath} backup ${source} ${exclude}" @@ -133,6 +123,11 @@ if [[ ${args[--view]} || ${args[--view-path]} ]]; then fi fi +echo jq argument ${args[--jq]} +if [[ ${args[--jq]} ]];then + cmd="${cmd} | jq ${args[--jq]}" +fi + echo $cmd confirm run this command? || return 1 eval $cmd From 21c9940d4d90eca8c0fb7cdb6ca9351c0547c426 Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Sat, 11 Sep 2021 08:49:40 -0700 Subject: [PATCH 10/13] fix: check for settings file existence or exit --- all/dev/backup/dbackup | 43 +++++++++++++++++++++++++++--- all/dev/backup/src/root_command.sh | 5 +++- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/all/dev/backup/dbackup b/all/dev/backup/dbackup index 47d6169..df88aab 100755 --- a/all/dev/backup/dbackup +++ b/all/dev/backup/dbackup @@ -17,6 +17,7 @@ root_command() { local settings=${args[--settings]} + if [[ $settings ]]; then if [[ -f $settings ]]; then echo loading settings file $settings module_load yaml @@ -24,7 +25,9 @@ root_command() { echo $s_source echo $s_target echo $s_host - + else + echo settings file $settings does not exist && return 1 + fi fi if [[ $s_server_host ]]; then @@ -308,6 +311,30 @@ dbackup_usage() { fi } +# :command.normalize_input +normalize_input() { + local arg flags + + while [[ $# -gt 0 ]]; do + arg="$1" + if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then + input+=("${BASH_REMATCH[1]}") + input+=("${BASH_REMATCH[2]}") + elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then + input+=("${BASH_REMATCH[1]}") + input+=("${BASH_REMATCH[2]}") + elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then + flags="${BASH_REMATCH[1]}" + for (( i=0 ; i < ${#flags} ; i++ )); do + input+=("-${flags:i:1}") + done + else + input+=("$arg") + fi + + shift + done +} # :command.inspect_args inspect_args() { readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort) @@ -342,7 +369,7 @@ parse_requirements() { --help | -h ) long_usage=yes dbackup_usage - exit 1 + exit ;; esac @@ -528,6 +555,7 @@ parse_requirements() { esac done + # :command.catch_all_filter # :command.default_assignments # :command.whitelist_filter } @@ -536,7 +564,12 @@ parse_requirements() { initialize() { version="0.1.0" long_usage='' - set -e + set -e + + if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then + printf "bash version 4 or higher is required\n" + exit 1 + fi # :src/initialize.sh # Code here runs inside the initialize() function @@ -552,7 +585,9 @@ initialize() { run() { declare -A args declare -a other_args - parse_requirements "$@" + declare -a input + normalize_input "$@" + parse_requirements "${input[@]}" if [[ $action == "root" ]]; then root_command diff --git a/all/dev/backup/src/root_command.sh b/all/dev/backup/src/root_command.sh index 0f84cd8..1e64a23 100644 --- a/all/dev/backup/src/root_command.sh +++ b/all/dev/backup/src/root_command.sh @@ -10,6 +10,7 @@ module_load path local settings=${args[--settings]} +if [[ $settings ]]; then if [[ -f $settings ]]; then echo loading settings file $settings module_load yaml @@ -17,7 +18,9 @@ if [[ -f $settings ]]; then echo $s_source echo $s_target echo $s_host - + else + echo settings file $settings does not exist && return 1 +fi fi if [[ $s_server_host ]]; then From ade5236b019d410465c9002acd6960b6f6fd119c Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Thu, 16 Sep 2021 14:24:33 -0700 Subject: [PATCH 11/13] feat: add second url to open argument to script --- all/modules/chromium/chromium.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/all/modules/chromium/chromium.sh b/all/modules/chromium/chromium.sh index 6f56778..ea25a70 100755 --- a/all/modules/chromium/chromium.sh +++ b/all/modules/chromium/chromium.sh @@ -9,6 +9,7 @@ local exe="/usr/bin/chromium" [[ ! $exe ]] && echo chromium not installed set && return 1 user=${CHROMIUM_USER} [[ $1 && ( ! $1 == -* ) ]] && user=$1 && shift +[[ $1 && ( ! $1 == -* ) ]] && url=$1 && shift user=${user:-$USER} if [[ $user == "$USER" ]]; then unset CHROME_CONFIG_HOME @@ -19,7 +20,7 @@ if [[ $user == "$USER" ]]; then [[ $user == "incognito" ]] && set -- "$@" "-incognito" dir=${CHROMIUM_HOME:-$DEFAULT} echo "$exe $@ --user-data-dir=$dir/$user" - "$exe" "$@" --user-data-dir="$dir"/"$user" + "$exe" "$@" --user-data-dir="$dir"/"$user" $url fi } From 4fb2e4698c23a82664c3427ef103813dffda5847 Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Tue, 19 Oct 2021 11:01:32 -0700 Subject: [PATCH 12/13] vscode script supports vscodium and uses USER as subdirectory gnome terminal alias to use from vscdoe --- all/alias/term | 1 + all/modules/vscode.sh | 32 ++++++++++++++++++-------------- 2 files changed, 19 insertions(+), 14 deletions(-) create mode 100755 all/alias/term diff --git a/all/alias/term b/all/alias/term new file mode 100755 index 0000000..0c642ef --- /dev/null +++ b/all/alias/term @@ -0,0 +1 @@ +alias gterm='GNOME_TERMINAL_SCREEN='\'''\'' gnome-terminal' \ No newline at end of file diff --git a/all/modules/vscode.sh b/all/modules/vscode.sh index c5095b9..53597f5 100755 --- a/all/modules/vscode.sh +++ b/all/modules/vscode.sh @@ -1,20 +1,24 @@ #!/bin/bash -command -v code >/dev/null 2>&1 || exit - +VSCODE_BIN=$(command -v code) +VSCODE_BIN=${VSCODE_BIN:-$(command -v codium)} +[[ $VSCODE_BIN ]] || { + echo "no vscode binary on machine" + exit +} # set default home here -# can pass -vscode () { -#local home=$HOME -local home=/opt/vscode -home=${VSCODE_HOME:-$home} -[[ $1 == "-h" ]] && home=$2 && shift 2 -mkdir -p $home -[[ $? -ne 0 ]] && echo "unable to set vscode home at $home, aborting" && return 1 -exts=${home}/extensions -user=${home}/user -exe=$(command -v code) -$exe --user-data-dir=$user --extensions-dir=$exts "$@" +# can pass +vscode() { + #local home=$HOME + local home=/opt/vscode + home=${VSCODE_HOME:-$home} + [[ $1 == "-h" ]] && home=$2 && shift 2 + mkdir -p ${home}/${USER} + [[ $? -ne 0 ]] && echo "unable to set vscode home at $home/$USER, aborting" && return 1 + exts=${home}/${USER}/extensions + user=${home}/${USER}/data + echo $VSCODE_BIN --user-data-dir=$user --extensions-dir=$exts "$@" + $VSCODE_BIN --user-data-dir=$user --extensions-dir=$exts "$@" } # # if script was executed then call the function From 49920c442afacd3ab90b86c4a656270b7b40a3b7 Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Sat, 23 Oct 2021 23:40:37 -0700 Subject: [PATCH 13/13] feat: supports chromium binary via faltpak feat: will propery handle instance, url and additional options --- all/modules/chromium/chromium.sh | 72 +++++++++++++++++++------------- 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/all/modules/chromium/chromium.sh b/all/modules/chromium/chromium.sh index ea25a70..0cdd614 100755 --- a/all/modules/chromium/chromium.sh +++ b/all/modules/chromium/chromium.sh @@ -1,36 +1,52 @@ #!/bin/bash -# user to start MUST be first in arguments +# user to start MUST be first in arguments # if not supplied then will use default -chromium_ () { -local DEFAULT=/opt/chromium -# local DEFAULT="$HOME/.local/share/chromium" -local exe="/usr/bin/chromium" -[[ ! $exe ]] && echo chromium not installed set && return 1 -user=${CHROMIUM_USER} -[[ $1 && ( ! $1 == -* ) ]] && user=$1 && shift -[[ $1 && ( ! $1 == -* ) ]] && url=$1 && shift -user=${user:-$USER} -if [[ $user == "$USER" ]]; then - unset CHROME_CONFIG_HOME - echo starting chromium for $USER in default directory within users home - echo $exe "$@" - $exe "$@" - else - [[ $user == "incognito" ]] && set -- "$@" "-incognito" - dir=${CHROMIUM_HOME:-$DEFAULT} - echo "$exe $@ --user-data-dir=$dir/$user" - "$exe" "$@" --user-data-dir="$dir"/"$user" $url +chromium_() { + local DEFAULT=/opt/chromium + # local DEFAULT="$HOME/.local/share/chromium" + local exe="/usr/bin/chromium" + if [[ ! -f $exe ]]; then + echo deb chromium not installed, checking for flatpak version + flatpak=$(flatpak info com.github.Eloston.UngoogledChromium | grep error:) + if [[ $flatpak ]]; then + echo no flatpak version either - exiting && return 1 + else + flatpak=true + exe="/usr/bin/flatpak run --branch=stable --arch=x86_64 --filesystem= --command=/app/bin/chromium --file-forwarding com.github.Eloston.UngoogledChromium @@u" + fi fi + + local instance=${CHROMIUM_INSTANCE} + [[ $1 && (! $1 == -*) ]] && instance=$1 && shift + [[ $1 && (! $1 == -*) ]] && url=$1 && shift + if [[ $instance =~ http[s]?:\/\/ ]]; then + url=$instance + instance="" + if [[ $url =~ ^-+ ]]; then + url="" + set -- "$@" $url + url="" + fi + fi + + local dir + if [[ ! $instance ]]; then + unset CHROME_CONFIG_HOME + $HOME/.config/chromium + echo starting chromium for $USER in + dir=$HOME/.config/chromium + exe="${exe//$dir}" + # echo $exe "$@" $([[ $flatpak ]] && echo " @@") + # $exe "$@" + else + [[ $instance == "incognito" ]] && set -- "$@" "-incognito" + dir=${CHROMIUM_HOME:-$DEFAULT}/$instance + exe="${exe//$dir}" + fi + $exe $@ --user-data-dir=$dir $url $([[ $flatpak ]] && echo "@@") + } # # if script was executed then call the function (return 0 2>/dev/null) || chromium_ $@ - - - - - - - -