365 lines
7.9 KiB
Bash
Executable File
365 lines
7.9 KiB
Bash
Executable File
#!/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
|
|
echo Running Backup Command
|
|
|
|
inspect_args
|
|
|
|
# module_load ssh
|
|
module_load confirm
|
|
|
|
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
|
|
[[ -f $source/exclude.bac ]] && exclude="$source/exclude.bac"
|
|
# ls $source
|
|
# [[ $EXCLUDE_BACKUP ]] && exclude="$BACKUP_EXCLUDE"
|
|
fi
|
|
echo making exclude
|
|
[[ $exclude ]] && exclude="--exclude-globbing-filelist $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"
|
|
echo $cmd
|
|
confirm run this command? || return 1
|
|
eval $cmd
|
|
}
|
|
|
|
# :command.version_command
|
|
version_command() {
|
|
echo "$version"
|
|
}
|
|
|
|
# :command.usage
|
|
backup_usage() {
|
|
if [[ -n $long_usage ]]; then
|
|
printf "backup - backup/mirror a directory using rdiff or rsync\n"
|
|
echo
|
|
else
|
|
printf "backup - backup/mirror a directory using rdiff or rsync\n"
|
|
echo
|
|
fi
|
|
|
|
printf "Usage:\n"
|
|
printf " backup [SOURCE] [TARGET] [options]\n"
|
|
printf " backup --help\n"
|
|
printf " backup --version | -v\n"
|
|
echo
|
|
|
|
if [[ -n $long_usage ]]; then
|
|
printf "Options:\n"
|
|
# :command.usage_fixed_flags
|
|
echo " --help"
|
|
printf " Show this help\n"
|
|
echo
|
|
echo " --version, -v"
|
|
printf " Show version number\n"
|
|
echo
|
|
# :command.usage_flags
|
|
# :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 " rdiff options\n"
|
|
echo
|
|
|
|
# :flag.usage
|
|
echo " --include_file, -i OPTIONS"
|
|
printf " rdiff options\n"
|
|
echo
|
|
|
|
# :flag.usage
|
|
echo " --exclude_file, -e OPTIONS"
|
|
printf " rdiff options\n"
|
|
echo
|
|
|
|
# :flag.usage
|
|
echo " --mirror, -m"
|
|
printf " make a mirror copy instead of a differential snapshot\n"
|
|
echo
|
|
|
|
# :flag.usage
|
|
echo " --sudo, -s"
|
|
printf " run the backup as sudo\n"
|
|
echo
|
|
|
|
# :flag.usage
|
|
echo " --no-dir, -n"
|
|
printf " don't make a directory of same name within the target, merge sub directories\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
|
|
# :command.usage_examples
|
|
printf "Examples:\n"
|
|
|
|
printf " backup -m -n -h thost --shost shost -u tuser --suser suser . /target/dir\n"
|
|
echo
|
|
|
|
fi
|
|
}
|
|
|
|
# :command.inspect_args
|
|
inspect_args() {
|
|
echo args:
|
|
for k in "${!args[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
|
|
}
|
|
|
|
# :command.command_functions
|
|
|
|
# :command.parse_requirements
|
|
parse_requirements() {
|
|
# :command.fixed_flag_filter
|
|
case "$1" in
|
|
--version | -v )
|
|
version_command
|
|
exit
|
|
;;
|
|
|
|
--help )
|
|
long_usage=yes
|
|
backup_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
|
|
--host | -h )
|
|
if [[ $2 && $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 && $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 && $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 && $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 && $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 && $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 && $2 != -* ]]; then
|
|
args[--include_file]="$2"
|
|
shift
|
|
shift
|
|
else
|
|
printf "%s\n" "--include_file requires an argument: --include_file, -i OPTIONS"
|
|
exit 1
|
|
fi
|
|
;;
|
|
|
|
# :flag.case
|
|
--exclude_file | -e )
|
|
if [[ $2 && $2 != -* ]]; then
|
|
args[--exclude_file]="$2"
|
|
shift
|
|
shift
|
|
else
|
|
printf "%s\n" "--exclude_file requires an argument: --exclude_file, -e OPTIONS"
|
|
exit 1
|
|
fi
|
|
;;
|
|
|
|
# :flag.case
|
|
--mirror | -m )
|
|
args[--mirror]=1
|
|
shift
|
|
;;
|
|
|
|
# :flag.case
|
|
--sudo | -s )
|
|
args[--sudo]=1
|
|
shift
|
|
;;
|
|
|
|
# :flag.case
|
|
--no-dir | -n )
|
|
args[--no-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.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.
|
|
}
|
|
|
|
# :command.run
|
|
run() {
|
|
declare -A args
|
|
parse_requirements "$@"
|
|
|
|
if [[ ${args[--version]} ]]; then
|
|
version_command
|
|
elif [[ ${args[--help]} ]]; then
|
|
long_usage=yes
|
|
backup_usage
|
|
elif [[ $action == "root" ]]; then
|
|
root_command
|
|
fi
|
|
}
|
|
|
|
initialize
|
|
run "$@"
|