Files
server-backup/main.sh
T
2025-03-22 00:36:45 +01:00

121 lines
3.2 KiB
Bash
Executable File

#!/bin/bash
#
# entrypoint for all actions
#
show_help() {
echo "usage: $0 options"
echo " -d, --daily start daily backup on local machine"
echo " -w, --weekly start weekly backup on local machine"
echo " --list list archives"
echo ' --content <archive> '
echo " show contents of archive"
echo ' -r, --restore <archive> <file(s)> '
echo " restore files from archive to pwd"
echo " -h, -?. --help show this help"
echo " "
echo " Script muss auf dem jeweiligen Server ausgeführt werden (ggf. via ssh)."
echo " "
}
HERE=$(dirname "$0")
if [ -f $HERE/main.inc.sh ]; then
. $HERE/main.inc.sh
else
echo "**"
echo "** ERROR: Datei \"$HERE/main.inc.sh\" nicht gefunden. "
echo "** Script wird abgebrochen"
echo "**"
show_help
exit 1
fi
if [ -f $HERE/$HOST_NAME.sh ]; then
. $HERE/$HOST_NAME.sh
else
echo "**"
echo "** ERROR: Datei \"$HOST_NAME.sh\" nicht gefunden (falscher Rechner?). "
echo "** Script wird abgebrochen."
echo "**"
show_help
exit 2
fi
file=
archive=
while :; do
case $1 in
-h|-\?|--help)
show_help # Display a usage synopsis.
exit 0
;;
-d|--daily)
backup_daily
break
;;
-w|--weekly)
backup_weekly
break
;;
--list)
list_archives "$NAS_ROOT/daily/$HOST_NAME"
if [ -d "$NAS_ROOT/weekly/$HOST_NAME" ]; then
list_archives "$NAS_ROOT/weekly/$HOST_NAME"
fi
break;
;;
--content)
if [ "$2" ]; then
list_archive_content "$NAS_ROOT" "$2"
else
echo 'ERROR: "--content" requires a non-empty option argument.'
fi
break
;;
-r|--restore)
if [ "$2" ]; then
archive=$2
shift
restore "$archive" "$@"
else
echo 'ERROR: "--restore" requires a non-empty option argument.'
fi
break
;;
# --- example -> https://mywiki.wooledge.org/BashFAQ/035
-f|--file) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
file=$2
shift
else
die 'ERROR: "--file" requires a non-empty option argument.'
fi
;;
--file=?*)
file=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
--file=) # Handle the case of an empty --file=
die 'ERROR: "--file" requires a non-empty option argument.'
;;
--) # End of all options.
shift
break
;;
-?*)
echo "WARN: Unknown option (ignored): $1"
echo
show_help
exit
;;
*) # Default case: No more options, so break out of the loop.
break
esac
shift
done
exit 0