83 lines
2.4 KiB
Nim
83 lines
2.4 KiB
Nim
import ../model/config_type
|
|
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 =
|
|
let others = concat(nc.source_dirs, nc.args.others)
|
|
let archivename = repo.path & "::" & genArchiveName()
|
|
return run genCommand(cmd = "create", repo = archivename, others = others)
|
|
|
|
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")
|