2024-08-16 17:40:24 +02:00
|
|
|
import ../model/config_type
|
2024-08-17 19:00:30 +02:00
|
|
|
import ../model/state_type
|
|
|
|
import ../notifier/notifier
|
2024-08-16 17:40:24 +02:00
|
|
|
import strutils, sequtils
|
|
|
|
import osproc
|
|
|
|
import times
|
|
|
|
import os
|
|
|
|
import nativesockets
|
|
|
|
|
|
|
|
const BORG_BIN = "borg"
|
|
|
|
|
|
|
|
proc genArchiveName(): string =
|
|
|
|
let hostname = getHostname()
|
|
|
|
let ts = getTime().format("yyyy-MM-dd'T'HH:mm:ssffffff")
|
|
|
|
return hostname & "-" & ts
|
|
|
|
|
|
|
|
proc genCommand(cmd: string, repo: string, others: seq[string]): string =
|
|
|
|
var str = BORG_BIN & " " & cmd & " "
|
|
|
|
str &= " " & repo & " "
|
|
|
|
str &= others.join(" ")
|
|
|
|
return str
|
|
|
|
|
|
|
|
proc run(cmd: string): int =
|
|
|
|
echo "Trying to run : ", cmd
|
|
|
|
try:
|
|
|
|
let res = execProcess(cmd)
|
|
|
|
echo res
|
|
|
|
except:
|
|
|
|
echo getCurrentExceptionMsg()
|
|
|
|
|
|
|
|
proc runDiscard(cmd: string): int =
|
|
|
|
echo "Trying to run : ", cmd
|
|
|
|
try:
|
|
|
|
let res = execCmd(cmd)
|
|
|
|
return res
|
|
|
|
except:
|
|
|
|
echo getCurrentExceptionMsg()
|
|
|
|
return 1
|
|
|
|
|
|
|
|
proc initRepo(nc: NorgConfig, repo: Repository): int =
|
|
|
|
return runDiscard genCommand(cmd = "init", repo = repo.path, others = nc.args.others)
|
|
|
|
|
|
|
|
proc backupSources(nc: NorgConfig, repo: Repository): int =
|
2024-08-17 19:00:30 +02:00
|
|
|
if nc.notifiers.len > 0:
|
|
|
|
notify(nc.notifiers, state=Running)
|
2024-08-16 17:40:24 +02:00
|
|
|
let others = concat(nc.source_dirs, nc.args.others)
|
|
|
|
let archivename = repo.path & "::" & genArchiveName()
|
2024-08-17 19:00:30 +02:00
|
|
|
let res = run genCommand(cmd = "create", repo = archivename, others = others)
|
|
|
|
if nc.notifiers.len > 0:
|
|
|
|
case res
|
|
|
|
of 0:
|
|
|
|
notify(nc.notifiers, state=Success)
|
|
|
|
of 1:
|
|
|
|
notify(nc.notifiers, state=Failure)
|
|
|
|
else:
|
|
|
|
notify(nc.notifiers, state=Failure)
|
|
|
|
|
2024-08-16 17:40:24 +02:00
|
|
|
|
|
|
|
proc listArchives(nc: NorgConfig, repo: Repository): int =
|
|
|
|
return run genCommand(cmd = "list", repo = repo.path, others = nc.args.others)
|
|
|
|
|
|
|
|
proc mountArchive(nc: NorgConfig, repo: Repository): int =
|
|
|
|
let archive = repo.path & "::" & nc.args.others[0]
|
|
|
|
echo archive
|
|
|
|
let others = nc.args.others[1..^1]
|
|
|
|
let ok = runDiscard genCommand(cmd = "mount", repo = archive, others = others)
|
|
|
|
if ok == 0:
|
|
|
|
echo "Mounted ", archive, " at ", others[0]
|
|
|
|
else:
|
|
|
|
echo "Failed to mount ", archive
|
|
|
|
|
|
|
|
|
|
|
|
proc unmountArchive(nc: NorgConfig): int =
|
|
|
|
let ok = runDiscard genCommand(cmd = "umount", repo = "", others = nc.args.others)
|
|
|
|
if ok == 0:
|
|
|
|
echo "Unmounted ", nc.args.others[0]
|
|
|
|
else:
|
|
|
|
echo "Failed to unmount ", nc.args.others[0]
|
|
|
|
|
|
|
|
proc execute*(nc: NorgConfig) =
|
|
|
|
putEnv("BORG_PASSPHRASE", nc.getEncryptionPassword())
|
|
|
|
for repo in nc.repositories:
|
|
|
|
case nc.args.borg_cmd
|
|
|
|
of "init":
|
|
|
|
discard initRepo(nc, repo)
|
|
|
|
of "backup":
|
|
|
|
discard backupSources(nc, repo)
|
|
|
|
of "create":
|
|
|
|
discard backupSources(nc, repo)
|
|
|
|
of "list":
|
|
|
|
discard listArchives(nc, repo)
|
|
|
|
of "mount":
|
|
|
|
discard mountArchive(nc, repo)
|
|
|
|
of "umount":
|
|
|
|
discard unmountArchive(nc)
|
|
|
|
delEnv("BORG_PASSPHRASE")
|