2020-03-26 02:21:59 -06:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
#
|
|
|
|
## borg helper script for viewing and restoring backups
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
### functions
|
|
|
|
consoleError() {
|
|
|
|
printf "%s\n%s\n" "$err" "$2"
|
|
|
|
printf "Exiting.\n\n%s" "$norm"
|
|
|
|
exit "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
textblock() {
|
|
|
|
printf "%s\n" "$1" | fold -w "$width" -s
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
### text formatting presets
|
|
|
|
err=$(tput bold)$(tput setaf 1)
|
2020-03-26 02:34:58 -06:00
|
|
|
norm=$(tput sgr0)
|
2020-03-26 02:21:59 -06:00
|
|
|
width=$(tput cols)
|
|
|
|
|
|
|
|
|
|
|
|
### pre-requisites
|
|
|
|
|
|
|
|
# is user root?
|
|
|
|
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
|
|
|
|
consoleError 1 "No operation requested. Please run this script with '--help' for valid parameters."
|
|
|
|
fi
|
|
|
|
|
|
|
|
# process startup parameters
|
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
case "$1" in
|
|
|
|
-a|--archive)
|
|
|
|
# name of backup archive
|
|
|
|
if [ -z "$2" ]; then
|
|
|
|
consoleError 1 "Please provide the name of the backup archive you want to work with or use '--list-all' to get a full list."
|
|
|
|
fi
|
|
|
|
archiveName="$2"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-f|--file)
|
|
|
|
# specific file to restore
|
|
|
|
if [ -z "$2" ]; then
|
|
|
|
consoleError 1 'Please provide the name of the specific file you want to restore.'
|
|
|
|
fi
|
|
|
|
fileName="$2"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-h|-\?|--help)
|
|
|
|
# display help
|
|
|
|
printf "\nStill working on the help text :-)\n\n"
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
-l|--list)
|
|
|
|
# list contents of specific backup
|
|
|
|
operation='viewarchive'
|
|
|
|
;;
|
|
|
|
|
|
|
|
-la|--list-all)
|
|
|
|
# list all backup archives
|
|
|
|
operation='listall'
|
|
|
|
;;
|
|
|
|
-p|--path)
|
|
|
|
# path to restore files
|
|
|
|
if [ -z "$2" ]; then
|
|
|
|
consoleError 1 'Please specify a path where you want files restored.'
|
|
|
|
fi
|
|
|
|
restorePath="${2%/}"
|
|
|
|
shift
|
|
|
|
;;
|
2020-03-26 02:25:51 -06:00
|
|
|
-r|--restore)
|
|
|
|
# restore archive/file
|
|
|
|
operation='restore'
|
|
|
|
;;
|
2020-03-26 02:21:59 -06:00
|
|
|
-v|--vars)
|
|
|
|
# location of borgvars file
|
|
|
|
if [ -z "$2" ]; then
|
|
|
|
consoleError 1 'Please provide the path to the file with your borg connection information.'
|
|
|
|
elif [ ! -f "$2" ]; then
|
|
|
|
consoleError 1 'The specified borg connection information file does not exist.'
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
varsFile="$2"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
# invalid option
|
|
|
|
printf "%s\nUnknown option: %s\n" "$err" "$1"
|
|
|
|
printf "Use '--help' for valid options.\n\n%s" "$norm"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
2020-03-26 02:35:30 -06:00
|
|
|
|
|
|
|
### check parameter validity
|
|
|
|
|
|
|
|
# no operation
|
|
|
|
if [ -z "$operation" ]; then
|
|
|
|
consoleError 1 'Nothing to do!'
|
|
|
|
fi
|
|
|
|
|
|
|
|
# list without archive
|
|
|
|
if [ "$operation" = 'list' ] && [ -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
|
|
|
|
|
|
|
|
# restore with no path
|
|
|
|
if [ "$operation" = 'restore' ] && [ -z "$restorePath" ]; then
|
|
|
|
consoleError 1 "Restore operation requested but no restore path provided."
|
|
|
|
# restore with no archive
|
|
|
|
elif [ "$operation" = 'restore' ] && [ -z "$archiveName" ]; then
|
|
|
|
consoleError 1 "Restore operation requested but no archive name provided."
|
|
|
|
fi
|
|
|
|
|
|
|
|
# file provided but no archive
|
|
|
|
if [ "$fileName" ] && [ -z "$archiveName" ]; then
|
|
|
|
consoleError 1 "Filename specified without an associated archive name."
|
|
|
|
fi
|
|
|
|
|