added borg extract functionality

This commit is contained in:
Paul Wilde 2024-08-18 15:43:11 +01:00
parent c659fe3212
commit 00c2696f82
3 changed files with 29 additions and 5 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
bin bin
*.toml *.toml
test

View file

@ -47,8 +47,8 @@ proc backupSources(nc: NorgConfig, repo: Repository): int =
let archivename = repo.path & "::" & genArchiveName() let archivename = repo.path & "::" & genArchiveName()
let res = run genCommand(cmd = "create", repo = archivename, others = others) let res = run genCommand(cmd = "create", repo = archivename, others = others)
let end_time = now() let end_time = now()
let elapsed = (end_time - start_time) let total = (end_time - start_time).inMilliSeconds()
let total = elapsed.inMilliSeconds() case res
of 0: of 0:
notify(nc.notifiers, state=Success, runtime=total) notify(nc.notifiers, state=Success, runtime=total)
of 1: of 1:
@ -62,7 +62,6 @@ proc listArchives(nc: NorgConfig, repo: Repository): int =
proc mountArchive(nc: NorgConfig, repo: Repository): int = proc mountArchive(nc: NorgConfig, repo: Repository): int =
let archive = repo.path & "::" & nc.args.others[0] let archive = repo.path & "::" & nc.args.others[0]
echo archive
let others = nc.args.others[1..^1] let others = nc.args.others[1..^1]
let ok = runDiscard genCommand(cmd = "mount", repo = archive, others = others) let ok = runDiscard genCommand(cmd = "mount", repo = archive, others = others)
if ok == 0: if ok == 0:
@ -70,7 +69,6 @@ proc mountArchive(nc: NorgConfig, repo: Repository): int =
else: else:
echo "Failed to mount ", archive echo "Failed to mount ", archive
proc unmountArchive(nc: NorgConfig): int = proc unmountArchive(nc: NorgConfig): int =
let ok = runDiscard genCommand(cmd = "umount", repo = "", others = nc.args.others) let ok = runDiscard genCommand(cmd = "umount", repo = "", others = nc.args.others)
if ok == 0: if ok == 0:
@ -78,6 +76,26 @@ proc unmountArchive(nc: NorgConfig): int =
else: else:
echo "Failed to unmount ", nc.args.others[0] echo "Failed to unmount ", nc.args.others[0]
proc isEmpty(dir: string): bool =
var count = 0
for idx, f in walkDir(dir): return false
#count += 1
return count == 0
proc extractArchive(nc: NorgConfig, repo: Repository): int =
let archive = repo.path & "::" & nc.args.others[0]
var others = nc.args.others[1..^1]
if nc.args.extract_destination != "":
discard existsOrCreateDir(nc.args.extract_destination)
setCurrentDir(nc.args.extract_destination)
let dir = getCurrentDir()
if dir.isEmpty():
echo "Restoring..."
let ok = run(genCommand(cmd = "extract", repo = archive, others = others))
return ok
else:
echo "Not restoring to non-empty destination\r\nPlease use the --destination flag"
proc execute*(nc: NorgConfig) = proc execute*(nc: NorgConfig) =
putEnv("BORG_PASSPHRASE", nc.getEncryptionPassword()) putEnv("BORG_PASSPHRASE", nc.getEncryptionPassword())
for repo in nc.repositories: for repo in nc.repositories:
@ -94,4 +112,6 @@ proc execute*(nc: NorgConfig) =
discard mountArchive(nc, repo) discard mountArchive(nc, repo)
of "umount": of "umount":
discard unmountArchive(nc) discard unmountArchive(nc)
of "extract":
discard extractArchive(nc, repo)
delEnv("BORG_PASSPHRASE") delEnv("BORG_PASSPHRASE")

View file

@ -3,6 +3,7 @@ import argparse
type type
NorgArgs* = object NorgArgs* = object
config_file*: string config_file*: string
extract_destination*: string
borg_cmd*: string borg_cmd*: string
others*: seq[string] others*: seq[string]
@ -11,11 +12,13 @@ var norg_args*: NorgArgs = NorgArgs()
proc parseArgs*() = proc parseArgs*() =
var p = newParser: var p = newParser:
option("-c", "--config", help="Config file to use", required = true) option("-c", "--config", help="Config file to use", required = true)
arg("borg_cmd", default=some("backup")) option("-d", "--destination", help="Destination when extracting backup", required = false)
arg("borg_cmd", default=some("list"))
arg("others", nargs = -1) arg("others", nargs = -1)
try: try:
var opts = p.parse(commandLineParams()) var opts = p.parse(commandLineParams())
norg_args.config_file = opts.config norg_args.config_file = opts.config
norg_args.extract_destination = opts.destination
norg_args.borg_cmd = opts.borg_cmd norg_args.borg_cmd = opts.borg_cmd
norg_args.others = opts.others norg_args.others = opts.others