added restic mount, waits for Ctrl+C to unmount

This commit is contained in:
Paul Wilde 2024-08-23 12:04:27 +01:00
parent 88492d5c85
commit 02f8c0f98b
18 changed files with 63 additions and 51 deletions

View file

@ -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:

View file

@ -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:

View file

@ -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

View file

@ -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"

View file

@ -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]

View file

@ -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

View file

@ -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":

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -1,5 +1,5 @@
import state_type
import notifier_type
import ../model/state_type
import ../model/notifier_type
import strformat
import ../utils/httprequest

View file

@ -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:

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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: