made notify non-discardable

This commit is contained in:
Paul Wilde 2024-11-25 11:20:50 +00:00
parent bb114f66e5
commit f6301cfaf2
7 changed files with 130 additions and 19 deletions

View file

@ -30,7 +30,7 @@ proc createArchive(nc: NorgConfig, repo: Repository, archivename: string, retry:
proc createBackup*(nc: NorgConfig, repo: Repository): int = proc createBackup*(nc: NorgConfig, repo: Repository): int =
let start_time = now() let start_time = now()
notify(nc.notifiers, state=Running) discard notify(nc.notifiers, state=Running)
let archivename = repo.path & "::" & genArchiveName() let archivename = repo.path & "::" & genArchiveName()
debug "Creating Archive: ", archivename debug "Creating Archive: ", archivename
let res = createArchive(nc, repo, archivename) let res = createArchive(nc, repo, archivename)
@ -40,9 +40,9 @@ proc createBackup*(nc: NorgConfig, repo: Repository): int =
of 0: of 0:
if not repo.append_only: if not repo.append_only:
discard pruneRepo(nc, repo) discard pruneRepo(nc, repo)
notify(nc.notifiers, state=Success, runtime=total) discard notify(nc.notifiers, state=Success, runtime=total)
of 1: of 1:
notify(nc.notifiers, state=Failure, runtime=total) discard notify(nc.notifiers, state=Failure, runtime=total)
else: else:
notify(nc.notifiers, state=Failure, runtime=total, msg = $res) discard notify(nc.notifiers, state=Failure, runtime=total, msg = $res)
return res return res

View file

@ -100,7 +100,6 @@ proc generateConfigFile*(file: string) =
var line = "" var line = ""
if r_strm != nil and w_strm != nil: if r_strm != nil and w_strm != nil:
while r_strm.readLine(line): while r_strm.readLine(line):
echo line
w_strm.writeLine("#" & line) w_strm.writeLine("#" & line)
r_strm.close() r_strm.close()
w_strm.close() w_strm.close()

View file

@ -18,9 +18,6 @@ proc initLogger*(level: Level = lvlInfo, strfmt: string = "[$levelname] ", log_f
filelogger = newFileLogger(log_file, levelThreshold=level, fmtStr=str) filelogger = newFileLogger(log_file, levelThreshold=level, fmtStr=str)
filelogger.log(lvlNotice, "Log Level: ", $level) filelogger.log(lvlNotice, "Log Level: ", $level)
#proc initLoggerThread*(level: Level = lvlInfo, strfmt: string = "[$levelname] ") =
# initLogThread(strfmt, level)
# threadlogging.notice("Log Level: ", $level)
proc newLogInfo*(): LogInfo = proc newLogInfo*(): LogInfo =
var log = LogInfo() var log = LogInfo()

View file

@ -1,7 +1,7 @@
import ../model/state_type import ../model/state_type
import ../model/notifier_types import ../model/notifier_types
proc notify*(notifiers: Notifiers, state: State, runtime: int = 0, msg: string = ""): int {.discardable.} = proc notify*(notifiers: Notifiers, state: State, runtime: int = 0, msg: string = ""): int =
if notifiers.uptimekuma.base_url != "" and if notifiers.uptimekuma.base_url != "" and
state in notifiers.uptimekuma.states: state in notifiers.uptimekuma.states:
notifiers.uptimekuma.send_notify(state, runtime, msg) return notifiers.uptimekuma.send_notify(state, runtime, msg)

View file

@ -21,9 +21,12 @@ proc send_notify*(uk: UptimeKuma, state: State, runtime: int = 0, msg: string =
let url = fmt"{uk.base_url}?status={status}&msg={message}&ping={runtime}" let url = fmt"{uk.base_url}?status={status}&msg={message}&ping={runtime}"
debug "Sending notification to " & url debug "Sending notification to " & url
try:
let res = sendHttpRequest(HttpGet, url) let res = sendHttpRequest(HttpGet, url)
if res.status == $Http200: if res.status == $Http200:
return 0 return 0
else: else:
return 1 return 1
except:
error(getCurrentExceptionMsg())

View file

@ -21,7 +21,7 @@ proc createArchive(nc: NorgConfig, repo: Repository, retry: int = 0): int =
proc createBackup*(nc: NorgConfig, repo: Repository): int = proc createBackup*(nc: NorgConfig, repo: Repository): int =
let start_time = now() let start_time = now()
notify(nc.notifiers, state=Running) discard notify(nc.notifiers, state=Running)
let res = createArchive(nc, repo) let res = createArchive(nc, repo)
let end_time = now() let end_time = now()
let total = (end_time - start_time).inMilliSeconds() let total = (end_time - start_time).inMilliSeconds()
@ -29,9 +29,9 @@ proc createBackup*(nc: NorgConfig, repo: Repository): int =
of 0: of 0:
if not repo.append_only: if not repo.append_only:
discard pruneRepo(nc, repo) discard pruneRepo(nc, repo)
notify(nc.notifiers, state=Success, runtime=total) discard notify(nc.notifiers, state=Success, runtime=total)
of 1: of 1:
notify(nc.notifiers, state=Failure, runtime=total) discard notify(nc.notifiers, state=Failure, runtime=total)
else: else:
notify(nc.notifiers, state=Failure, runtime=total, msg = $res) discard notify(nc.notifiers, state=Failure, runtime=total, msg = $res)
return res return res

112
norg_backup.toml Normal file
View file

@ -0,0 +1,112 @@
## Defines what folders you would like to back up
#source_directories = [
# "/home/me",
# "/opt/other_things"
#]
#
## Defines your backup repository/repositories
#[[repositories]]
#label = "LocalBorgBackup"
#path = "/path/to/backup"
## You can define to backup tool here, default is "borg"
#tool = "borg"
#
## You can add more repositories of varying types.
## When defining multiple repositories, a new backup will run for each repository
## defined.
#[[repositories]]
#label = "RemoteBorgBase"
#path = "ssh://1234abcd@1234abcd.repo.borgbase.com/./repo"
#tool = "borg"
#
#[[repositories]]
#label = "LocalResticBackup
#path = "/path/to/another/backup"
#
## You can define restic as a backup tool
#tool = "restic"
#
## if a repository is append-only, you can tell norg not to try and prune it
## Note: This does not set the actual repository as append-only, only informs Norg
## so the prune step does not run which can cause failed notifications with restic
#append_only = true
#
## Defines encryption information i.e. backup encryption passphrase
## If a passphrase is not set or is incorrect, borg/restic will prompt you for one
## which is likely to cause your backups to not run.
#[encryption]
## Set a string variable for the passphrase
#encryption_passphrase = "MyVeryS3curePassphrase!"
#
## Get the passphrase from the result of a command
## encryption_passcommand = "cat my_passphrase.txt"
#
## These actions can be configured to run scripts (i.e. back up a database)
## at various times throughout the running process
#[actions]
## "everything" means before or after every possible option for all repositories
#before_everything = ["echo before everything"]
#after_everything = ["echo after everything"]
## "actions" means before any action, per repository
#before_actions = ["echo before actions"]
#after_actions = ["echo after actions"]
## before or after the backup process per repository
#before_backup = ["echo before backup"]
#after_backup = ["echo after backup"]
## before or after the extract process per repository
#before_extract = ["echo before extract"]
#after_extract = ["echo after extract"]
## before or after the prune process per repository
#before_prune = ["echo before prune"]
#after_prune = ["echo after prune"]
## before or after the compact process per respository
#before_compact = ["echo before compact"]
#after_compact = ["echo after compact"]
## before or after the check processs per repository
#before_check = ["echo before check"]
#after_check = ["echo after check"]
#
## Send a status update to Uptime Kuma
## more status monitor tools are planned to be supported in the future
#[uptimekuma]
## The base/push url of your Uptime Kuma monitor - without the query string.
## The query string will be generated at run time and will change dependant on the state of your backup.
#base_url = "https://uptime.kuma.url/api/push/1234abcd"
#
## what backup states you wish to send an alert for, defaults to Success, Failure and Running
#states = ["Success","Failure", "Running"]
#
## Set locations of the borg or restic binaries if you need to.
## Norg will check some common locations and use the first one it finds, but
## if you have them installed in a non-standard location, set that location here
#[binaries]
#borg_bin = "/usr/local/bin/borg"
#restic_bin = "/usr/local/bin/restic"
#
## Logging configuration - will output to console
#[logging]
## Log level options are below, each option includes the logs from the one before it
## "notice", "info", "warn", "error", "fatal", "debug"
#log_level = "info"
#
## Exclude (or include) files/folders based on filenames or patterns.
#[exclusions]
## Exclude files or folders matching paths
#excludes = [
# "/etc/file_i_dont_want",
# "/folder_i_dont_want"
#]
#
## Put all your exlusions in a plain text file and they can be linked to here
#exclude_file = "/home/me/exclusions.txt"
#
## Include or Exclude based on patterns (Borg Only)
## is a direct reference to borg-patterns: https://manpages.debian.org/testing/borgbackup/borg-patterns.1.en.html
#patterns = [
# "/home/*/junk"
#]
#
## Or use a patterns file (Borg Only)
#patterns_file = "/home/me/patterns.lst"
#
#