style: cleanup formatting, spelling

- use project-custom dictionary
- cleanup spelling mistakes
- rename functions to conform with camelCase for spellcheck
- reformat file: spacing, indentation
This commit is contained in:
Asif Bacchus 2021-05-15 09:15:57 -06:00
parent 8b201ca047
commit c73a3b8531
5 changed files with 182 additions and 147 deletions

View File

@ -0,0 +1,12 @@
<component name="ProjectDictionaryState">
<dictionary name="asif">
<words>
<w>borgbackup</w>
<w>borghelper</w>
<w>borgvars</w>
<w>listall</w>
<w>makevars</w>
<w>viewarchive</w>
</words>
</dictionary>
</component>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ContentModelUserStore">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="RIDER_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$/../.." />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -7,4 +7,7 @@
<inspection_tool class="SubjectLimit" enabled="true" level="ERROR" enabled_by_default="true" />
</profile>
</component>
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -17,6 +17,7 @@ trap trapExit 1 2 3 6
### functions
cleanup() {
# remove borg temp directory, if it exists
# shellcheck disable=SC2154
if [ -d "${borgBaseDir}/tmp" ]; then
if ! rm -rf "${borgBaseDir}/tmp" >/dev/null 2>&1; then
consoleError 3 "Script completed successfully but could not remove temporary directory at '$borgBaseDir/tmp'. Sorry to be messy."
@ -37,61 +38,61 @@ consoleError() {
scriptHelp() {
printf "\n"
textblock "${bold}Usage: borghelper.sh [parameters]${norm}"
textBlock "${bold}Usage: borghelper.sh [parameters]${norm}"
printf "\n"
textblock "Simple script to read connection parameters from a flat text file and process borg 'info', 'list' and 'restore' commands without the very long command lines that are required when specifying repo names and passwords, etc."
textBlock "Simple script to read connection parameters from a flat text file and process borg 'info', 'list' and 'restore' commands without the very long command lines that are required when specifying repo names and passwords, etc."
printf "\n"
textblock "${magenta}The script has the following parameters:${norm}"
textBlock "${magenta}The script has the following parameters:${norm}"
printf "\n"
textblock "${magenta}--- Required Parameters ---${norm}"
textBlock "${magenta}--- Required Parameters ---${norm}"
printf "\n"
ptextblock "-v|--vars"
textblock "Path to the .borgvars file from which to read borg connection information. This is not required if run with '--makevars'."
textBlockParams "-v|--vars"
textBlock "Path to the .borgvars file from which to read borg connection information. This is not required if run with '--makevars'."
printf "\n"
textblock "${magenta}--- Operation Modes ---${norm}"
textBlock "${magenta}--- Operation Modes ---${norm}"
printf "\n"
ptextblock "--makevars"
textblock "Create a sample .borgvars file that you can fill in and use with this script."
ptextblock "-i|--info"
textblock "Get information about a specified borg repo archive. Requires you supply '--archive'."
ptextblock "-l|--list"
textblock "List contents of a specified borg repo archive. Requires you supply '--archive'. You can optionally specify a file/pattern to search for using '--file'."
ptextblock "-la|--list-all"
textblock "List all available archives within the repo specified in your .borgvars file."
ptextblock "-r|--restore"
textblock "Restore the specified borg repo archive/file(s). Requires you supply '--archive'."
textBlockParams "--makevars"
textBlock "Create a sample .borgvars file that you can fill in and use with this script."
textBlockParams "-i|--info"
textBlock "Get information about a specified borg repo archive. Requires you supply '--archive'."
textBlockParams "-l|--list"
textBlock "List contents of a specified borg repo archive. Requires you supply '--archive'. You can optionally specify a file/pattern to search for using '--file'."
textBlockParams "-la|--list-all"
textBlock "List all available archives within the repo specified in your .borgvars file."
textBlockParams "-r|--restore"
textBlock "Restore the specified borg repo archive/file(s). Requires you supply '--archive'."
printf "\n"
textblock "${magenta}--- Selector Parameters ---${norm}"
textBlock "${magenta}--- Selector Parameters ---${norm}"
printf "\n"
ptextblock "-a|--archive"
textblock "The archive within your borg repo you wish to work with."
ptextblock "--exclude"
textblock "Pattern (python/borg) of files to exclude from a restore operation."
ptextblock "-f|--file"
textblock "Specific file/pattern (python/borg) within an archive for which you want to restore or search. Requires that you supply '--archive'."
ptextblock "-p|--path"
textblock "Path to which you want your archive/files restored. This script will attempt to create the directory for you if it does not already exist."
textBlockParams "-a|--archive"
textBlock "The archive within your borg repo you wish to work with."
textBlockParams "--exclude"
textBlock "Pattern (python/borg) of files to exclude from a restore operation."
textBlockParams "-f|--file"
textBlock "Specific file/pattern (python/borg) within an archive for which you want to restore or search. Requires that you supply '--archive'."
textBlockParams "-p|--path"
textBlock "Path to which you want your archive/files restored. This script will attempt to create the directory for you if it does not already exist."
printf "\n"
textblock "${magenta}--- Restore Options ---${norm}"
textBlock "${magenta}--- Restore Options ---${norm}"
printf "\n"
ptextblock "--progress"
textblock "Display progress indicator during restore operations. WARNING: This can drastically slow down operations on larger archives!"
ptextblock "--verbose"
textblock "List the individual files being processed during restore operations."
textBlockParams "--progress"
textBlock "Display progress indicator during restore operations. WARNING: This can drastically slow down operations on larger archives!"
textBlockParams "--verbose"
textBlock "List the individual files being processed during restore operations."
printf "\n"
textblock "${magenta}--- Other Parameters ---${norm}"
textBlock "${magenta}--- Other Parameters ---${norm}"
printf "\n"
ptextblock "-h|-?|--help"
textblock "This help screen."
textBlockParams "-h|-?|--help"
textBlock "This help screen."
printf "\n"
exit 0
}
textblock() {
textBlock() {
printf "%s\n" "$1" | fold -w "$width" -s
}
ptextblock() {
textBlockParams() {
printf "%s%s%s\n" "$cyan" "$1" "$norm"
}
@ -101,7 +102,6 @@ trapExit () {
exit 99
}
### text formatting presets
if command -v tput >/dev/null; then
bold=$(tput bold)
@ -119,7 +119,6 @@ else
width=80
fi
### pre-requisites
# is user root?
@ -221,7 +220,6 @@ while [ $# -gt 0 ]; do
shift
done
### process 'makevars' operation
if [ "$operation" = 'makevars' ]; then
if ! printf "sshKeyFile=\nborgBaseDir=\nborgRepo=\nborgRepoPassword=\nborgRepoKey=\nborgRemote=\n" >./sample.borgvars; then
@ -275,8 +273,8 @@ if [ "$operation" = 'restore' ]; then
if [ -d "$restorePath" ]; then
# convert to absolute path
restorePath=$(
cd "$restorePath" || \
consoleError 5 'Cannot access specified restore directory.'; \
cd "$restorePath" ||
consoleError 5 'Cannot access specified restore directory.'
pwd -P
)
if ! touch "${restorePath}/touch.test" >/dev/null 2>&1; then
@ -288,8 +286,8 @@ if [ "$operation" = 'restore' ]; then
else
# convert to absolute path
restorePath=$(
cd "$restorePath" || \
consoleError 5 'Cannot access specified restore directory.'; \
cd "$restorePath" ||
consoleError 5 'Cannot access specified restore directory.'
pwd -P
)
if ! touch "${restorePath}/touch.test" >/dev/null 2>&1; then
@ -305,10 +303,12 @@ fi
case "${varsFile}" in
/*)
# absolute path, no need to rewrite variable
# shellcheck disable=SC1090
. "${varsFile}"
;;
*)
# relative path, prepend './' to create absolute path
# shellcheck disable=SC1090
. "./${varsFile}"
;;
esac
@ -322,6 +322,7 @@ fi
export BORG_BASE_DIR="${borgBaseDir%/}"
## check path to SSH keyfile
# shellcheck disable=SC2154
if [ -z "${sshKeyFile}" ]; then
consoleError 2 "$varsFile: 'sshKeyFile' is not specified."
elif [ ! -f "${sshKeyFile}" ]; then
@ -330,12 +331,14 @@ fi
export BORG_RSH="ssh -i ${sshKeyFile}"
# check borg repo connect string
# shellcheck disable=SC2154
if [ -z "${borgRepo}" ]; then
consoleError 2 "$varsFile: 'borgRepo' is not specified."
fi
export BORG_REPO="${borgRepo}"
# check borg repo password
# shellcheck disable=SC2154
if [ -n "${borgRepoPassword}" ]; then
export BORG_PASSPHRASE="${borgRepoPassword}"
elif [ "${borgRepoPassword}" = 'none' ]; then
@ -345,14 +348,15 @@ else
fi
# check borg keyfile if supplied
# shellcheck disable=SC2154
if [ -n "${borgRepoKey}" ]; then
export BORG_KEY_FILE="${borgRepoKey}"
fi
# export borg remote path, if specified
# shellcheck disable=SC2154
if [ -n "${borgRemote}" ]; then export BORG_REMOTE_PATH="${borgRemote}"; fi
### create borg temp dir:
## python requires a writable temporary directory when unpacking borg and
## executing commands. This defaults to /tmp but many systems mount /tmp with
@ -367,7 +371,6 @@ if [ ! -d "${borgBaseDir}/tmp" ]; then
fi
export TMPDIR="${borgBaseDir}/tmp"
### execute borg operations
# info operations
@ -388,29 +391,31 @@ elif [ "$operation" = 'restore' ]; then
if [ -z "$fileName" ]; then
# restore entire archive
if [ "$exclusions" ]; then
# shellcheck disable=SC2086
borg --show-rc ${commonOptions} extract ${restoreOptions} ::"${archiveName}" --exclude "${exclusions}"
else
# shellcheck disable=SC2086
borg --show-rc ${commonOptions} extract ${restoreOptions} ::"${archiveName}"
fi
elif [ "$fileName" ]; then
# restore file/pattern
if [ "$exclusions" ]; then
# shellcheck disable=SC2086
borg --show-rc ${commonOptions} extract ${restoreOptions} ::"${archiveName}" "${fileName}" --exclude "${exclusions}"
else
# shellcheck disable=SC2086
borg --show-rc ${commonOptions} extract ${restoreOptions} ::"${archiveName}" "${fileName}"
fi
fi
fi
### exit gracefully
cleanup
exit 0
### exit codes
# 0: no errors, script completed successfully
# 1: parameter error (missing, non-existant or invalid input)
# 1: parameter error (missing, non-existent or invalid input)
# 2: parameter missing/invalid in .borgvars file
# 3: could not create/remove borg tmp directory
# 4: could not write sample borgvars file (permissions?)