Release URBM 2026.06.15.r008

This commit is contained in:
Mikei386
2026-06-15 07:17:37 +02:00
parent b0fa11a40a
commit 5e68a50373
13 changed files with 143 additions and 14 deletions
+7
View File
@@ -47,6 +47,7 @@ func New(socket string, svc *service.Service, log *slog.Logger) *Server {
mux.HandleFunc("POST /v1/repositories/{id}/init", s.initRepository)
mux.HandleFunc("POST /v1/repositories/{id}/unlock", s.unlockRepository)
mux.HandleFunc("POST /v1/repositories/{id}/{action}", s.maintenance)
mux.HandleFunc("GET /v1/repositories/stats", s.repositoryStats)
mux.HandleFunc("GET /v1/repositories/{id}/snapshots", s.snapshots)
mux.HandleFunc("GET /v1/repositories/{id}/snapshots/{snapshot}/files", s.snapshotFiles)
mux.HandleFunc("POST /v1/restores", s.restore)
@@ -220,6 +221,12 @@ func (s *Server) snapshots(w http.ResponseWriter, r *http.Request) {
}
writeJSON(w, 200, items)
}
func (s *Server) repositoryStats(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Minute)
defer cancel()
writeJSON(w, 200, s.service.RepositoryStats(ctx))
}
func (s *Server) snapshotFiles(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Minute)
defer cancel()
+9
View File
@@ -162,6 +162,15 @@ type Snapshot struct {
Tags []string `json:"tags"`
}
type RepositoryStats struct {
RepositoryID string `json:"repositoryId"`
RepositoryName string `json:"repositoryName"`
StoredBytes int64 `json:"storedBytes"`
FileCount int64 `json:"fileCount"`
SnapshotCount int `json:"snapshotCount"`
Error string `json:"error,omitempty"`
}
func DefaultConfig() Config {
return Config{
SchemaVersion: SchemaVersion,
+22
View File
@@ -91,6 +91,12 @@ type Progress struct {
type ProgressCallback func(Progress)
type Stats struct {
TotalSize int64 `json:"total_size"`
TotalFileCount int64 `json:"total_file_count"`
TotalBlobCount int64 `json:"total_blob_count"`
}
func (r *Runner) Init(ctx context.Context, repo model.Repository) error {
return r.run(ctx, repo, []string{"init"}, nil, nil)
}
@@ -191,6 +197,22 @@ func (r *Runner) Snapshots(ctx context.Context, repo model.Repository) ([]model.
return snapshots, nil
}
func (r *Runner) Stats(ctx context.Context, repo model.Repository, mode string) (Stats, error) {
args := []string{"stats", "--json"}
if mode != "" {
args = append(args, "--mode", mode)
}
var output []byte
if err := r.run(ctx, repo, args, nil, &output); err != nil {
return Stats{}, err
}
var stats Stats
if err := json.Unmarshal(output, &stats); err != nil {
return Stats{}, fmt.Errorf("decode repository stats: %w", err)
}
return stats, nil
}
func (r *Runner) List(ctx context.Context, repo model.Repository, snapshot, path string) ([]map[string]any, error) {
args := []string{"ls", snapshot, "--json"}
if path != "" {
+25
View File
@@ -82,6 +82,31 @@ func TestBackupImageStreamsRawDeviceWithStableFilename(t *testing.T) {
}
}
func TestStatsUsesRequestedRepositoryCountingMode(t *testing.T) {
dir := t.TempDir()
argsPath := filepath.Join(dir, "args")
script := filepath.Join(dir, "restic")
body := fmt.Sprintf("#!/bin/sh\nprintf '%%s\\n' \"$@\" > '%s'\nprintf '%%s\\n' '{\"total_size\":4096,\"total_file_count\":12,\"total_blob_count\":8}'\n", argsPath)
if err := os.WriteFile(script, []byte(body), 0700); err != nil {
t.Fatal(err)
}
runner := &Runner{Binary: script, RuntimeDir: dir, Secrets: fakeSecrets{"password": "secret"}}
repo := model.Repository{Type: model.RepositoryLocal, Location: "/repo", PasswordRef: "password"}
stats, err := runner.Stats(context.Background(), repo, "raw-data")
if err != nil {
t.Fatal(err)
}
if stats.TotalSize != 4096 || stats.TotalFileCount != 12 || stats.TotalBlobCount != 8 {
t.Fatalf("unexpected stats: %+v", stats)
}
args, _ := os.ReadFile(argsPath)
for _, expected := range []string{"stats", "--json", "--mode", "raw-data"} {
if !strings.Contains(string(args), expected) {
t.Fatalf("arguments missing %q: %s", expected, args)
}
}
}
func TestForgetUsesAgeAndCalendarRetention(t *testing.T) {
dir := t.TempDir()
argsPath := filepath.Join(dir, "args")
+34
View File
@@ -202,6 +202,40 @@ func (s *Service) Snapshots(ctx context.Context, repoID string) ([]model.Snapsho
return s.restic.Snapshots(ctx, mounted.Repository)
}
func (s *Service) RepositoryStats(ctx context.Context) []model.RepositoryStats {
config := s.Config()
result := make([]model.RepositoryStats, 0, len(config.Repositories))
for _, repo := range config.Repositories {
item := model.RepositoryStats{RepositoryID: repo.ID, RepositoryName: repo.Name}
mounted, err := s.mounts.Prepare(ctx, repo)
if err == nil {
var stored, files restic.Stats
stored, err = s.restic.Stats(ctx, mounted.Repository, "raw-data")
if err == nil {
files, err = s.restic.Stats(ctx, mounted.Repository, "restore-size")
}
if err == nil {
var snapshots []model.Snapshot
snapshots, err = s.restic.Snapshots(ctx, mounted.Repository)
item.StoredBytes = stored.TotalSize
item.FileCount = files.TotalFileCount
item.SnapshotCount = len(snapshots)
}
if cleanupErr := s.mounts.Cleanup(context.Background(), mounted); err == nil && cleanupErr != nil {
err = cleanupErr
}
}
if err != nil {
item.Error = err.Error()
}
result = append(result, item)
if ctx.Err() != nil {
break
}
}
return result
}
func (s *Service) SnapshotFiles(ctx context.Context, repoID, snapshot, path string) ([]map[string]any, error) {
repo, ok := s.repository(repoID)
if !ok {