From 88492d5c8578a5888a27eddc96050ba8dfd605c6 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Fri, 23 Aug 2024 10:30:36 +0100 Subject: [PATCH] restic init, list and backup working --- norg/borg/create.nim | 2 +- norg/model/command_type.nim | 3 +++ norg/restic/backup.nim | 37 ++++++++++++++++++++++++++++++++++--- norg/restic/execute.nim | 2 +- norg/restic/init.nim | 4 ++-- norg/restic/list.nim | 6 +++--- norg/restic/mount.nim | 23 ++++++++++++++++------- norg/restic/prune.nim | 19 +++++++++++++++++++ norg/restic/restic.nim | 11 ++++++++--- 9 files changed, 87 insertions(+), 20 deletions(-) create mode 100644 norg/restic/prune.nim diff --git a/norg/borg/create.nim b/norg/borg/create.nim index 9f353a6..cfb0dbc 100644 --- a/norg/borg/create.nim +++ b/norg/borg/create.nim @@ -30,9 +30,9 @@ proc createArchive(nc: NorgConfig, repo: Repository, archivename: string, retry: proc createBackup*(nc: NorgConfig, repo: Repository): int = let start_time = now() notify(nc.notifiers, state=Running) - let end_time = now() let archivename = repo.path & "::" & genArchiveName() let res = createArchive(nc, repo, archivename) + let end_time = now() let total = (end_time - start_time).inMilliSeconds() case res of 0: diff --git a/norg/model/command_type.nim b/norg/model/command_type.nim index 82e7ae7..21087f7 100644 --- a/norg/model/command_type.nim +++ b/norg/model/command_type.nim @@ -16,5 +16,8 @@ type proc toCommand*(str: string): Command = for cmd in Command.items: if str == $cmd: return cmd + case str + of "backup": return CREATE + of "snapshots","archives": return LIST diff --git a/norg/restic/backup.nim b/norg/restic/backup.nim index 359731f..567c880 100644 --- a/norg/restic/backup.nim +++ b/norg/restic/backup.nim @@ -1,6 +1,37 @@ 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 = - echo "Not Yet Implemented" - discard - + let start_time = now() + 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 diff --git a/norg/restic/execute.nim b/norg/restic/execute.nim index ad2deaf..4fd7f00 100644 --- a/norg/restic/execute.nim +++ b/norg/restic/execute.nim @@ -7,6 +7,6 @@ export run proc genCommand*(cmd: string, repo: string, others: seq[string]): string = let args = others.join(" ") - let cmd = fmt"{RESTIC_BIN} {cmd} {repo} {args}" + let cmd = fmt"{RESTIC_BIN} {cmd} -r {repo} {args}" return cmd diff --git a/norg/restic/init.nim b/norg/restic/init.nim index 47b09d2..4e02824 100644 --- a/norg/restic/init.nim +++ b/norg/restic/init.nim @@ -1,6 +1,6 @@ import ../model/config_type +import execute proc initRepo*(nc: NorgConfig, repo: Repository): int = - echo "Not Yet Implemented" - discard + return runDiscard genCommand(cmd = "init", repo = repo.path, others = nc.args.others) diff --git a/norg/restic/list.nim b/norg/restic/list.nim index fa9e89b..769f0e8 100644 --- a/norg/restic/list.nim +++ b/norg/restic/list.nim @@ -1,7 +1,7 @@ import ../model/config_type +import execute -proc listArchives*(nc: NorgConfig, repo: Repository): int = - echo "Not Yet Implemented" - discard +proc listSnapshots*(nc: NorgConfig, repo: Repository): int = + return run genCommand(cmd = "snapshots", repo = repo.path, others = nc.args.others) diff --git a/norg/restic/mount.nim b/norg/restic/mount.nim index 2398781..7257720 100644 --- a/norg/restic/mount.nim +++ b/norg/restic/mount.nim @@ -1,10 +1,19 @@ import ../model/config_type +import execute +import strformat -proc mountArchive*(nc: NorgConfig, repo: Repository): int = - echo "Not Yet Implemented" - discard - -proc unmountArchive*(nc: NorgConfig): int = - echo "Not Yet Implemented" - discard +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) + if ok == 0: + echo fmt"Mounted {snapshot} at {others[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] diff --git a/norg/restic/prune.nim b/norg/restic/prune.nim new file mode 100644 index 0000000..688fb1b --- /dev/null +++ b/norg/restic/prune.nim @@ -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 diff --git a/norg/restic/restic.nim b/norg/restic/restic.nim index 617ea24..5f3e716 100644 --- a/norg/restic/restic.nim +++ b/norg/restic/restic.nim @@ -6,6 +6,7 @@ import backup import list import mount import extract +import prune proc execute*(nc: NorgConfig, repo: Repository) = case nc.args.command @@ -16,14 +17,18 @@ proc execute*(nc: NorgConfig, repo: Repository) = discard createBackup(nc, repo) run_actions(norg_config.actions.after_backup) of LIST: - discard listArchives(nc, repo) + discard listSnapshots(nc, repo) of MOUNT: - discard mountArchive(nc, repo) + discard mountSnapshot(nc, repo) of UMOUNT: - discard unmountArchive(nc) + discard unmountSnapshot(nc) of EXTRACT: run_actions(norg_config.actions.before_extract) discard extractArchive(nc, repo) 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: discard