#! /usr/local/env perl use warnings; use strict; # Part of backupServer package. This module removes copies of the current directory # older than a certain number of days. # This is the "cleanup" routine for version, which removes older versions # Syntax: rolloff_version targetDir age rootDir client server # rootDir, client and server are used to build the base directory and targetDir is relative to # them. # age will determine what to delete; anything named older than now()-age is removed. my $targetDir = shift; # the directory under which we create the backup copy my $age = shift; # Number of days to keep my $rootDir = shift; my $client = shift; my $server = shift; my $confDir = shift; sub getDirectories { my $rootDirectory = shift; opendir( my $dh, $rootDirectory ); # following gets all non-dotted directory names my @files = grep { ! /^\./ && -d "$rootDirectory/$_" } readdir( $dh ); closedir $dh; return @files; } $rootDir = "$rootDir/$client/$server/$targetDir"; die "Invalid Call, exiting" unless ( $rootDir && $client && $server && $targetDir && $age); die "Invalid Path for rolloff_version, exiting" unless -d $rootDir; my $toKeep = `date -d "-$age days" +"%Y-%m-%d"`; $toKeep =~ m/(\d{4})-(\d{2})-(\d{2})/; $toKeep = $1 * 365 + $2 * 30.5 + $3; # rough approximation, not valid but works for this. my @currentDirectories = &getDirectories( $rootDir ); print "Removing old versions\n"; foreach $dir ( @currentDirectories ) { if ( $dir =~ m/(\d{4})-(\d{2})-(\d{2})/ ) { if ( $1 * 365 + $2 * 30.5 + $3 < $toKeep ) { $temp = qx!rm -fR $rootDir/$dir!; print "\t" . ( $temp ? $temp : $dir ) . "\n"; } } } 1;