BlueGit

timfa bluegit

Files

bluegit / internal / backupserver / config / config.go

config.go

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