add better handling of borg exit codes

This commit is contained in:
Paul Wilde 2024-12-30 19:20:02 +00:00
parent fa64cf55f4
commit d79780b98b
6 changed files with 32 additions and 14 deletions

@ -1 +1 @@
Subproject commit 9b5c6dd04825d33cb27a9c6715764ad7e23182c4 Subproject commit b7d436287a9ecbe87466babf56d81f3c9fb56959

View file

@ -1,6 +1,6 @@
# Package # Package
version = "0.1.13" version = "0.1.14"
author = "Paul Wilde" author = "Paul Wilde"
description = "A Borg Backup Orchestration Tool" description = "A Borg Backup Orchestration Tool"
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"

View file

@ -10,7 +10,6 @@ import mount
import extract import extract
proc initRepo(nc: NorgConfig, repo: Repository): int = proc initRepo(nc: NorgConfig, repo: Repository): int =
return runDiscard genCommand(cmd = "init", repo = repo.path, further_args = nc.args.further_args) return runDiscard genCommand(cmd = "init", repo = repo.path, further_args = nc.args.further_args)

View file

@ -1,5 +1,7 @@
import ../model/config_type import ../model/config_type
import ../model/state_type import ../model/state_type
import ../model/exit_codes
import ../model/tool_type
import ../notifier/notifier import ../notifier/notifier
import ../model/log_type import ../model/log_type
@ -16,33 +18,35 @@ proc genArchiveName(): string =
let ts = getTime().format("yyyy-MM-dd'T'HH:mm:ss'.'ffffff") let ts = getTime().format("yyyy-MM-dd'T'HH:mm:ss'.'ffffff")
return fmt"{hostname}-{ts}" return fmt"{hostname}-{ts}"
proc createArchive(nc: NorgConfig, repo: Repository, archivename: string, retry: int = 0): int = proc createArchive(nc: NorgConfig, repo: Repository, archivename: string, retry: int = 0): (EXIT_CODE,string) =
let further_args = nc.args.further_args let further_args = nc.args.further_args
let res = run genCreateCommand(repo = archivename, sources = nc.source_directories, stats=nc.args.stats, exc=nc.exclusions, further_args = further_args) let res = run genCreateCommand(repo = archivename, sources = nc.source_directories, stats=nc.args.stats, exc=nc.exclusions, further_args = further_args)
if res != 0: if res != 0:
info "Failed to run Borg. Waiting 15 seconds and trying again" info "Failed to run Borg. Waiting 15 seconds and trying again"
sleep 15 * 1000 # 15 seconds sleep 15 * 1000 # 15 seconds
if retry == nc.retries: if retry == nc.retries:
return 1 return (BORG_ERROR, "Max Retries Reached")
else: else:
return createArchive(nc, repo, archivename, retry + 1) return createArchive(nc, repo, archivename, retry + 1)
return res return (res.toExitCode(BORG),"Success")
proc createBackup*(nc: NorgConfig, repo: Repository): int = proc createBackup*(nc: NorgConfig, repo: Repository): EXIT_CODE =
let start_time = now() let start_time = now()
discard 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,msg) = createArchive(nc, repo, archivename)
let end_time = now() let end_time = now()
let total = (end_time - start_time).inMilliSeconds() let total = (end_time - start_time).inMilliSeconds()
case res case res
of 0: of BORG_SUCCESS:
if not repo.append_only: if not repo.append_only:
discard pruneRepo(nc, repo) discard pruneRepo(nc, repo)
discard notify(nc.notifiers, state=Success, runtime=total) discard notify(nc.notifiers, state=Success, runtime=total, msg=msg)
of 1: of BORG_WARNING:
discard notify(nc.notifiers, state=Failure, runtime=total) discard notify(nc.notifiers, state=Success, runtime=total, msg="Warning")
of BORG_ERROR:
discard notify(nc.notifiers, state=Failure, runtime=total, msg=msg)
else: else:
discard notify(nc.notifiers, state=Failure, runtime=total, msg = $res) discard notify(nc.notifiers, state=Failure, runtime=total, msg = $res)
return res return res

15
norg/model/exit_codes.nim Normal file
View file

@ -0,0 +1,15 @@
import tool_type
import std/enumutils
type
EXIT_CODE* = enum
BORG_SUCCESS = 0
BORG_WARNING = 1
BORG_ERROR = 2
OTHER = 99
proc toExitCode*(i: int, tool: BackupTool): EXIT_CODE =
for code in EXIT_CODE:
if i == ord(code): return code
return OTHER

View file

@ -11,7 +11,7 @@ proc run*(cmd: string): int =
return exitcode return exitcode
except: except:
error getCurrentExceptionMsg() error getCurrentExceptionMsg()
return 1 return 2
proc runDiscard*(cmd: string): int = proc runDiscard*(cmd: string): int =
debug fmt"Trying to run : {cmd}" debug fmt"Trying to run : {cmd}"
@ -22,5 +22,5 @@ proc runDiscard*(cmd: string): int =
return exitcode return exitcode
except: except:
error getCurrentExceptionMsg() error getCurrentExceptionMsg()
return 1 return 2