restic init, list and backup working

This commit is contained in:
Paul Wilde 2024-08-23 10:30:36 +01:00
parent f142e7b1cf
commit 88492d5c85
9 changed files with 87 additions and 20 deletions

View file

@ -30,9 +30,9 @@ proc createArchive(nc: NorgConfig, repo: Repository, archivename: string, retry:
proc createBackup*(nc: NorgConfig, repo: Repository): int = proc createBackup*(nc: NorgConfig, repo: Repository): int =
let start_time = now() let start_time = now()
notify(nc.notifiers, state=Running) notify(nc.notifiers, state=Running)
let end_time = now()
let archivename = repo.path & "::" & genArchiveName() let archivename = repo.path & "::" & genArchiveName()
let res = createArchive(nc, repo, archivename) let res = createArchive(nc, repo, archivename)
let end_time = now()
let total = (end_time - start_time).inMilliSeconds() let total = (end_time - start_time).inMilliSeconds()
case res case res
of 0: of 0:

View file

@ -16,5 +16,8 @@ type
proc toCommand*(str: string): Command = proc toCommand*(str: string): Command =
for cmd in Command.items: for cmd in Command.items:
if str == $cmd: return cmd if str == $cmd: return cmd
case str
of "backup": return CREATE
of "snapshots","archives": return LIST

View file

@ -1,6 +1,37 @@
import ../model/config_type import ../model/config_type
import ../model/state_type
import ../notifier/notifier
import execute
import prune
import nativesockets
import times
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)
if res != 0:
sleep 15 * 1000 # 15 seconds
if retry == nc.retries:
return 1
else:
return createArchive(nc, repo, retry + 1)
return res
proc createBackup*(nc: NorgConfig, repo: Repository): int = proc createBackup*(nc: NorgConfig, repo: Repository): int =
echo "Not Yet Implemented" let start_time = now()
discard notify(nc.notifiers, state=Running)
let res = createArchive(nc, repo)
let end_time = now()
let total = (end_time - start_time).inMilliSeconds()
case res
of 0:
discard pruneRepo(nc, repo)
notify(nc.notifiers, state=Success, runtime=total)
of 1:
notify(nc.notifiers, state=Failure, runtime=total)
else:
notify(nc.notifiers, state=Failure, runtime=total, msg = $res)
return res

View file

@ -7,6 +7,6 @@ export run
proc genCommand*(cmd: string, repo: string, others: seq[string]): string = proc genCommand*(cmd: string, repo: string, others: seq[string]): string =
let args = others.join(" ") let args = others.join(" ")
let cmd = fmt"{RESTIC_BIN} {cmd} {repo} {args}" let cmd = fmt"{RESTIC_BIN} {cmd} -r {repo} {args}"
return cmd return cmd

View file

@ -1,6 +1,6 @@
import ../model/config_type import ../model/config_type
import execute
proc initRepo*(nc: NorgConfig, repo: Repository): int = proc initRepo*(nc: NorgConfig, repo: Repository): int =
echo "Not Yet Implemented" return runDiscard genCommand(cmd = "init", repo = repo.path, others = nc.args.others)
discard

View file

@ -1,7 +1,7 @@
import ../model/config_type import ../model/config_type
import execute
proc listArchives*(nc: NorgConfig, repo: Repository): int = proc listSnapshots*(nc: NorgConfig, repo: Repository): int =
echo "Not Yet Implemented" return run genCommand(cmd = "snapshots", repo = repo.path, others = nc.args.others)
discard

View file

@ -1,10 +1,19 @@
import ../model/config_type import ../model/config_type
import execute
import strformat
proc mountArchive*(nc: NorgConfig, repo: Repository): int = proc mountSnapshot*(nc: NorgConfig, repo: Repository): int =
echo "Not Yet Implemented" let snapshot = repo.path & "::" & nc.args.others[0]
discard let others = nc.args.others[1..^1]
let ok = runDiscard genCommand(cmd = "mount", repo = snapshot, others = others)
proc unmountArchive*(nc: NorgConfig): int = if ok == 0:
echo "Not Yet Implemented" echo fmt"Mounted {snapshot} at {others[0]}"
discard 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]

19
norg/restic/prune.nim Normal file
View file

@ -0,0 +1,19 @@
import ../model/config_type
import strformat
import execute
proc addPruneOptions(cmd: var string, maintenance: Maintenance) =
cmd = fmt"""{cmd} \
--keep-hourly {maintenance.keep_hourly} \
--keep-daily {maintenance.keep_daily} \
--keep-weekly {maintenance.keep_weekly} \
--keep-monthly {maintenance.keep_monthly} \
--keep-yearly {maintenance.keep_yearly} \
"""
proc pruneRepo*(nc: NorgConfig, repo: Repository): int =
var cmd = genCommand(cmd = "prune", repo = repo.path, others = nc.args.others)
cmd.addPruneOptions(nc.maintenance)
return run cmd

View file

@ -6,6 +6,7 @@ import backup
import list import list
import mount import mount
import extract import extract
import prune
proc execute*(nc: NorgConfig, repo: Repository) = proc execute*(nc: NorgConfig, repo: Repository) =
case nc.args.command case nc.args.command
@ -16,14 +17,18 @@ proc execute*(nc: NorgConfig, repo: Repository) =
discard createBackup(nc, repo) discard createBackup(nc, repo)
run_actions(norg_config.actions.after_backup) run_actions(norg_config.actions.after_backup)
of LIST: of LIST:
discard listArchives(nc, repo) discard listSnapshots(nc, repo)
of MOUNT: of MOUNT:
discard mountArchive(nc, repo) discard mountSnapshot(nc, repo)
of UMOUNT: of UMOUNT:
discard unmountArchive(nc) discard unmountSnapshot(nc)
of EXTRACT: of EXTRACT:
run_actions(norg_config.actions.before_extract) run_actions(norg_config.actions.before_extract)
discard extractArchive(nc, repo) discard extractArchive(nc, repo)
run_actions(norg_config.actions.after_extract) run_actions(norg_config.actions.after_extract)
of PRUNE:
run_actions(norg_config.actions.before_prune)
discard pruneRepo(nc, repo)
run_actions(norg_config.actions.after_prune)
else: else:
discard discard