diff --git a/norg/borg/borg.nim b/norg/borg/borg.nim index a99a64c..819894a 100644 --- a/norg/borg/borg.nim +++ b/norg/borg/borg.nim @@ -10,10 +10,10 @@ import extract proc initRepo(nc: NorgConfig, repo: Repository): int = - return runDiscard genCommand(cmd = "init", repo = repo.path, others = nc.args.others) + return runDiscard genCommand(cmd = "init", repo = repo.path, further_args = nc.args.further_args) proc listArchives(nc: NorgConfig, repo: Repository): int = - return run genCommand(cmd = "list", repo = repo.path, others = nc.args.others) + return run genCommand(cmd = "list", repo = repo.path, further_args = nc.args.further_args) proc compactRepo(nc: NorgConfig, repo: Repository): int = echo "Not Yet Implemented." @@ -26,23 +26,29 @@ proc checkRepo(nc: NorgConfig, repo: Repository): int = proc execute*(nc: NorgConfig, repo: Repository) = case nc.args.command of INIT: + echo "Initializing repo: ", repo.label discard initRepo(nc, repo) of CREATE: run_actions(norg_config.actions.before_backup) + echo "Creating archive on ", repo.label discard createBackup(nc, repo) run_actions(norg_config.actions.after_backup) of LIST: + echo "Listing Archives on ", repo.label discard listArchives(nc, repo) of MOUNT: + echo "Mounting Archive from ", repo.label discard mountArchive(nc, repo) of UMOUNT: discard unmountArchive(nc) of EXTRACT: run_actions(norg_config.actions.before_extract) + echo "Extracting archive from ", repo.label discard extractArchive(nc, repo) run_actions(norg_config.actions.after_extract) of PRUNE: run_actions(norg_config.actions.before_prune) + echo "Pruning repo: ", repo.label discard pruneRepo(nc, repo) run_actions(norg_config.actions.after_prune) of COMPACT: diff --git a/norg/borg/create.nim b/norg/borg/create.nim index cfb0dbc..56cf01d 100644 --- a/norg/borg/create.nim +++ b/norg/borg/create.nim @@ -17,8 +17,8 @@ proc genArchiveName(): string = return fmt"{hostname}-{ts}" proc createArchive(nc: NorgConfig, repo: Repository, archivename: string, retry: int = 0): int = - let others = concat(nc.source_directories, nc.args.others) - let res = run genCommand(cmd = "create", repo = archivename, others = others) + let further_args = concat(nc.source_directories, nc.args.further_args) + let res = run genCommand(cmd = "create", repo = archivename, further_args = further_args) if res != 0: sleep 15 * 1000 # 15 seconds if retry == nc.retries: diff --git a/norg/borg/execute.nim b/norg/borg/execute.nim index 999a3bb..ff48b70 100644 --- a/norg/borg/execute.nim +++ b/norg/borg/execute.nim @@ -5,8 +5,8 @@ import ../model/command_type import ../utils/run export run -proc genCommand*(cmd: string, repo: string, others: seq[string]): string = - let args = others.join(" ") +proc genCommand*(cmd: string, repo: string, further_args: seq[string]): string = + let args = further_args.join(" ") let cmd = fmt"{BORG_BIN} {cmd} {repo} {args}" return cmd diff --git a/norg/borg/extract.nim b/norg/borg/extract.nim index e94c930..acd4c8a 100644 --- a/norg/borg/extract.nim +++ b/norg/borg/extract.nim @@ -10,15 +10,15 @@ proc isEmpty(dir: string): bool = return count == 0 proc extractArchive*(nc: NorgConfig, repo: Repository): int = - let archive = fmt"{repo.path}::{nc.args.others[0]}" - var others = nc.args.others[1..^1] + let archive = 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 genCommand(cmd = "extract", repo = archive, others = others) + 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" diff --git a/norg/borg/mount.nim b/norg/borg/mount.nim index 0e9a94a..017c014 100644 --- a/norg/borg/mount.nim +++ b/norg/borg/mount.nim @@ -3,17 +3,17 @@ import execute import strformat proc mountArchive*(nc: NorgConfig, repo: Repository): int = - let archive = repo.path & "::" & nc.args.others[0] - let others = nc.args.others[1..^1] - let ok = runDiscard genCommand(cmd = "mount", repo = archive, others = others) + let archive = repo.path & "::" & nc.args.further_args[0] + let further_args = nc.args.further_args[1..^1] + let ok = runDiscard genCommand(cmd = "mount", repo = archive, further_args = further_args) if ok == 0: - echo fmt"Mounted {archive} at {others[0]}" + echo fmt"Mounted {archive} at {further_args[0]}" else: echo "Failed to mount ", archive proc unmountArchive*(nc: NorgConfig): int = - let ok = runDiscard genCommand(cmd = "umount", repo = "", others = nc.args.others) + let ok = runDiscard genCommand(cmd = "umount", repo = "", further_args = nc.args.further_args) if ok == 0: - echo "Unmounted ", nc.args.others[0] + echo "Unmounted ", nc.args.further_args[0] else: - echo "Failed to unmount ", nc.args.others[0] + echo "Failed to unmount ", nc.args.further_args[0] diff --git a/norg/borg/prune.nim b/norg/borg/prune.nim index 688fb1b..a48cb1a 100644 --- a/norg/borg/prune.nim +++ b/norg/borg/prune.nim @@ -14,6 +14,6 @@ proc addPruneOptions(cmd: var string, maintenance: Maintenance) = """ proc pruneRepo*(nc: NorgConfig, repo: Repository): int = - var cmd = genCommand(cmd = "prune", repo = repo.path, others = nc.args.others) + var cmd = genCommand(cmd = "prune", repo = repo.path, further_args = nc.args.further_args) cmd.addPruneOptions(nc.maintenance) return run cmd diff --git a/norg/config/args.nim b/norg/config/args.nim index 278644b..c957502 100644 --- a/norg/config/args.nim +++ b/norg/config/args.nim @@ -6,23 +6,26 @@ type config_file*: string extract_destination*: string command*: Command - others*: seq[string] + repository*: string + further_args*: seq[string] var norg_args*: NorgArgs = NorgArgs() proc parseArgs*() = var p = newParser: help("Norg\r\nA portable borg backup wrapper utility") - option("-c", "--config", help="Config file to use", required = true) + option("-c", "--config", help="Config file to use.", required = true) option("-d", "--destination", help="Destination when extracting backup", required = false) - arg("borg_cmd", default=some("list")) - arg("others", nargs = -1) + option("-r", "--repository", help="Define an explicit repository to work on by either label or path.", required = false) + arg("command", help="The command to run, defaults to 'create' which will perform a backup.", default=some("create")) + arg("further_args", nargs = -1, help="Any further arguments to send onto borg or restic.") try: var opts = p.parse(commandLineParams()) norg_args.config_file = opts.config norg_args.extract_destination = opts.destination - norg_args.command = opts.borg_cmd.toCommand() - norg_args.others = opts.others + 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": diff --git a/norg/config/init.nim b/norg/config/init.nim index 57192fc..fa64869 100644 --- a/norg/config/init.nim +++ b/norg/config/init.nim @@ -16,7 +16,6 @@ proc parseSourceDirectories*(in_conf: TomlValueRef): seq[string] = norg_config.source_directories.add(dir.getStr()) return src_dirs - proc parseRepositories*(rep_conf: TomlValueRef): seq[Repository] = var repos: seq[Repository] = @[] for r in rep_conf.getElems(): @@ -27,7 +26,8 @@ proc parseRepositories*(rep_conf: TomlValueRef): seq[Repository] = if rtable.hasKey("tool"): repo.tool = rtable["tool"].getStr("borg").toBackupTool() repos.add(repo) - echo repos + if norg_config.args.repository != "": + repos = repos.findRepository(norg_config.args.repository) return repos proc parseEncryption*(enc_conf: TomlValueRef) = @@ -36,7 +36,6 @@ proc parseEncryption*(enc_conf: TomlValueRef) = setEncryptionPassphraseFile(enc_conf{"encryption_passphrase_file"}.getStr("")) setEncryptionPassCommand(enc_conf{"encryption_passcommand"}.getStr("")) - proc parseConfigFile*(file: string): NorgConfig = norg_config = newNorgConfig() let in_conf = parsetoml.parseFile(file) diff --git a/norg/model/notifier_types.nim b/norg/model/notifier_types.nim index d5aa29e..2ddbada 100644 --- a/norg/model/notifier_types.nim +++ b/norg/model/notifier_types.nim @@ -1,8 +1,8 @@ import notifier_type -import uptimekuma_type +import ../notifier/uptimekuma_notifier export notifier_type -export uptimekuma_type +export uptimekuma_notifier type Notifiers* = object diff --git a/norg/model/repository_type.nim b/norg/model/repository_type.nim index cd1bfd7..0a0e279 100644 --- a/norg/model/repository_type.nim +++ b/norg/model/repository_type.nim @@ -1,3 +1,4 @@ +import sugar import tool_type export tool_type @@ -8,3 +9,9 @@ type label*: string tool*: BackupTool +proc findRepository*(repos: seq[Repository], r: string): seq[Repository] = + let repo = collect: + for item in repos: + if item.label == r or item.path == r: + item + return repo diff --git a/norg/model/uptimekuma_type.nim b/norg/notifier/uptimekuma_notifier.nim similarity index 90% rename from norg/model/uptimekuma_type.nim rename to norg/notifier/uptimekuma_notifier.nim index 296d05a..1f216f8 100644 --- a/norg/model/uptimekuma_type.nim +++ b/norg/notifier/uptimekuma_notifier.nim @@ -1,5 +1,5 @@ -import state_type -import notifier_type +import ../model/state_type +import ../model/notifier_type import strformat import ../utils/httprequest diff --git a/norg/restic/backup.nim b/norg/restic/backup.nim index 567c880..0c65658 100644 --- a/norg/restic/backup.nim +++ b/norg/restic/backup.nim @@ -10,8 +10,8 @@ import sequtils import os proc createArchive(nc: NorgConfig, repo: Repository, retry: int = 0): int = - let others = concat(nc.source_directories, nc.args.others) - let res = run genCommand(cmd = "backup", repo = repo.path, others = others) + let further_args = concat(nc.source_directories, nc.args.further_args) + let res = run genCommand(cmd = "backup", repo = repo.path, further_args = further_args) if res != 0: sleep 15 * 1000 # 15 seconds if retry == nc.retries: diff --git a/norg/restic/execute.nim b/norg/restic/execute.nim index 4fd7f00..14acc59 100644 --- a/norg/restic/execute.nim +++ b/norg/restic/execute.nim @@ -5,8 +5,8 @@ import ../model/command_type import ../utils/run export run -proc genCommand*(cmd: string, repo: string, others: seq[string]): string = - let args = others.join(" ") +proc genCommand*(cmd: string, repo: string, further_args: seq[string]): string = + let args = further_args.join(" ") let cmd = fmt"{RESTIC_BIN} {cmd} -r {repo} {args}" return cmd diff --git a/norg/restic/init.nim b/norg/restic/init.nim index 4e02824..57c7a5a 100644 --- a/norg/restic/init.nim +++ b/norg/restic/init.nim @@ -2,5 +2,5 @@ import ../model/config_type import execute proc initRepo*(nc: NorgConfig, repo: Repository): int = - return runDiscard genCommand(cmd = "init", repo = repo.path, others = nc.args.others) + return runDiscard genCommand(cmd = "init", repo = repo.path, further_args = nc.args.further_args) diff --git a/norg/restic/list.nim b/norg/restic/list.nim index 769f0e8..45f0b21 100644 --- a/norg/restic/list.nim +++ b/norg/restic/list.nim @@ -2,6 +2,6 @@ import ../model/config_type import execute proc listSnapshots*(nc: NorgConfig, repo: Repository): int = - return run genCommand(cmd = "snapshots", repo = repo.path, others = nc.args.others) + return run genCommand(cmd = "snapshots", repo = repo.path, further_args = nc.args.further_args) diff --git a/norg/restic/mount.nim b/norg/restic/mount.nim index 7257720..00d195b 100644 --- a/norg/restic/mount.nim +++ b/norg/restic/mount.nim @@ -3,17 +3,10 @@ import execute import strformat proc mountSnapshot*(nc: NorgConfig, repo: Repository): int = - let snapshot = repo.path & "::" & nc.args.others[0] - let others = nc.args.others[1..^1] - let ok = runDiscard genCommand(cmd = "mount", repo = snapshot, others = others) + #let further_args = nc.args.further_args[1..^1] + let further_args = nc.args.further_args + let ok = runDiscard genCommand(cmd = "mount", repo = repo.path, further_args = further_args) if ok == 0: - echo fmt"Mounted {snapshot} at {others[0]}" + echo fmt"Mounted {repo.path} at {further_args[0]}" else: - echo "Failed to mount ", snapshot - -proc unmountSnapshot*(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] + echo "Failed to mount ", repo.path diff --git a/norg/restic/prune.nim b/norg/restic/prune.nim index 688fb1b..a48cb1a 100644 --- a/norg/restic/prune.nim +++ b/norg/restic/prune.nim @@ -14,6 +14,6 @@ proc addPruneOptions(cmd: var string, maintenance: Maintenance) = """ proc pruneRepo*(nc: NorgConfig, repo: Repository): int = - var cmd = genCommand(cmd = "prune", repo = repo.path, others = nc.args.others) + var cmd = genCommand(cmd = "prune", repo = repo.path, further_args = nc.args.further_args) cmd.addPruneOptions(nc.maintenance) return run cmd diff --git a/norg/restic/restic.nim b/norg/restic/restic.nim index 5f3e716..7c83ddd 100644 --- a/norg/restic/restic.nim +++ b/norg/restic/restic.nim @@ -11,23 +11,27 @@ import prune proc execute*(nc: NorgConfig, repo: Repository) = case nc.args.command of INIT: + echo "Initializing repo: ", repo.label discard initRepo(nc, repo) of CREATE: run_actions(norg_config.actions.before_backup) + echo "Backing up to ", repo.label discard createBackup(nc, repo) run_actions(norg_config.actions.after_backup) of LIST: + echo "Listing Snapshots at ", repo.label discard listSnapshots(nc, repo) of MOUNT: + echo "Mounting Snapshot from ", repo.label discard mountSnapshot(nc, repo) - of UMOUNT: - discard unmountSnapshot(nc) of EXTRACT: run_actions(norg_config.actions.before_extract) + echo "Extracting backup from ", repo.label discard extractArchive(nc, repo) run_actions(norg_config.actions.after_extract) of PRUNE: run_actions(norg_config.actions.before_prune) + echo "Pruning repo: ", repo.label discard pruneRepo(nc, repo) run_actions(norg_config.actions.after_prune) else: