norgbackup/norg/borg/create.nim

49 lines
1.5 KiB
Nim
Raw Permalink Normal View History

2024-08-19 03:21:46 +02:00
import ../model/config_type
import ../model/state_type
import ../notifier/notifier
2024-08-27 22:16:31 +02:00
import ../model/log_type
2024-08-19 03:21:46 +02:00
import execute
import prune
import os
import times
import strformat
import nativesockets
proc genArchiveName(): string =
let hostname = getHostname()
let ts = getTime().format("yyyy-MM-dd'T'HH:mm:ss'.'ffffff")
return fmt"{hostname}-{ts}"
proc createArchive(nc: NorgConfig, repo: Repository, archivename: string, retry: int = 0): int =
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)
2024-08-19 03:21:46 +02:00
if res != 0:
2024-11-25 12:14:50 +01:00
info "Failed to run Borg. Waiting 15 seconds and trying again"
2024-08-19 03:21:46 +02:00
sleep 15 * 1000 # 15 seconds
if retry == nc.retries:
return 1
else:
return createArchive(nc, repo, archivename, retry + 1)
return res
proc createBackup*(nc: NorgConfig, repo: Repository): int =
let start_time = now()
2024-11-25 12:20:50 +01:00
discard notify(nc.notifiers, state=Running)
2024-08-19 03:21:46 +02:00
let archivename = repo.path & "::" & genArchiveName()
2024-08-27 22:16:31 +02:00
debug "Creating Archive: ", archivename
2024-08-19 03:21:46 +02:00
let res = createArchive(nc, repo, archivename)
2024-08-23 11:30:36 +02:00
let end_time = now()
2024-08-19 03:21:46 +02:00
let total = (end_time - start_time).inMilliSeconds()
case res
of 0:
if not repo.append_only:
discard pruneRepo(nc, repo)
2024-11-25 12:20:50 +01:00
discard notify(nc.notifiers, state=Success, runtime=total)
2024-08-19 03:21:46 +02:00
of 1:
2024-11-25 12:20:50 +01:00
discard notify(nc.notifiers, state=Failure, runtime=total)
2024-08-19 03:21:46 +02:00
else:
2024-11-25 12:20:50 +01:00
discard notify(nc.notifiers, state=Failure, runtime=total, msg = $res)
2024-08-19 03:21:46 +02:00
return res