-
Notifications
You must be signed in to change notification settings - Fork 14
/
borgcron_starter.sh
executable file
·77 lines (69 loc) · 1.92 KB
/
borgcron_starter.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/sh
# Cron script to execute the borg backup.
#
# LICENSE: MIT license, see LICENSE.md
#
CURRENT_DIR="$( dirname "$0" )"
# dir where config files are stored
CONFIG_DIR="$CURRENT_DIR/config"
# basic functions
track_exitcode() {
if [ "$1" -gt "$exitcode" ]; then
exitcode="$1"
fi
}
dir_contains_files() {
ls -A "$1"
}
get_full_path() {
# thanks https://stackoverflow.com/questions/5265702/how-to-get-full-path-of-a-file
# use realpath command, if it exists
if command -v realpath >/dev/null 2>&1; then
realpath "$1"
else
readlink -f "$1"
fi
}
cli_help() {
echo "Usage:"
echo "$( basename "$0" ) [<files>]"
echo
echo "files If <files> is given, it will cycle through each given config file and"
echo " execute the backups exactly as given on the command line."
echo " If it is not given, it will just run all backups one by one."
}
exitcode=0 #exitcode on zero :)
# check for error if config dir is empty
if [ ! -d "$CONFIG_DIR" ] || [ ! "$( dir_contains_files "$CONFIG_DIR" )" ]; then
echo "No backup settings file(s) could be found in the config folder \"$CONFIG_DIR\"." >&2
echo "To get help enter: $( basename "$0" ) --help"
exit 1
fi
# parse parameters
case "$1" in
'' ) # process all backup config files in $CONFIG_DIR
for configfile in "$CONFIG_DIR"/*.sh;
do
"$CURRENT_DIR/borgcron.sh" "$( get_full_path "$configfile" )"
track_exitcode "$?"
done
;;
--help|-h|-? ) # show help message
cli_help
exit
;;
*) # specific config file(s) passed
for configfile in "$@"; do
# remove possible ".sh" ending
configfile="$( echo "$configfile"|sed 's/.sh$//')"
if [ -e "$CONFIG_DIR/$configfile.sh" ]; then
"$CURRENT_DIR/borgcron.sh" "$( get_full_path "$CONFIG_DIR/$configfile.sh" )"
track_exitcode "$?"
else
echo "The backup settings file \"$configfile.sh\" could not be found." >&2
track_exitcode 1 # custom exit code for "file not found" warning
fi
done
;;
esac
exit "$exitcode"