Example #1
0
func (j Job) clean(dataset string) error {
	mutexFor(dataset).Lock()
	defer mutexFor(dataset).Unlock()

	ds, err := zfs.GetDataset(dataset)
	if err != nil {
		return err
	}

	snaps, err := ds.Snapshots()
	if err != nil {
		return err
	}

	prefix := dataset + "@" + j.Family + "-"
	var matching []*zfs.Dataset
	for _, snap := range snaps {
		if strings.HasPrefix(snap.Name, prefix) {
			matching = append(matching, snap)
		}
	}

	sort.Sort(datasetList(matching))

	if len(matching) <= j.Keep {
		return nil
	}

	var flags zfs.DestroyFlag
	if j.Recursive {
		flags |= zfs.DestroyRecursive
	}
	for _, snap := range matching[:len(matching)-j.Keep] {
		if err := snap.Destroy(flags); err != nil {
			return err
		}
		if verbose {
			fmt.Println("Destroyed", snap.Name)
		}
	}

	return nil
}
Example #2
0
func (j Job) snapshot(dataset, name string) error {
	mutexFor(dataset).Lock()
	defer mutexFor(dataset).Unlock()

	ds, err := zfs.GetDataset(dataset)
	if err != nil {
		return err
	}

	ss, err := ds.Snapshot(name, j.Recursive)
	if err != nil {
		return err
	}

	if verbose {
		fmt.Println("Created", ss.Name)
	}

	return nil
}