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

View File

@ -6,9 +6,19 @@ _PLUG_ARGS=(
"message;m;;string;(No Message)" "message;m;;string;(No Message)"
"title;t;;string;(No Title)" "title;t;;string;(No Title)"
"level;l;;string;info" "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() { function get_level_emoji() {
case "$1" in case "$1" in
"critical"|"error") echo "⛔" ;; "critical"|"error") echo "⛔" ;;
@ -30,6 +40,18 @@ function get_level_priority() {
esac 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() { function main() {
token_path="$(get_config_dir "sh.zio.notify")/token" token_path="$(get_config_dir "sh.zio.notify")/token"
[[ ! -f "$token_path" ]] && die "'$token_path' does not exist" [[ ! -f "$token_path" ]] && die "'$token_path' does not exist"
@ -37,24 +59,27 @@ function main() {
token="$(cat "$token_path")" token="$(cat "$token_path")"
hostname="$(hostname -s)" hostname="$(hostname -s)"
invoker="$_invoker" invoked_by="$_invoked_by"
level_emoji="$(get_level_emoji "$_level")" level_emoji="$(get_level_emoji "$_level")"
triggered_by="sh.zio.backup"
n_message="$_message" n_message="$_message"
n_priority="$(get_level_priority "$_level")" n_priority="$(get_level_priority "$_level")"
n_title="$level_emoji [$hostname] $_title" n_title="$level_emoji [$hostname] $_title"
n_topic="zio_backup_a123" n_topic="$(get_topic "$_invoked_by")"
body='{ body='{
"message": "'$n_message'", "message": "'$n_message'",
"priority": '$n_priority', "priority": '$n_priority',
"tags": ["'$hostname'", "'$invoker'"], "tags": ["'$hostname'", "'$invoked_by'"],
"title": "'$n_title'", "title": "'$n_title'",
"topic": "'$n_topic'" "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 if [[ $_PLUG_INVOKED != "true" ]]; then