#!/bin/bash echo "running root command" inspect_args # module_load ssh module_load confirm module_load path local settings=${args[--settings]} if [[ $settings ]]; then if [[ -f $settings ]]; then echo loading settings file $settings module_load yaml eval $(parse_yaml $settings "s_") 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 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 password="RESTIC_PASSWORD=${password}" local server=${args[--server]:-$BACKUP_SERVER} server=${server:-$s_server} local hostname=${args[--hostname]:-$s_hostname} hostname=${hostname:-$s_host} hostname=${hostname:-$HOSTNAME} local options="${args[--options]:-$BACKUP_OPTIONS}" options="${options:-$s_options}" 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=${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="/${hostname}${target}" fi if [[ $server ]]; then target="rest:${server}${target}" fi if [[ $smount ]]; then setpath="--set-path ${source} --tag mount-point:${smount}" source=${smount}${source} 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) exclude=$([[ -f $exclude ]] && echo "--iexclude-file $exclude" || echo "") echo source: $source echo target $target echo exclude: $exclude local sudo=$([[ ${args[--sudo]} || $s_sudo || $BACKUP_SUDO ]] && echo "sudo" || echo "") local pcmd="${sudo} ${password} ${bin} ${options} -r ${target}" 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 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="${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 return 3 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 # sudo chown -R $USER:$USER $target # sudo chown -R $USER:$USER /home/$USER/.cache/restic # sudo chmod -R g+rwX /home/$USER/.cache/restic