Compare commits

...

12 Commits

Author SHA1 Message Date
Asif Bacchus 91359306aa make operations mutually exclusive 2020-03-26 04:44:29 -06:00
Asif Bacchus 7bd6f369e7 add info operations 2020-03-26 04:43:27 -06:00
Asif Bacchus 39f519a67e add progress and file-list options as switches 2020-03-26 04:40:36 -06:00
Asif Bacchus f80a66439c add restore operation 2020-03-26 04:33:16 -06:00
Asif Bacchus 07fdd04754 perform list operations 2020-03-26 04:07:37 -06:00
Asif Bacchus dc75c0afea exit gracefully, list exit codes 2020-03-26 04:02:05 -06:00
Asif Bacchus 8eb2c07785 change error code for failing to remove tmp dir 2020-03-26 04:01:54 -06:00
Asif Bacchus 7f2f054ebc catch if tmp dir cannot be removed 2020-03-26 04:01:15 -06:00
Asif Bacchus 5254dba0bf create tmp dir and clean up after 2020-03-26 03:56:23 -06:00
Asif Bacchus 515e57e4f3 change no borg repo passwd identifier 2020-03-26 03:46:48 -06:00
Asif Bacchus 099be9ff89 wrong operation name in preflight check 2020-03-26 03:46:31 -06:00
Asif Bacchus ba58997b9f don't use tput if it's not installed 2020-03-26 03:25:54 -06:00
2 changed files with 114 additions and 10 deletions

View File

@ -1,3 +1,19 @@
{
"bookmarks": []
"bookmarks": [
{
"fsPath": "$ROOTPATH$/borghelper.sh",
"bookmarks": [
-1,
253,
151,
-1,
-1,
-1,
-1,
-1,
-1,
84
]
}
]
}

View File

@ -8,6 +8,15 @@
trap trapExit 1 2 3 6
### functions
cleanup () {
# remove borg temp directory, if it exists
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."
fi
fi
}
consoleError() {
printf "%s\n%s\n" "$err" "$2"
printf "Exiting.\n\n%s" "$norm"
@ -19,23 +28,30 @@ textblock() {
}
trapExit () {
cleanup
printf "%s\nScript execution terminated via signal.\n\n%s" "$err" "$norm"
exit 99
}
### text formatting presets
err=$(tput bold)$(tput setaf 1)
norm=$(tput sgr0)
width=$(tput cols)
if command -v tput > /dev/null; then
err=$(tput bold)$(tput setaf 1)
norm=$(tput sgr0)
width=$(tput cols)
else
err="[ERROR] "
norm=""
width=80
fi
### pre-requisites
# is user root?
if [ ! "$( id -u )" -eq 0 ]; then
consoleError 1 'You must be root to run this script.'
fi
#if [ ! "$( id -u )" -eq 0 ]; then
# consoleError 1 'You must be root to run this script.'
#fi
# has a parameter been passed to this script?
if [ -z "$1" ]; then
@ -66,6 +82,10 @@ while [ $# -gt 0 ]; do
printf "\nStill working on the help text :-)\n\n"
exit 0
;;
-i|--info)
# show archive information
operation='info'
;;
-l|--list)
# list contents of specific backup
operation='viewarchive'
@ -83,6 +103,10 @@ while [ $# -gt 0 ]; do
restorePath="${2%/}"
shift
;;
--progress)
# show progress
commonOptions="$commonOptions --progress"
;;
-r|--restore)
# restore archive/file
operation='restore'
@ -98,6 +122,10 @@ while [ $# -gt 0 ]; do
varsFile="$2"
shift
;;
--verbose)
# display each file being processed
restoreOptions="$restoreOptions --list"
;;
*)
# invalid option
printf "%s\nUnknown option: %s\n" "$err" "$1"
@ -121,8 +149,13 @@ if [ -z "$varsFile" ]; then
consoleError 1 'You must provide a valid .borgvars file with information about your borg repo.'
fi
# info without archive
if [ "$operation" = 'info' ] && [ -z "$archiveName" ]; then
consoleError 1 "Info operation requested but no archive name provided. Please use '--list-all' for a list of all available archives."
fi
# list without archive
if [ "$operation" = 'list' ] && [ -z "$archiveName" ]; then
if [ "$operation" = 'viewarchive' ] && [ -z "$archiveName" ]; then
consoleError 1 "List operation requested but no archive name provided. Please use '--list-all' for a list of all available archives."
fi
@ -139,6 +172,10 @@ if [ "$fileName" ] && [ -z "$archiveName" ]; then
consoleError 1 "Filename specified without an associated archive name."
fi
# clean-up leading spaces in option strings
commonOptions=${commonOptions##[[:space:]]}
restoreOptions=${restoreOptions##[[:space:]]}
### read borg information file
@ -179,13 +216,64 @@ export BORG_REPO="${borgRepo}"
# check borg repo password
if [ -n "${borgRepoPassword}" ]; then
export BORG_PASSPHRASE="${borgRepoPassword}"
elif [ "${borgRepoPassword}" = '<none>' ]; then
elif [ "${borgRepoPassword}" = 'none' ]; then
export BORG_PASSPHRASE=""
else
consoleError 2 "$varsFile: 'borgRepoPassword' must be specified or must be '<none>' if no password has been set (VERY INSECURE!)."
consoleError 2 "$varsFile: 'borgRepoPassword' must be specified or must be 'none' if no password has been set (VERY INSECURE!)."
fi
# export borg remote path, if specified
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
## the 'noexec' option for security. Thus, we will use/create a 'tmp' folder
## within the BORG_BASE_DIR and instruct python to use that instead of /tmp
# check if BORG_BASE_DIR/tmp exists, if not, create it
if [ ! -d "${borgBaseDir}/tmp" ]; then
if ! mkdir "${borgBaseDir}/tmp"; then
consoleError 3 'Unable to create temp working directory for borg.'
fi
fi
export TMPDIR="${borgBaseDir}/tmp"
### execute borg operations
# info operations
if [ "$operation" = 'info' ]; then
borg info ::"${archiveName}"
# list operations
elif [ "$operation" = 'listall' ]; then
borg list
elif [ "$operation" = 'viewarchive' ]; then
borg list ::"${archiveName}"
# restore operations
elif [ "$operation" = 'restore' ]; then
if [ -z "$fileName" ]; then
# restore entire archive
cd "$restorePath" || consoleError 4 'Could not change to restore directory.'
borg --show-rc "$commonOptions" extract "$restoreOptions" ::"${archiveName}"
elif [ "$fileName" ]; then
# restore single file
cd "$restorePath" || consoleError 4 'Could not change to restore directory.'
borg --show-rc "$commonOptions" extract "$restoreOptions" ::"${archiveName}" "${fileName}"
fi
fi
### exit gracefully
cleanup
exit 0
### exit codes
# 0: no errors, script completed successfully
# 1: parameter error (missing, non-existant or invalid input)
# 2: parameter missing/invalid in .borgvars file
# 3: could not create/remove borg tmp directory
#EOF