Skip to content

Instantly share code, notes, and snippets.

@jasonrm
Created September 5, 2009 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonrm/181352 to your computer and use it in GitHub Desktop.
Save jasonrm/181352 to your computer and use it in GitHub Desktop.
Take and manage ZFS snapshots on Mac OS X 10.5/10.6
#!/usr/bin/php
<?php
print("zfsSnapshot.php : Starting ZFS snapshots\n");
$howFarBackInTime_Hours = 8;
$howFrequentIsScriptCalled_Minutes = 10;
$keepThisMany = $howFarBackInTime_Hours * 60 / $howFrequentIsScriptCalled_Minutes;
print("zfsSnapshot.php : Keeping $keepThisMany snapshots\n");
exec('zfs list -o name -t filesystem | sed 1d', $filesystemList);
foreach ($filesystemList as $filesystem){
print "Taking snapshot on [$filesystem]\n";
exec('zfs snapshot "' . $filesystem . '@' . date(DATE_ATOM) . '"');
$zfsList='zfs list -t snapshot | grep -Eo "' . $filesystem . '@[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9](.[0-9][0-9]:[0-9][0-9])?"';
exec($zfsList, $snapshotList);
$numberToDelete=count($snapshotList) - $keepThisMany;
foreach ($snapshotList as $snapshot){
if ($numberToDelete > 0){
print "Deleting old snapshot [" . $snapshot . "]\n";
print exec('zfs destroy "' . $snapshot . '"');
$numberToDelete -= 1;
}
else break;
}
unset($snapshotList);
}
print("zfsSnapshot.php : Finished ZFS snapshots\n\n");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment