2
0
Fork 0
main
Ducky 2024-04-01 21:26:52 +01:00
parent 73e43624b7
commit bfbf3c128f
2 changed files with 78 additions and 32 deletions

View File

@ -73,32 +73,53 @@ function invoke_restic() {
if [[ -z $command ]]; then
say warning "No command specified. Not running"
break
elif [[ $command == "generate" || $command == "self-update" ]]; then
say warning "Unsupported command: $command"
else
attempts=10
attempts_delay=600
break
fi
while true; do
set -o pipefail
exec 3>&1
"$restic_path" \
--cache-dir "$cache_dir" \
--password-file "$restic_repo_passwd_file" \
--repo "$(cat $restic_repo_file)" \
$command $args
restic_out="$("$restic_path" \
--cache-dir "$cache_dir" \
--password-file "$restic_repo_passwd_file" \
--repo "$(cat $restic_repo_file)" \
$command $args 2>&1 | tee /dev/fd/3)"
#attempts=10
#attempts_delay=600
if [[ $? == 0 ]]; then
break
else
say warning "Command failed."
echo "$restic_out"
/usr/local/bin/sh.zio.notify \
--title "Backup failed" \
--message '```\\n'"${restic_out//$'\n'/'\\n'}"'\\n```' \
--level "warning"
break
#while true; do
#set -o pipefail
#exec 3>&1
#function trigger_ntfy() {
# level="$1"
# title="$2"
# output="$3"
# /usr/local/bin/sh.zio.notify
# --invoked-by "sh.zio.backup" \
# --level "$level" \
# --message '```\\n'"${output//$'\n'/'\\n'}"'\\n```' \
# --title "Backup failed"
#}
#restic_out="$("$restic_path" \
# --cache-dir "$cache_dir" \
# --password-file "$restic_repo_passwd_file" \
# --repo "$(cat $restic_repo_file)" \
# $command $args 2>&1 | tee /dev/fd/3)"
#else
#say warning "Command failed."
#echo "$restic_out"
#/usr/local/bin/sh.zio.notify \
# --title "Backup failed" \
# --message '```\\n'"${restic_out//$'\n'/'\\n'}"'\\n```' \
# --level "warning" \
# --invoked-by "sh.zio.backup"
#trigger_ntfy "warning" "Backup failed: "
#break
#echo "---- THE OUTPUT: $restic_out"
#if [[ $attempts == 0 ]]; then
# say warning "Command failed. No attempts left"
@ -108,12 +129,12 @@ function invoke_restic() {
# say warning "Command failed. Trying again in $attempts_delay seconds ($attempts attempts remaining)..."
# sleep $attempts_delay
#fi
fi
#fi
set +o pipefail
exec 3>&-
done
fi
#set +o pipefail
#exec 3>&-
#done
#fi
}
function invoke_script() {

View File

@ -6,9 +6,19 @@ _PLUG_ARGS=(
"message;m;;string;(No Message)"
"title;t;;string;(No Title)"
"level;l;;string;info"
"invoker;i;;string"
"invoked-by;i;;string"
)
function get_cache_header() {
topic="$1"
if [[ "$topic" == "zio_test" ]]; then
echo ""
else
echo "Cache: no"
fi
}
function get_level_emoji() {
case "$1" in
"critical"|"error") echo "⛔" ;;
@ -30,6 +40,18 @@ function get_level_priority() {
esac
}
function get_topic() {
invoked_by="$1"
topic_prefix="zio"
topic="test"
if [[ -n "$invoked_by" ]]; then
topic="$(echo "$invoked_by" | sed -r 's/sh.zio.//g' | sed -r 's/[.]+/-/g')"
fi
echo "${topic_prefix}_$topic"
}
function main() {
token_path="$(get_config_dir "sh.zio.notify")/token"
[[ ! -f "$token_path" ]] && die "'$token_path' does not exist"
@ -37,24 +59,27 @@ function main() {
token="$(cat "$token_path")"
hostname="$(hostname -s)"
invoker="$_invoker"
invoked_by="$_invoked_by"
level_emoji="$(get_level_emoji "$_level")"
triggered_by="sh.zio.backup"
n_message="$_message"
n_priority="$(get_level_priority "$_level")"
n_title="$level_emoji [$hostname] $_title"
n_topic="zio_backup_a123"
n_topic="$(get_topic "$_invoked_by")"
body='{
"message": "'$n_message'",
"priority": '$n_priority',
"tags": ["'$hostname'", "'$invoker'"],
"tags": ["'$hostname'", "'$invoked_by'"],
"title": "'$n_title'",
"topic": "'$n_topic'"
}'
curl -d "$body" -H "Authorization: Bearer $token" -H "Cache: no" -H "Markdown: yes" "https://ntfy.zio.sh/"
curl -d "$body" \
-H "Authorization: Bearer $token" \
-H "$(get_cache_header "$n_topic")" \
-H "Markdown: yes" \
"https://ntfy.zio.sh/"
}
if [[ $_PLUG_INVOKED != "true" ]]; then