BlueGit

timfa bluegit

Files

bluegit / internal / backupserver / config / config.go

config.go

config.go 4.6 KiB Download Raw
1// Package config leest de configuratie van de backup server.
2package config
3
4import (
5 "fmt"
6 "os"
7 "strings"
8 "time"
9
10 "gopkg.in/yaml.v3"
11)
12
13// Config is /etc/bluegit-backup/config.yml.
14type Config struct {
15 // Listen is waar de backup server luistert (plain HTTP; TLS doet een
16 // proxy ervoor als dat nodig is).
17 Listen string `yaml:"listen"`
18 // PublicURL is het adres waarop BlueGit deze server bereikt, voor
19 // meldingen, de archieflijst en rollback.
20 PublicURL string `yaml:"public_url"`
21 // DataDir bevat de draaiende kopie, de archieven en de koppeling.
22 DataDir string `yaml:"data_dir"`
23 // ArchiveInterval: zo vaak wordt de hele kopie gecomprimeerd en
24 // opgeslagen. Standaard 24h (dagelijks).
25 ArchiveInterval Duration `yaml:"archive_interval"`
26 // RetentionDays: archieven ouder dan dit worden verwijderd. 0 = nooit
27 // op leeftijd. Het nieuwste archief blijft altijd staan.
28 RetentionDays int `yaml:"retention_days"`
29 // MinFreeMB: zakt de vrije ruimte op DataDir hieronder, dan gaat het
30 // oudste archief weg (het nieuwste blijft altijd staan).
31 MinFreeMB int64 `yaml:"min_free_mb"`
32 // RateLimitKiB begrenst het downloaden van BlueGit, in KiB/s.
33 // Standaard 1024. 0 = geen limiet.
34 RateLimitKiB int64 `yaml:"rate_limit_kib"`
35 // FullSyncInterval: ook zonder melding wordt zo vaak gesynchroniseerd.
36 FullSyncInterval Duration `yaml:"full_sync_interval"`
37 // MinSyncInterval: na een melding wacht de server minstens zo lang
38 // sinds de vorige sync, zodat een drukke BlueGit niet elke seconde een
39 // databasekopie hoeft te maken.
40 MinSyncInterval Duration `yaml:"min_sync_interval"`
41}
42
43// Duration leest "24h", "30m" enzovoort.
44type Duration struct{ time.Duration }
45
46// UnmarshalYAML leest een Go-duur of een aantal seconden.
47func (d *Duration) UnmarshalYAML(n *yaml.Node) error {
48 s := strings.TrimSpace(n.Value)
49 if s == "" {
50 return nil
51 }
52 if strings.HasSuffix(s, "d") {
53 var days int
54 if _, err := fmt.Sscanf(s, "%dd", &days); err == nil {
55 d.Duration = time.Duration(days) * 24 * time.Hour
56 return nil
57 }
58 }
59 v, err := time.ParseDuration(s)
60 if err != nil {
61 return fmt.Errorf("ongeldige duur %q", s)
62 }
63 d.Duration = v
64 return nil
65}
66
67// MarshalYAML schrijft de duur terug als tekst.
68func (d Duration) MarshalYAML() (any, error) { return d.String(), nil }
69
70// Defaults vult lege velden.
71func (c *Config) Defaults() {
72 if c.Listen == "" {
73 c.Listen = "0.0.0.0:8080"
74 }
75 if c.DataDir == "" {
76 c.DataDir = "/var/lib/bluegit-backup"
77 }
78 if c.ArchiveInterval.Duration == 0 {
79 c.ArchiveInterval.Duration = 24 * time.Hour
80 }
81 if c.RetentionDays == 0 {
82 c.RetentionDays = 30
83 }
84 if c.RetentionDays < 0 {
85 c.RetentionDays = 0
86 }
87 if c.MinFreeMB == 0 {
88 c.MinFreeMB = 1024
89 }
90 if c.RateLimitKiB == 0 {
91 c.RateLimitKiB = 1024
92 }
93 if c.RateLimitKiB < 0 {
94 c.RateLimitKiB = 0
95 }
96 if c.FullSyncInterval.Duration == 0 {
97 c.FullSyncInterval.Duration = time.Hour
98 }
99 if c.MinSyncInterval.Duration == 0 {
100 c.MinSyncInterval.Duration = 10 * time.Second
101 }
102}
103
104// Validate controleert de combinatie.
105func (c Config) Validate() error {
106 if c.ArchiveInterval.Duration < time.Second {
107 return fmt.Errorf("archive_interval moet minstens 1s zijn")
108 }
109 if c.FullSyncInterval.Duration < time.Second || c.MinSyncInterval.Duration < 0 {
110 return fmt.Errorf("full_sync_interval moet minstens 1s zijn")
111 }
112 if c.PublicURL != "" && !strings.HasPrefix(c.PublicURL, "http://") && !strings.HasPrefix(c.PublicURL, "https://") {
113 return fmt.Errorf("public_url moet met http:// of https:// beginnen")
114 }
115 return nil
116}
117
118// Load leest path. Een ontbrekend bestand geeft de standaardwaarden.
119func Load(path string) (Config, error) {
120 var c Config
121 b, err := os.ReadFile(path)
122 if err != nil && !os.IsNotExist(err) {
123 return c, err
124 }
125 if err == nil {
126 if err := yaml.Unmarshal(b, &c); err != nil {
127 return c, fmt.Errorf("%s: %w", path, err)
128 }
129 }
130 c.Defaults()
131 return c, c.Validate()
132}
133
134// Example is een voorbeeldconfiguratie.
135func Example() string {
136 return `# bluegit-backup. Kopieer naar /etc/bluegit-backup/config.yml.
137listen: 0.0.0.0:8080
138# Waar BlueGit deze server bereikt (meldingen, archieflijst, rollback).
139public_url: http://backup.lan:8080
140data_dir: /var/lib/bluegit-backup
141# Zo vaak wordt de hele kopie gecomprimeerd opgeslagen. Standaard dagelijks.
142archive_interval: 24h
143# Archieven ouder dan dit gaan weg (0 = nooit op leeftijd). Het nieuwste blijft.
144retention_days: 30
145# Minder vrije ruimte dan dit: het oudste archief gaat weg.
146min_free_mb: 1024
147# Downloadsnelheid van BlueGit in KiB/s, zodat BlueGit er geen last van heeft.
148rate_limit_kib: 1024
149# Ook zonder melding zo vaak alles nalopen.
150full_sync_interval: 1h
151# Minimale tijd tussen twee syncs na meldingen.
152min_sync_interval: 10s
153`
154}