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

Free predefined amount of space by removing snapshots #72

Open
Harvie opened this issue Oct 6, 2017 · 1 comment
Open

Free predefined amount of space by removing snapshots #72

Harvie opened this issue Oct 6, 2017 · 1 comment

Comments

@Harvie
Copy link

Harvie commented Oct 6, 2017

I'd rather have way to tell zfs-auto-snapshot to use as much space for snapshots as possible while leaving predefined amount of free space. Let's say that i will tell zfs-auto-snapshot to free 10GB or 10% of space, so it will delete oldest snapshots until enough space is freed and then stop. This is somewhat oportunistic approach to snapshots that will not delete more snapshots than needed just because they are old. It's kinda similar to how NILFS2 garbage collection works. Also it's vital to combine with TTL, so snapshots are protected for some time period even when disk gets full.

@ljluestc
Copy link

#!/bin/bash

filesystem="pool/dataset"  # Replace with your ZFS filesystem
desired_free_space=10  # Replace with the desired amount of free space in GB or percentage

# Calculate the current snapshot usage
snapshot_usage=$(zfs list -H -o referenced -t snapshot "$filesystem")

# Calculate the available space
available_space=$(zfs get -H -o value available "$filesystem")

# Calculate the target free space
if [[ $desired_free_space =~ % ]]; then
  desired_free_space_pct=$(echo "$desired_free_space" | tr -d '%')
  target_free_space=$(echo "scale=0; $available_space * $desired_free_space_pct / 100" | bc)
else
  target_free_space=$((desired_free_space * 1024 * 1024 * 1024))  # Convert GB to bytes
fi

# List and sort snapshots
snapshots=$(zfs list -H -o name -t snapshot -S creation "$filesystem")

# Iterate over the snapshots
total_snapshot_usage=0
deleted_snapshots=0

for snapshot in $snapshots; do
  # Calculate the space used by the current snapshot
  snapshot_usage=$(zfs list -H -o referenced "$snapshot")

  # Check if deleting the current snapshot reduces usage below the target
  if ((total_snapshot_usage + snapshot_usage > snapshot_usage)); then
    # Delete the current snapshot
    zfs destroy "$snapshot"
    ((deleted_snapshots++))

    # Update the total snapshot usage
    total_snapshot_usage=$((total_snapshot_usage + snapshot_usage))

    # Check if the target free space has been reached
    if ((total_snapshot_usage >= target_free_space)); then
      break
    fi
  fi
done

echo "Deleted $deleted_snapshots snapshots."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants