added exclusion and patterns options. Also added config file to backup

This commit is contained in:
Paul Wilde 2024-09-03 15:20:44 +01:00
parent 2585d00528
commit fd28fc2b58
10 changed files with 70 additions and 8 deletions

View file

@ -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 # Log level options are below, each option includes the logs from the one before it
# "notice", "info", "warn", "error", "fatal", "debug" # "notice", "info", "warn", "error", "fatal", "debug"
log_level = "info" 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"

View file

@ -1,6 +1,6 @@
# Package # Package
version = "0.1.9" version = "0.1.10"
author = "Paul Wilde" author = "Paul Wilde"
description = "A Borg Backup Wrapper" description = "A Borg Backup Wrapper"
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"

View file

@ -18,7 +18,7 @@ proc genArchiveName(): string =
proc createArchive(nc: NorgConfig, repo: Repository, archivename: string, retry: int = 0): int = proc createArchive(nc: NorgConfig, repo: Repository, archivename: string, retry: int = 0): int =
let further_args = nc.args.further_args 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: if res != 0:
info "Failed to run Restic. Waiting 15 seconds and trying again" info "Failed to run Restic. Waiting 15 seconds and trying again"
sleep 15 * 1000 # 15 seconds sleep 15 * 1000 # 15 seconds

View file

@ -2,6 +2,7 @@ import strutils
import strformat import strformat
import ../model/command_type import ../model/command_type
import ../model/exclusions_type
import ../utils/run import ../utils/run
export 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}" let cmd = fmt"{BORG_BIN} {cmd} {repo} {args}"
return cmd 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(" ") let args = further_args.join(" ")
var stats_flag = "--stats" var stats_flag = "--stats"
if not stats: stats_flag = "" if not stats: stats_flag = ""
var source_dirs = "" var source_dirs = ""
for source in sources: for source in sources:
source_dirs &= fmt""""{source}" """ 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 return cmd
proc genDeleteCommand*(repo: string, archive: string, further_args: seq[string]): string = proc genDeleteCommand*(repo: string, archive: string, further_args: seq[string]): string =

View file

@ -57,9 +57,21 @@ proc addConfigBinaries*(current: var Binaries, bin_conf: TomlValueRef) =
if restic != "": if restic != "":
current.restic.insert(restic,0) 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 = proc parseConfigFile*(file: string): NorgConfig =
let in_conf = parsetoml.parseFile(file) let in_conf = parsetoml.parseFile(file)
norg_config.source_directories = parseSourceDirectories(in_conf) 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"}) norg_config.repositories = parseRepositories(in_conf{"repositories"})
if norg_config.args.repository != "": if norg_config.args.repository != "":
echo "Filtering repo list to selected: ", 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.maintenance = parseMaintenance(in_conf{"maintenance"})
norg_config.bins.addConfigBinaries(in_conf{"binaries"}) norg_config.bins.addConfigBinaries(in_conf{"binaries"})
norg_config.bins.setBinaryLocations() norg_config.bins.setBinaryLocations()
norg_config.exclusions.addExclusions(in_conf{"exclusions"})
norg_config.parseLogging(in_conf{"logging"}) norg_config.parseLogging(in_conf{"logging"})
return norg_config return norg_config

View file

@ -5,6 +5,7 @@ import maintenance_type
import binary_type import binary_type
import args_type import args_type
import log_type import log_type
import exclusions_type
export repository_type export repository_type
export notifier_types export notifier_types
@ -13,6 +14,7 @@ export maintenance_type
export binary_type export binary_type
export args_type export args_type
export log_type export log_type
export exclusions_type
import ../utils/version import ../utils/version
@ -28,6 +30,7 @@ type
notifiers*: Notifiers notifiers*: Notifiers
actions*: Actions actions*: Actions
args*: NorgArgs args*: NorgArgs
exclusions*: Exclusions
bins*: Binaries bins*: Binaries
log_info*: LogInfo log_info*: LogInfo

View file

@ -0,0 +1,7 @@
type
Exclusions* = object
exclude_file*: string
excludes*: seq[string]
patterns_file*: string
patterns*: seq[string]

View file

@ -40,5 +40,7 @@ proc start() =
when isMainModule: when isMainModule:
start() start()
echo "Always remember to check your backups!"
# sleep for a bit to let logs finish # sleep for a bit to let logs finish
sleep 5 sleep 5

View file

@ -10,7 +10,7 @@ import os
proc createArchive(nc: NorgConfig, repo: Repository, retry: int = 0): int = proc createArchive(nc: NorgConfig, repo: Repository, retry: int = 0): int =
let further_args = nc.args.further_args 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: if res != 0:
sleep 15 * 1000 # 15 seconds sleep 15 * 1000 # 15 seconds
if retry == nc.retries: if retry == nc.retries:

View file

@ -3,6 +3,7 @@ import strformat
import ../model/command_type import ../model/command_type
import ../model/maintenance_type import ../model/maintenance_type
import ../model/exclusions_type
import ../utils/run import ../utils/run
export 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}" let cmd = fmt"{RESTIC_BIN} {cmd} -r {repo} {args}"
return cmd 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 = "" var source_dirs = ""
for source in sources: for source in sources:
source_dirs &= fmt""""{source}" """ source_dirs &= fmt""""{source}" """
let args = further_args.join(" ") 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 return cmd
proc genRestoreCommand*(repo_snapshot: string, destination: string, further_args: seq[string]): string = proc genRestoreCommand*(repo_snapshot: string, destination: string, further_args: seq[string]): string =