Compare commits
12 Commits
02a27f55a1
...
91359306aa
Author | SHA1 | Date |
---|---|---|
Asif Bacchus | 91359306aa | |
Asif Bacchus | 7bd6f369e7 | |
Asif Bacchus | 39f519a67e | |
Asif Bacchus | f80a66439c | |
Asif Bacchus | 07fdd04754 | |
Asif Bacchus | dc75c0afea | |
Asif Bacchus | 8eb2c07785 | |
Asif Bacchus | 7f2f054ebc | |
Asif Bacchus | 5254dba0bf | |
Asif Bacchus | 515e57e4f3 | |
Asif Bacchus | 099be9ff89 | |
Asif Bacchus | ba58997b9f |
|
@ -1,3 +1,19 @@
|
||||||
{
|
{
|
||||||
"bookmarks": []
|
"bookmarks": [
|
||||||
|
{
|
||||||
|
"fsPath": "$ROOTPATH$/borghelper.sh",
|
||||||
|
"bookmarks": [
|
||||||
|
-1,
|
||||||
|
253,
|
||||||
|
151,
|
||||||
|
-1,
|
||||||
|
-1,
|
||||||
|
-1,
|
||||||
|
-1,
|
||||||
|
-1,
|
||||||
|
-1,
|
||||||
|
84
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
106
borghelper.sh
106
borghelper.sh
|
@ -8,6 +8,15 @@
|
||||||
trap trapExit 1 2 3 6
|
trap trapExit 1 2 3 6
|
||||||
|
|
||||||
### functions
|
### 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() {
|
consoleError() {
|
||||||
printf "%s\n%s\n" "$err" "$2"
|
printf "%s\n%s\n" "$err" "$2"
|
||||||
printf "Exiting.\n\n%s" "$norm"
|
printf "Exiting.\n\n%s" "$norm"
|
||||||
|
@ -19,23 +28,30 @@ textblock() {
|
||||||
}
|
}
|
||||||
|
|
||||||
trapExit () {
|
trapExit () {
|
||||||
|
cleanup
|
||||||
printf "%s\nScript execution terminated via signal.\n\n%s" "$err" "$norm"
|
printf "%s\nScript execution terminated via signal.\n\n%s" "$err" "$norm"
|
||||||
exit 99
|
exit 99
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
### text formatting presets
|
### text formatting presets
|
||||||
err=$(tput bold)$(tput setaf 1)
|
if command -v tput > /dev/null; then
|
||||||
norm=$(tput sgr0)
|
err=$(tput bold)$(tput setaf 1)
|
||||||
width=$(tput cols)
|
norm=$(tput sgr0)
|
||||||
|
width=$(tput cols)
|
||||||
|
else
|
||||||
|
err="[ERROR] "
|
||||||
|
norm=""
|
||||||
|
width=80
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
### pre-requisites
|
### pre-requisites
|
||||||
|
|
||||||
# is user root?
|
# is user root?
|
||||||
if [ ! "$( id -u )" -eq 0 ]; then
|
#if [ ! "$( id -u )" -eq 0 ]; then
|
||||||
consoleError 1 'You must be root to run this script.'
|
# consoleError 1 'You must be root to run this script.'
|
||||||
fi
|
#fi
|
||||||
|
|
||||||
# has a parameter been passed to this script?
|
# has a parameter been passed to this script?
|
||||||
if [ -z "$1" ]; then
|
if [ -z "$1" ]; then
|
||||||
|
@ -66,6 +82,10 @@ while [ $# -gt 0 ]; do
|
||||||
printf "\nStill working on the help text :-)\n\n"
|
printf "\nStill working on the help text :-)\n\n"
|
||||||
exit 0
|
exit 0
|
||||||
;;
|
;;
|
||||||
|
-i|--info)
|
||||||
|
# show archive information
|
||||||
|
operation='info'
|
||||||
|
;;
|
||||||
-l|--list)
|
-l|--list)
|
||||||
# list contents of specific backup
|
# list contents of specific backup
|
||||||
operation='viewarchive'
|
operation='viewarchive'
|
||||||
|
@ -83,6 +103,10 @@ while [ $# -gt 0 ]; do
|
||||||
restorePath="${2%/}"
|
restorePath="${2%/}"
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
|
--progress)
|
||||||
|
# show progress
|
||||||
|
commonOptions="$commonOptions --progress"
|
||||||
|
;;
|
||||||
-r|--restore)
|
-r|--restore)
|
||||||
# restore archive/file
|
# restore archive/file
|
||||||
operation='restore'
|
operation='restore'
|
||||||
|
@ -98,6 +122,10 @@ while [ $# -gt 0 ]; do
|
||||||
varsFile="$2"
|
varsFile="$2"
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
|
--verbose)
|
||||||
|
# display each file being processed
|
||||||
|
restoreOptions="$restoreOptions --list"
|
||||||
|
;;
|
||||||
*)
|
*)
|
||||||
# invalid option
|
# invalid option
|
||||||
printf "%s\nUnknown option: %s\n" "$err" "$1"
|
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.'
|
consoleError 1 'You must provide a valid .borgvars file with information about your borg repo.'
|
||||||
fi
|
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
|
# 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."
|
consoleError 1 "List operation requested but no archive name provided. Please use '--list-all' for a list of all available archives."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -139,6 +172,10 @@ if [ "$fileName" ] && [ -z "$archiveName" ]; then
|
||||||
consoleError 1 "Filename specified without an associated archive name."
|
consoleError 1 "Filename specified without an associated archive name."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# clean-up leading spaces in option strings
|
||||||
|
commonOptions=${commonOptions##[[:space:]]}
|
||||||
|
restoreOptions=${restoreOptions##[[:space:]]}
|
||||||
|
|
||||||
|
|
||||||
### read borg information file
|
### read borg information file
|
||||||
|
|
||||||
|
@ -179,13 +216,64 @@ export BORG_REPO="${borgRepo}"
|
||||||
# check borg repo password
|
# check borg repo password
|
||||||
if [ -n "${borgRepoPassword}" ]; then
|
if [ -n "${borgRepoPassword}" ]; then
|
||||||
export BORG_PASSPHRASE="${borgRepoPassword}"
|
export BORG_PASSPHRASE="${borgRepoPassword}"
|
||||||
elif [ "${borgRepoPassword}" = '<none>' ]; then
|
elif [ "${borgRepoPassword}" = 'none' ]; then
|
||||||
export BORG_PASSPHRASE=""
|
export BORG_PASSPHRASE=""
|
||||||
else
|
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
|
fi
|
||||||
|
|
||||||
# export borg remote path, if specified
|
# export borg remote path, if specified
|
||||||
if [ -n "${borgRemote}" ]; then export BORG_REMOTE_PATH="${borgRemote}"; fi
|
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
|
Loading…
Reference in New Issue