feat(PROJ-36): archivmail recompress — Nachkomprimierung bestehender Mails

Neuer CLI-Subcommand: archivmail recompress [--dry-run]
Komprimiert alle unkomprimierten Dateien im Store atomisch (temp + rename).
Überspringt bereits komprimierte Dateien (Magic-Byte 0x01).
Aktualisiert storage_objects und emails.storage_id in der DB.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
sysops
2026-04-05 01:30:42 +02:00
parent 27d45f58e8
commit 956b5b6d5f
4 changed files with 250 additions and 0 deletions
+5
View File
@@ -284,6 +284,7 @@ Commands:
import-piler Aus mailpiler migrieren (pilerexport oder direkte Store-Methode)
export E-Mails exportieren (EML, MBOX)
reindex Index neu aufbauen (alle oder pro Mandant)
recompress Bestehende Mails nachträglich gzip-komprimieren
version Version anzeigen
help Diese Hilfe anzeigen
@@ -322,5 +323,9 @@ archivmail export [flags]
archivmail reindex [flags]
--config Pfad zur Konfigurationsdatei (Standard: /etc/archivmail/config.yml)
--tenant Mandanten-ID für partiellen Reindex (0 = alle)
archivmail recompress [flags]
--config Pfad zur Konfigurationsdatei (Standard: /etc/archivmail/config.yml)
--dry-run Simulation: zeigt wie viel gespart würde, ohne Dateien zu ändern
`, AppVersion)
}
+62
View File
@@ -0,0 +1,62 @@
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"github.com/archivmail/config"
"github.com/archivmail/internal/storage"
)
// runRecompress walks the mail store and gzip-compresses any file that is not
// yet compressed. Files are replaced atomically (write to temp, then rename).
//
// Usage: archivmail recompress [--config path] [--dry-run]
func runRecompress(args []string) {
fset := flag.NewFlagSet("recompress", flag.ExitOnError)
configPath := fset.String("config", "/etc/archivmail/config.yml", "path to config file")
dryRun := fset.Bool("dry-run", false, "simulate without writing changes")
_ = fset.Parse(args)
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
cfg, err := config.Load(*configPath)
if err != nil {
fmt.Fprintf(os.Stderr, "error: load config: %v\n", err)
os.Exit(1)
}
storeCfg := storage.Config{
Dir: cfg.Storage.StorePath,
Keyfile: cfg.Storage.Keyfile,
DSN: cfg.Database.DSN(),
CompressEnabled: true,
}
mailStore, err := storage.New(storeCfg)
if err != nil {
fmt.Fprintf(os.Stderr, "error: storage init: %v\n", err)
os.Exit(1)
}
defer mailStore.Close()
if *dryRun {
logger.Info("recompress: DRY-RUN — keine Änderungen werden gespeichert")
}
stats, err := mailStore.Recompress(context.Background(), *dryRun, logger)
if err != nil {
fmt.Fprintf(os.Stderr, "error: recompress: %v\n", err)
os.Exit(1)
}
logger.Info("recompress: abgeschlossen",
"total", stats.Total,
"compressed", stats.Compressed,
"already_compressed", stats.AlreadyCompressed,
"skipped_errors", stats.Errors,
"bytes_saved_mb", fmt.Sprintf("%.1f MB", float64(stats.BytesSaved)/1024/1024),
)
}
+3
View File
@@ -57,6 +57,9 @@ func main() {
case "reindex":
runReindex(os.Args[2:])
return
case "recompress":
runRecompress(os.Args[2:])
return
case "version":
fmt.Printf("archivmail %s\n", AppVersion)
for mod, ver := range Modules {