Skip to content

Commit

Permalink
Run copying and pruning against multiple storages in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Sep 1, 2022
1 parent 2316111 commit 908a563
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
32 changes: 24 additions & 8 deletions cmd/backup/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/otiai10/copy"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/openpgp"
"golang.org/x/sync/errgroup"
)

// script holds all the stateful information required to orchestrate a
Expand Down Expand Up @@ -493,10 +494,15 @@ func (s *script) copyArchive() error {
}
}

g := errgroup.Group{}
for _, backend := range s.storages {
if err := backend.Copy(s.file); err != nil {
return err
}
b := backend
g.Go(func() error {
return b.Copy(s.file)
})
}
if err := g.Wait(); err != nil {
return fmt.Errorf("copyArchive: error copyin archive: %w", err)
}

return nil
Expand All @@ -512,16 +518,26 @@ func (s *script) pruneBackups() error {

deadline := time.Now().AddDate(0, 0, -int(s.c.BackupRetentionDays)).Add(s.c.BackupPruningLeeway)

g := errgroup.Group{}
for _, backend := range s.storages {
if stats, err := backend.Prune(deadline, s.c.BackupPruningPrefix); err == nil {
s.stats.Storages[backend.Name()] = StorageStats{
b := backend
g.Go(func() error {
stats, err := b.Prune(deadline, s.c.BackupPruningPrefix)
if err != nil {
return err
}
s.stats.Lock()
s.stats.Storages[b.Name()] = StorageStats{
Total: stats.Total,
Pruned: stats.Pruned,
}
s.stats.Unlock()
return nil
})
}

} else {
return err
}
if err := g.Wait(); err != nil {
return fmt.Errorf("pruneBackups: error pruning backups: %w", err)
}

return nil
Expand Down
2 changes: 2 additions & 0 deletions cmd/backup/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main

import (
"bytes"
"sync"
"time"
)

Expand Down Expand Up @@ -32,6 +33,7 @@ type StorageStats struct {

// Stats global stats regarding script execution
type Stats struct {
sync.Mutex
StartTime time.Time
EndTime time.Time
TookTime time.Duration
Expand Down

0 comments on commit 908a563

Please sign in to comment.