-
Notifications
You must be signed in to change notification settings - Fork 1
/
bbbs-client
executable file
·40 lines (34 loc) · 1.96 KB
/
bbbs-client
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
#!/usr/bin/env bash
bbbs_user_name=borg
# Set an UTF-8 locale, see README.md for explanation about this
lcutf8=0 # utf8 locale is not set
unilocales=$(locale -a 2>/dev/null | grep -iE '\.utf-?8') # List UTF-8 locales
for env in 'LC_ALL' 'LC_CTYPE' 'LANG'; do # LC_ALL > LC_CTYPE > LANG ; LANGUAGE is for gettext
if [ -n "${!env:+x}" ]; then # Is defined & not null
(echo "${!env}" | grep -iE '\.utf-?8' > /dev/null) && lcutf8=1 && break # If it's utf8 already, we're done
if [ -n "$unilocales" ]; then # If we have somewhere to search
simple=$(echo "${!env}" | cut -d'.' -f1 | cut -d'@' -f1)
found=$(echo "$unilocales" | grep -im1 "^${simple}\.") # Look for an utf8 alternative
[ -n "$found" ] && export "${env}=${found}" && lcutf8=2 && break # Set it and done
fi
export -n "$env" # Otherwise remove this non-utf8 higher priority
fi
done
if [ $lcutf8 -eq 0 -a -n "$unilocales" ]; then # Still no utf8 locale prioritized, all non-utf8 locales are now unset
for try in '^C\.' '^en_GB\.' '^en_US\.' '^en_' ''; do # So last try .. C, then English, then first available
found=$(echo "$unilocales" | grep -im1 "$try")
[ -n "$found" ] && export LC_CTYPE="$found" && lcutf8=3 && break
done
fi
unset -v unilocales
bbbs_opt_path=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
bbbs_user_home=$(getent passwd "$bbbs_user_name" | cut -d: -f6)
# https://borgbackup.readthedocs.io/en/stable/usage/general.html#environment-variables
export BORG_BASE_DIR="$bbbs_user_home"
# Allow hooks to be executed
[ "$1" = "create" -a -x "${bbbs_user_home}/backup-pre" ] && "${bbbs_user_home}/backup-pre"
[ "$1" = "create-stdin" -a -x "${bbbs_user_home}/backup-pre-stdin" ] && "${bbbs_user_home}/backup-pre-stdin"
[ "$1" = "create-stdin" ] && cmd=create || cmd="$1"
"${bbbs_opt_path}/borg.bin" "$cmd" "${@:2}"
[ "$1" = "create" -a -x "${bbbs_user_home}/backup-post" ] && "${bbbs_user_home}/backup-post"
[ "$1" = "create-stdin" -a -x "${bbbs_user_home}/backup-post-stdin" ] && "${bbbs_user_home}/backup-post-stdin"