Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added KeepHolds so cleaner does not try to destroy snapshots with holds #38

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func processAll(now time.Time, conf *conf.Config, zfsExecutor zfs.Executor) ([]z
}
list.KeepNamed(plan.Protect)
list.KeepLatest(plan.Latest)
err = list.KeepHolds(zfsExecutor)
if err != nil {
return nil, err
}
for _, period := range plan.Periods {
start := now.Add(-period.Age)
list.Sieve(start, period.Frequency)
Expand Down
4 changes: 4 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func (t *testExecutor) HasSnapshot(dataset string) (bool, error) {
panic("implement me")
}

func (t *testExecutor) HasHolds(dataset string) (bool, error) {
return false, nil
}

func (t *testExecutor) DestroySnapshot(dataset string) ([]byte, error) {
return nil, nil
}
Expand Down
14 changes: 14 additions & 0 deletions zfs/SnapshotList.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ func (l SnapshotList) KeepOldest(num int) {
}
}

// KeepHolds will keep all snapshots with zfs holds on it.
func (l SnapshotList) KeepHolds(zfsExecutor Executor) error {
for _, snapshot := range l {
hasHolds, err := zfsExecutor.HasHolds(snapshot.Name)
if err != nil {
return err
}
if hasHolds {
snapshot.Keep = true
}
}
return nil
}

// Sieve will mark snapshots to keep according to start time and frequency.
func (l SnapshotList) Sieve(start time.Time, frequency time.Duration) {
// The ZFS resolution on creation time is one second. If we get a frequency
Expand Down
4 changes: 4 additions & 0 deletions zfs/SnapshotList_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func (t *testExecutor) HasSnapshot(dataset string) (bool, error) {
panic("implement me")
}

func (t *testExecutor) HasHolds(dataset string) (bool, error) {
return false, nil
}

func (t *testExecutor) DestroySnapshot(dataset string) ([]byte, error) {
return nil, nil
}
Expand Down
14 changes: 14 additions & 0 deletions zfs/ZfsExecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Executor interface {
GetSnapshotList(dataset string) ([]byte, error)
GetFilesystems() ([]byte, error)
HasSnapshot(dataset string) (bool, error)
HasHolds(dataset string) (bool, error)
DestroySnapshot(dataset string) ([]byte, error)
}

Expand Down Expand Up @@ -62,6 +63,19 @@ func (z *executorImpl) HasSnapshot(dataset string) (bool, error) {
return len(output) > 0, nil
}

func (z *executorImpl) HasHolds(snapshot string) (bool, error) {
argsStr := fmt.Sprintf("holds -H %s", snapshot)
args := strings.Fields(argsStr)
output, err := exec.Command(z.zfsCommandName, args...).Output()
if exitError, ok := err.(*exec.ExitError); ok {
return false, fmt.Errorf("failed to get snapshot holds to see if the snapshots has any holds: %s error: %s", snapshot, exitError.Stderr)
}
if err != nil {
return false, err
}
return len(output) != 0, nil
}

func (z *executorImpl) DestroySnapshot(snapshot string) ([]byte, error) {
output, err := exec.Command(z.zfsCommandName, "destroy", snapshot).Output()
if exitError, ok := err.(*exec.ExitError); ok {
Expand Down
Loading