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

Enhancement to delete_snapshots #1

Closed
sigxcpu76 opened this issue Dec 5, 2015 · 2 comments
Closed

Enhancement to delete_snapshots #1

sigxcpu76 opened this issue Dec 5, 2015 · 2 comments

Comments

@sigxcpu76
Copy link

This will not work on SunOS platforms, so I've changed some bits:

  • the timestamps used in snapshots list are UNIX timestamps now (-p parameter) for easy comparison
  • the absolute oldest date computation is added for SunOS platform
  • the "from " is not outputted anymore, because it is not in the snapshots list
--- delete_snapshots.sh Sat Dec  5 10:03:02 2015
+++ /opt/custom/sbin/delete_snapshots   Sat Dec  5 10:02:00 2015
@@ -79,6 +79,8 @@
   platform='bsd'
 elif [[ "$unamestr" == 'Darwin' ]]; then
   platform='bsd'
+elif [[ "$unamestr" == 'SunOS' ]]; then
+  platform='sunos'
 else
   echo -e "unknown platform $unamestr 1>&2"
   exit 1
@@ -93,6 +95,8 @@

 if [[ "$platform" == 'linux' ]]; then
 compare_timestamp=`date --date="-$(echo $compare_seconds) seconds" +"%s"`
+elif [[ "$platform" == 'sunos' ]]; then
+compare_timestamp=$((`date +"%s"` - $compare_seconds))
 else
 compare_timestamp=`date -j -v-$(echo $compare_seconds)S +"%s"`
 fi
@@ -99,7 +103,7 @@

 # get a list of snapshots sorted by creation date, so that we get the oldest first
 # This will allow us to skip the loop early
-snapshots=`zfs list -H -t snapshot -o name,creation -s creation | grep $pattern`
+snapshots=`zfs list -H -t snapshot -o name,creation -s creation -p | grep $pattern`

 if [[ -z $snapshots ]]; then
   echo "no snapshots found for pattern $pattern"
@@ -112,14 +116,8 @@
 IFS=$'\n'
 for line in $snapshots; do
   snapshot=`echo $line | cut -f 1`
-  creation_date=`echo $line | cut -f 2`
+  creation_date_timestamp=`echo $line | cut -f 2`

-  if [[ "$platform" == 'linux' ]]; then
-    creation_date_timestamp=`date --date="$creation_date" "+%s"`
-  else
-    creation_date_timestamp=`date -j -f "%a %b %d %H:%M %Y" "$creation_date" "+%s"`
-  fi
-
   # Check if the creation date of a snapshot is less than our compare date
   # Meaning if it is older than our compare date
   # It is younger, we can stop processing since we the list is sorted by
@@ -130,10 +128,10 @@
       echo "DELETE: $snapshot from $creation_date"
       zfs destroy $snapshot
     else
-      echo "WOULD DELETE: $snapshot from $creation_date"
+      echo "WOULD DELETE: $snapshot"
     fi
   else
-    echo "KEEP: $snapshot from $creation_date"
+    echo "KEEP: $snapshot"
     echo "No more snapshots to be processed for $pattern. Skipping.."
     break
   fi
@dtomilso
Copy link

dtomilso commented Apr 6, 2021

Would you mind pasting a copy of the script with the changes?
This is breaking for me when trying to run on Solaris 11.4 December 2020 SRU.

@sigxcpu76
Copy link
Author

sigxcpu76 commented Apr 6, 2021

I've moved away from Illumos for 5 years now :)
I highly recommend a "keep the last N snaphots" pattern instead of deleting by date.

Nowadays I use the following script to cleanup by the above mentioned pattern.

#!/bin/bash

ROOT_DS=slow
KEEP=3

for i in $(zfs list -H -r $ROOT_DS -o name); do
	for s in $(zfs list -S creation -H -t snapshot -o name $i | tail -n +${KEEP}); do
		echo "Deleting $s"
		zfs destroy $s
	done
done

tail in SunOS may be different, so you need to test. That syntax is used to display the last lines except .
few($KEEP)

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