From 7654fc04bfa80b5e9315ee6ace43300fe9792386 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Fri, 23 Aug 2024 12:31:43 +0100 Subject: [PATCH] WIP: find out why --repository arg isn't working --- norg/borg/extract.nim | 2 +- norg/config/args.nim | 1 - norg/config/init.nim | 5 +++-- norg/model/command_type.nim | 1 + norg/restic/execute.nim | 5 +++++ norg/restic/extract.nim | 5 ----- norg/restic/restic.nim | 6 +++--- norg/restic/restore.nim | 24 ++++++++++++++++++++++++ 8 files changed, 37 insertions(+), 12 deletions(-) delete mode 100644 norg/restic/extract.nim create mode 100644 norg/restic/restore.nim diff --git a/norg/borg/extract.nim b/norg/borg/extract.nim index acd4c8a..0e02a17 100644 --- a/norg/borg/extract.nim +++ b/norg/borg/extract.nim @@ -21,4 +21,4 @@ proc extractArchive*(nc: NorgConfig, repo: Repository): int = let ok = run genCommand(cmd = "extract", repo = archive, further_args = further_args) return ok else: - echo "Not restoring to non-empty destination\r\nPlease use the --destination flag" + echo "Not restoring to non-empty destination\r\nPlease use the --destination flag or cd to an empty directory." diff --git a/norg/config/args.nim b/norg/config/args.nim index c957502..4f0d33e 100644 --- a/norg/config/args.nim +++ b/norg/config/args.nim @@ -26,7 +26,6 @@ proc parseArgs*() = norg_args.command = opts.command.toCommand() norg_args.repository = opts.repository norg_args.further_args = opts.further_args - except ShortCircuit as err: if err.flag == "argparse_help": echo err.help diff --git a/norg/config/init.nim b/norg/config/init.nim index fa64869..bfd1879 100644 --- a/norg/config/init.nim +++ b/norg/config/init.nim @@ -26,8 +26,6 @@ proc parseRepositories*(rep_conf: TomlValueRef): seq[Repository] = if rtable.hasKey("tool"): repo.tool = rtable["tool"].getStr("borg").toBackupTool() repos.add(repo) - if norg_config.args.repository != "": - repos = repos.findRepository(norg_config.args.repository) return repos proc parseEncryption*(enc_conf: TomlValueRef) = @@ -41,6 +39,9 @@ proc parseConfigFile*(file: string): NorgConfig = let in_conf = parsetoml.parseFile(file) norg_config.source_directories = parseSourceDirectories(in_conf) norg_config.repositories = parseRepositories(in_conf{"repositories"}) + if norg_config.args.repository != "": + echo "Filtering repo list to selected: ", norg_config.args.repository + norg_config.repositories = norg_config.repositories.findRepository(norg_config.args.repository) parseEncryption(in_conf{"encryption"}) norg_config.notifiers = parseNotifiers(in_conf) norg_config.actions = parseActions(in_conf{"actions"}) diff --git a/norg/model/command_type.nim b/norg/model/command_type.nim index 21087f7..15b5962 100644 --- a/norg/model/command_type.nim +++ b/norg/model/command_type.nim @@ -19,5 +19,6 @@ proc toCommand*(str: string): Command = case str of "backup": return CREATE of "snapshots","archives": return LIST + of "restore": return EXTRACT diff --git a/norg/restic/execute.nim b/norg/restic/execute.nim index 14acc59..10d4c0e 100644 --- a/norg/restic/execute.nim +++ b/norg/restic/execute.nim @@ -10,3 +10,8 @@ proc genCommand*(cmd: string, repo: string, further_args: seq[string]): string = let cmd = fmt"{RESTIC_BIN} {cmd} -r {repo} {args}" return cmd +proc genRestoreCommand*(cmd: string, repo_snapshot: string, destination: string, further_args: seq[string]): string = + let args = further_args.join(" ") + let cmd = fmt"{RESTIC_BIN} {cmd} -r {repo_snapshot} --target {destination} {args}" + return cmd + diff --git a/norg/restic/extract.nim b/norg/restic/extract.nim deleted file mode 100644 index 4264ac8..0000000 --- a/norg/restic/extract.nim +++ /dev/null @@ -1,5 +0,0 @@ -import ../model/config_type - -proc extractArchive*(nc: NorgConfig, repo: Repository): int = - echo "Not Yet Implemented" - discard diff --git a/norg/restic/restic.nim b/norg/restic/restic.nim index 7c83ddd..22e358d 100644 --- a/norg/restic/restic.nim +++ b/norg/restic/restic.nim @@ -5,7 +5,7 @@ import init import backup import list import mount -import extract +import restore import prune proc execute*(nc: NorgConfig, repo: Repository) = @@ -26,8 +26,8 @@ proc execute*(nc: NorgConfig, repo: Repository) = discard mountSnapshot(nc, repo) of EXTRACT: run_actions(norg_config.actions.before_extract) - echo "Extracting backup from ", repo.label - discard extractArchive(nc, repo) + echo "Restoring snapshot from ", repo.label + discard restoreSnapshot(nc, repo) run_actions(norg_config.actions.after_extract) of PRUNE: run_actions(norg_config.actions.before_prune) diff --git a/norg/restic/restore.nim b/norg/restic/restore.nim new file mode 100644 index 0000000..4f1de51 --- /dev/null +++ b/norg/restic/restore.nim @@ -0,0 +1,24 @@ +import ../model/config_type +import execute +import strformat +import os + +proc isEmpty(dir: string): bool = + var count = 0 + for idx, f in walkDir(dir): return false + #count += 1 + return count == 0 + +proc restoreSnapshot*(nc: NorgConfig, repo: Repository): int = + let repo_snapshot = fmt"{repo.path} {nc.args.further_args[0]}" + var further_args = nc.args.further_args[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 genRestoreCommand(cmd = "restore", repo_snapshot = repo_snapshot, destination = nc.args.extract_destination, further_args = further_args) + return ok + else: + echo "Not restoring to non-empty destination\r\nPlease use the --destination flag or cd to an empty directory."