-
Notifications
You must be signed in to change notification settings - Fork 3
/
cleanup.go
66 lines (54 loc) · 1.83 KB
/
cleanup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package backup
import (
"context"
"github.com/kolesa-team/scylla-octopus/pkg/cmd"
"github.com/kolesa-team/scylla-octopus/pkg/entity"
"time"
)
// Cleanup removes temporary files on database node, as well as expired backups from this node in remote storage.
func (s *Service) Cleanup(ctx context.Context, node *entity.Node, snapshotTag string) entity.CleanupResult {
result := entity.CleanupResult{
RemovedRemoteBackups: []entity.RemoteBackup{},
}
if s.options.CleanupLocal {
result.LocalError = s.cleanupLocal(ctx, node, snapshotTag)
}
if s.options.CleanupRemote {
result.RemovedRemoteBackups, result.RemoteError = s.CleanupExpiredBackups(ctx, node, time.Now())
}
return result
}
// CleanupExpiredBackups removes expired backups from a node in remote storage.
func (s *Service) CleanupExpiredBackups(
ctx context.Context,
node *entity.Node,
now time.Time,
) ([]entity.RemoteBackup, error) {
expiredBackups, err := s.ListExpiredBackups(ctx, node, now)
if err != nil {
return []entity.RemoteBackup{}, err
}
for i, backup := range expiredBackups {
backup.RemoveError = s.remoteStorage.RemoveBackup(ctx, node.Cmd, backup.Path)
if backup.RemoveError == nil {
backup.Removed = true
}
expiredBackups[i] = backup
}
return expiredBackups, err
}
// removes given snapshot and temporary files on database node
func (s *Service) cleanupLocal(ctx context.Context, node *entity.Node, snapshotTag string) error {
logCtx := s.logger.With("host", node.Info.Host)
err := s.scylla.RemoveSnapshot(ctx, node, snapshotTag)
if err == nil {
logCtx.Infow("database snapshot removed", "tag", snapshotTag)
}
err = cmd.EnsureDirectoryIsEmpty(ctx, node.Cmd, s.options.LocalPath)
if err != nil {
logCtx.Errorw("could not remove local backup directory", "error", err)
} else {
logCtx.Info("local backup directory removed")
}
return err
}