diff --git a/docs/static/downloads/norg.toml.sample b/docs/static/downloads/norg.toml.sample index ee7f8fd..32b65a3 100644 --- a/docs/static/downloads/norg.toml.sample +++ b/docs/static/downloads/norg.toml.sample @@ -88,3 +88,25 @@ restic_bin = "/usr/local/bin/restic" # Log level options are below, each option includes the logs from the one before it # "notice", "info", "warn", "error", "fatal", "debug" log_level = "info" + +# Exclude (or include) files/folders based on filenames or patterns. +[exclusions] +# Exclude files or folders matching paths +excludes = [ + "/etc/file_i_dont_want", + "/folder_i_dont_want" +] + +# Put all your exlusions in a plain text file and they can be linked to here +exclude_file = "/home/me/exclusions.txt" + +# Include or Exclude based on patterns (Borg Only) +# is a direct reference to borg-patterns: https://manpages.debian.org/testing/borgbackup/borg-patterns.1.en.html +patterns = [ + "/home/*/junk" +] + +# Or use a patterns file (Borg Only) +patterns_file = "/home/me/patterns.lst" + + diff --git a/norg.nimble b/norg.nimble index ac5d9f7..86842ed 100644 --- a/norg.nimble +++ b/norg.nimble @@ -1,6 +1,6 @@ # Package -version = "0.1.9" +version = "0.1.10" author = "Paul Wilde" description = "A Borg Backup Wrapper" license = "AGPL-3.0-or-later" diff --git a/norg/borg/create.nim b/norg/borg/create.nim index 30409d0..4ac29a0 100644 --- a/norg/borg/create.nim +++ b/norg/borg/create.nim @@ -18,7 +18,7 @@ proc genArchiveName(): string = proc createArchive(nc: NorgConfig, repo: Repository, archivename: string, retry: int = 0): int = let further_args = nc.args.further_args - let res = run genCreateCommand(repo = archivename, sources = nc.source_directories, stats=nc.args.stats, further_args = further_args) + let res = run genCreateCommand(repo = archivename, sources = nc.source_directories, stats=nc.args.stats, exc=nc.exclusions, further_args = further_args) if res != 0: info "Failed to run Restic. Waiting 15 seconds and trying again" sleep 15 * 1000 # 15 seconds diff --git a/norg/borg/execute.nim b/norg/borg/execute.nim index 7fe701e..32a4b75 100644 --- a/norg/borg/execute.nim +++ b/norg/borg/execute.nim @@ -2,6 +2,7 @@ import strutils import strformat import ../model/command_type +import ../model/exclusions_type import ../utils/run export run @@ -11,14 +12,23 @@ proc genCommand*(cmd: string, repo: string, further_args: seq[string]): string = let cmd = fmt"{BORG_BIN} {cmd} {repo} {args}" return cmd -proc genCreateCommand*(repo: string, sources: seq[string], stats: bool, further_args: seq[string]): string = +proc genCreateCommand*(repo: string, sources: seq[string], stats: bool, exc: Exclusions, further_args: seq[string]): string = let args = further_args.join(" ") var stats_flag = "--stats" if not stats: stats_flag = "" var source_dirs = "" for source in sources: source_dirs &= fmt""""{source}" """ - let cmd = fmt"{BORG_BIN} create {stats_flag} {repo} {source_dirs} {args}" + var cmd = fmt"{BORG_BIN} create {stats_flag} {repo} {source_dirs}" + if exc.exclude_file != "": + cmd = fmt"{cmd} --exclude-from {exc.exclude_file}" + if exc.patterns_file != "": + cmd = fmt"{cmd} --pattern-from {exc.patterns_file}" + for exclude in exc.excludes: + cmd = fmt"{cmd} --exclude {exclude}" + for pattern in exc.patterns: + cmd = fmt"{cmd} --pattern {pattern}" + cmd = fmt"{cmd} {args}" return cmd proc genDeleteCommand*(repo: string, archive: string, further_args: seq[string]): string = diff --git a/norg/config/init.nim b/norg/config/init.nim index 13d17c3..0c71308 100644 --- a/norg/config/init.nim +++ b/norg/config/init.nim @@ -57,9 +57,21 @@ proc addConfigBinaries*(current: var Binaries, bin_conf: TomlValueRef) = if restic != "": current.restic.insert(restic,0) +proc addExclusions*(current: var Exclusions, exc_conf: TomlValueRef) = + current.exclude_file = exc_conf{"exclude_file"}.getStr() + current.excludes = @[] + current.patterns_file = exc_conf{"patterns_file"}.getStr() + current.patterns = @[] + for e in exc_conf{"excludes"}.getElems(): + current.excludes.add(e.getStr()) + for p in exc_conf{"patterns"}.getElems(): + current.patterns.add(p.getStr()) + proc parseConfigFile*(file: string): NorgConfig = let in_conf = parsetoml.parseFile(file) norg_config.source_directories = parseSourceDirectories(in_conf) + # Add config file to backup + norg_config.source_directories.add(file) norg_config.repositories = parseRepositories(in_conf{"repositories"}) if norg_config.args.repository != "": echo "Filtering repo list to selected: ", norg_config.args.repository @@ -70,7 +82,7 @@ proc parseConfigFile*(file: string): NorgConfig = norg_config.maintenance = parseMaintenance(in_conf{"maintenance"}) norg_config.bins.addConfigBinaries(in_conf{"binaries"}) norg_config.bins.setBinaryLocations() + norg_config.exclusions.addExclusions(in_conf{"exclusions"}) norg_config.parseLogging(in_conf{"logging"}) return norg_config - diff --git a/norg/model/config_type.nim b/norg/model/config_type.nim index 689e3e7..ea32430 100644 --- a/norg/model/config_type.nim +++ b/norg/model/config_type.nim @@ -5,6 +5,7 @@ import maintenance_type import binary_type import args_type import log_type +import exclusions_type export repository_type export notifier_types @@ -13,6 +14,7 @@ export maintenance_type export binary_type export args_type export log_type +export exclusions_type import ../utils/version @@ -28,6 +30,7 @@ type notifiers*: Notifiers actions*: Actions args*: NorgArgs + exclusions*: Exclusions bins*: Binaries log_info*: LogInfo diff --git a/norg/model/exclusions_type.nim b/norg/model/exclusions_type.nim new file mode 100644 index 0000000..868d740 --- /dev/null +++ b/norg/model/exclusions_type.nim @@ -0,0 +1,7 @@ + +type + Exclusions* = object + exclude_file*: string + excludes*: seq[string] + patterns_file*: string + patterns*: seq[string] diff --git a/norg/norg.nim b/norg/norg.nim index 69c72d1..9185174 100644 --- a/norg/norg.nim +++ b/norg/norg.nim @@ -40,5 +40,7 @@ proc start() = when isMainModule: start() + echo "Always remember to check your backups!" # sleep for a bit to let logs finish sleep 5 + diff --git a/norg/restic/backup.nim b/norg/restic/backup.nim index e44ff0a..d03aa8a 100644 --- a/norg/restic/backup.nim +++ b/norg/restic/backup.nim @@ -10,7 +10,7 @@ import os proc createArchive(nc: NorgConfig, repo: Repository, retry: int = 0): int = let further_args = nc.args.further_args - let res = run genBackupCommand(repo = repo.path, sources = nc.source_directories, further_args = further_args) + let res = run genBackupCommand(repo = repo.path, sources = nc.source_directories, exc= nc.exclusions, 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 4971e28..006d793 100644 --- a/norg/restic/execute.nim +++ b/norg/restic/execute.nim @@ -3,6 +3,7 @@ import strformat import ../model/command_type import ../model/maintenance_type +import ../model/exclusions_type import ../utils/run export run @@ -11,12 +12,17 @@ proc genCommand*(cmd: string, repo: string, further_args: seq[string]): string = let cmd = fmt"{RESTIC_BIN} {cmd} -r {repo} {args}" return cmd -proc genBackupCommand*(repo: string, sources: seq[string], further_args: seq[string]): string = +proc genBackupCommand*(repo: string, sources: seq[string], exc: Exclusions, further_args: seq[string]): string = var source_dirs = "" for source in sources: source_dirs &= fmt""""{source}" """ let args = further_args.join(" ") - let cmd = fmt"{RESTIC_BIN} backup -r {repo} {source_dirs} {args}" + var cmd = fmt"{RESTIC_BIN} backup -r {repo} {source_dirs}" + if exc.exclude_file != "": + cmd = fmt"{cmd} --exclude-file={exc.exclude_file}" + for exclude in exc.excludes: + cmd = fmt"{cmd} --exclude={exclude}" + cmd = fmt"{cmd} {args}" return cmd proc genRestoreCommand*(repo_snapshot: string, destination: string, further_args: seq[string]): string =