| 1 | // Package config leest de configuratie van de backup server. |
| 2 | package config |
| 3 | |
| 4 | import ( |
| 5 | "fmt" |
| 6 | "net/url" |
| 7 | "os" |
| 8 | "strings" |
| 9 | "time" |
| 10 | |
| 11 | "gitlab.com/Timfa/bluegit/internal/apppaths" |
| 12 | "gopkg.in/yaml.v3" |
| 13 | ) |
| 14 | |
| 15 | // DefaultDataDir is de standaard gegevensmap. |
| 16 | func DefaultDataDir() string { |
| 17 | return apppaths.DataDir("/var/lib/bluegit-backup", "BlueGit-backup") |
| 18 | } |
| 19 | |
| 20 | // DefaultConfigPath is waar een oude config.yml nog kan staan. |
| 21 | func DefaultConfigPath() string { |
| 22 | return apppaths.ConfigFile("", "BLUEGIT_BACKUP_CONFIG", "bluegit-backup.yml", "/etc/bluegit-backup/config.yml", "BlueGit-backup") |
| 23 | } |
| 24 | |
| 25 | // Config is de draaiende configuratie van de backup server. |
| 26 | // Instellingen staan in data_dir/settings.yml; een oude config.yml wordt |
| 27 | // één keer overgenomen. |
| 28 | type Config struct { |
| 29 | // Listen is waar de backup server luistert (plain HTTP; TLS doet een |
| 30 | // proxy ervoor als dat nodig is). |
| 31 | Listen string `yaml:"listen"` |
| 32 | // PublicURL is het adres waarop BlueGit deze server bereikt, voor |
| 33 | // meldingen, de archieflijst en rollback. |
| 34 | PublicURL string `yaml:"public_url"` |
| 35 | // DataDir bevat de draaiende kopie, de archieven en de koppeling. |
| 36 | DataDir string `yaml:"data_dir"` |
| 37 | // ArchiveInterval: zo vaak wordt de hele kopie gecomprimeerd en |
| 38 | // opgeslagen. Standaard 24h (dagelijks). |
| 39 | ArchiveInterval Duration `yaml:"archive_interval"` |
| 40 | // RetentionDays: archieven ouder dan dit worden verwijderd. 0 = nooit |
| 41 | // op leeftijd. Het nieuwste archief blijft altijd staan. |
| 42 | RetentionDays int `yaml:"retention_days"` |
| 43 | // MinFreeMB: zakt de vrije ruimte op DataDir hieronder, dan gaat het |
| 44 | // oudste archief weg (het nieuwste blijft altijd staan). |
| 45 | MinFreeMB int64 `yaml:"min_free_mb"` |
| 46 | // RateLimitKiB begrenst het downloaden van BlueGit, in KiB/s. |
| 47 | // Standaard 1024. 0 = geen limiet. |
| 48 | RateLimitKiB int64 `yaml:"rate_limit_kib"` |
| 49 | // FullSyncInterval: ook zonder melding wordt zo vaak gesynchroniseerd. |
| 50 | FullSyncInterval Duration `yaml:"full_sync_interval"` |
| 51 | // MinSyncInterval: na een melding wacht de server minstens zo lang |
| 52 | // sinds de vorige sync, zodat een drukke BlueGit niet elke seconde een |
| 53 | // databasekopie hoeft te maken. |
| 54 | MinSyncInterval Duration `yaml:"min_sync_interval"` |
| 55 | } |
| 56 | |
| 57 | // Duration leest "24h", "30m" enzovoort. |
| 58 | type Duration struct{ time.Duration } |
| 59 | |
| 60 | // UnmarshalYAML leest een Go-duur of een aantal seconden. |
| 61 | func (d *Duration) UnmarshalYAML(n *yaml.Node) error { |
| 62 | s := strings.TrimSpace(n.Value) |
| 63 | if s == "" { |
| 64 | return nil |
| 65 | } |
| 66 | if strings.HasSuffix(s, "d") { |
| 67 | var days int |
| 68 | if _, err := fmt.Sscanf(s, "%dd", &days); err == nil { |
| 69 | d.Duration = time.Duration(days) * 24 * time.Hour |
| 70 | return nil |
| 71 | } |
| 72 | } |
| 73 | v, err := time.ParseDuration(s) |
| 74 | if err != nil { |
| 75 | return fmt.Errorf("ongeldige duur %q", s) |
| 76 | } |
| 77 | d.Duration = v |
| 78 | return nil |
| 79 | } |
| 80 | |
| 81 | // MarshalYAML schrijft de duur terug als tekst. |
| 82 | func (d Duration) MarshalYAML() (any, error) { return d.String(), nil } |
| 83 | |
| 84 | // Defaults vult lege velden. |
| 85 | func (c *Config) Defaults() { |
| 86 | if c.Listen == "" { |
| 87 | c.Listen = "0.0.0.0:8080" |
| 88 | } |
| 89 | if c.DataDir == "" { |
| 90 | c.DataDir = DefaultDataDir() |
| 91 | } |
| 92 | if c.ArchiveInterval.Duration == 0 { |
| 93 | c.ArchiveInterval.Duration = 24 * time.Hour |
| 94 | } |
| 95 | if c.RetentionDays == 0 { |
| 96 | c.RetentionDays = 30 |
| 97 | } |
| 98 | if c.RetentionDays < 0 { |
| 99 | c.RetentionDays = 0 |
| 100 | } |
| 101 | if c.MinFreeMB == 0 { |
| 102 | c.MinFreeMB = 1024 |
| 103 | } |
| 104 | if c.RateLimitKiB == 0 { |
| 105 | c.RateLimitKiB = 1024 |
| 106 | } |
| 107 | if c.RateLimitKiB < 0 { |
| 108 | c.RateLimitKiB = 0 |
| 109 | } |
| 110 | if c.FullSyncInterval.Duration == 0 { |
| 111 | c.FullSyncInterval.Duration = time.Hour |
| 112 | } |
| 113 | if c.MinSyncInterval.Duration == 0 { |
| 114 | c.MinSyncInterval.Duration = 10 * time.Second |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Validate controleert de combinatie. |
| 119 | func (c Config) Validate() error { |
| 120 | if c.ArchiveInterval.Duration < time.Second { |
| 121 | return fmt.Errorf("archive_interval moet minstens 1s zijn") |
| 122 | } |
| 123 | if c.FullSyncInterval.Duration < time.Second || c.MinSyncInterval.Duration < 0 { |
| 124 | return fmt.Errorf("full_sync_interval moet minstens 1s zijn") |
| 125 | } |
| 126 | if _, err := ParsePublicURL(c.PublicURL); err != nil { |
| 127 | return err |
| 128 | } |
| 129 | return nil |
| 130 | } |
| 131 | |
| 132 | // ParsePublicURL controleert een publiek adres. Leeg is toegestaan (tot |
| 133 | // er gekoppeld wordt). Een domein of een IP-adres mag; IPv6 tussen hakjes. |
| 134 | func ParsePublicURL(s string) (string, error) { |
| 135 | s = strings.TrimRight(strings.TrimSpace(s), "/") |
| 136 | if s == "" { |
| 137 | return "", nil |
| 138 | } |
| 139 | u, err := url.Parse(s) |
| 140 | if err != nil || u.Host == "" || (u.Scheme != "http" && u.Scheme != "https") { |
| 141 | return "", fmt.Errorf("public_url moet een http(s)-adres zijn, bijvoorbeeld http://backup.lan:8080 of http://192.168.1.50:8080") |
| 142 | } |
| 143 | if u.User != nil { |
| 144 | return "", fmt.Errorf("public_url mag geen gebruikersnaam of wachtwoord bevatten") |
| 145 | } |
| 146 | return s, nil |
| 147 | } |
| 148 | |
| 149 | // Load leest path. Een ontbrekend bestand geeft de standaardwaarden. |
| 150 | func Load(path string) (Config, error) { |
| 151 | var c Config |
| 152 | b, err := os.ReadFile(path) |
| 153 | if err != nil && !os.IsNotExist(err) { |
| 154 | return c, err |
| 155 | } |
| 156 | if err == nil { |
| 157 | if err := yaml.Unmarshal(b, &c); err != nil { |
| 158 | return c, fmt.Errorf("%s: %w", path, err) |
| 159 | } |
| 160 | } |
| 161 | c.Defaults() |
| 162 | return c, c.Validate() |
| 163 | } |
| 164 | |
| 165 | // Example is een voorbeeld van het oude config.yml-formaat. Dat bestand is |
| 166 | // niet meer nodig; een bestaand exemplaar wordt één keer overgenomen. |
| 167 | func Example() string { |
| 168 | return `# Dit bestand is niet meer nodig om bluegit-backup te draaien. |
| 169 | # Start met --data-dir of BLUEGIT_BACKUP_DATA_DIR (standaard |
| 170 | # /var/lib/bluegit-backup) en regel de rest in de webinterface |
| 171 | # (http://<server>:8080, Instellingen) of via de API. |
| 172 | # Een bestaand bestand op dit pad wordt één keer overgenomen in |
| 173 | # data_dir/settings.yml; daarna wint de webinterface. |
| 174 | listen: 0.0.0.0:8080 |
| 175 | # Waar BlueGit deze server bereikt (domein of IP-adres). |
| 176 | public_url: http://192.168.1.50:8080 |
| 177 | data_dir: /var/lib/bluegit-backup |
| 178 | archive_interval: 24h |
| 179 | retention_days: 30 |
| 180 | min_free_mb: 1024 |
| 181 | rate_limit_kib: 1024 |
| 182 | full_sync_interval: 1h |
| 183 | min_sync_interval: 10s |
| 184 | ` |
| 185 | } |