#! /usr/bin/env perl use strict; use warnings; # Part of backupServer package. This module removes snapshots of the machine backup # older than a certain number of days. # This is the "cleanup" routine for version_zfs, which removes older versions # Syntax: version_cleanup_zfs age rootDir client server # rootDir, client and server are used to build the base directory # age is the number of days to keep, so any snapshot older than that is suspect # this assumes the snapshot name begins with @YYYYMMDD (everything after is ignored) my $age = shift; # Number of days to keep my $rootDir = shift; my $client = shift; my $server = shift; sub getSnapshots { my $rootDirectory = shift; my @snapshots = map{ ($_) = split( "\t", $_) } `zfs list -H -t snapshot | grep $rootDirectory`; chomp @snapshots; return @snapshots; } sub getOldestToKeep { my $age = shift; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time - ($age * 86400) ); return sprintf ( "%04d%02d%02d", $year+1900,$mon+1,$mday); } $rootDir = "$rootDir/$client/$server"; die "Invalid Call, exiting" unless ( $rootDir && $client && $server && $age); die "Invalid Path for rolloff_version, exiting" unless -d $rootDir; my $toKeep = &getOldestToKeep($age); print "$toKeep\n"; # following command accepts path, and returns tab separated list, the first # value being the zfs name mounted there my $GETZFSFROMPATH = 'zfs list -H '; ($rootDir) = split( "\t", `$GETZFSFROMPATH $rootDir` ); print "$rootDir\n\n"; my @currentDirectories = sort &getSnapshots( "$client/$server" ); print join( "\n", @currentDirectories ) . "\n"; print "Removing old versions\n"; foreach my $snap ( @currentDirectories ) { $snap =~ m/\@(\d{8})/; my $timestamp = $1; if ( $timestamp < $toKeep ) { print "Removing snapshot $snap\n"; `zfs destroy -r $snap`; } } 1;