From 18709215efe8a8f2b695ddbe9211e3263fd0f0a2 Mon Sep 17 00:00:00 2001 From: Milan Date: Sat, 23 Sep 2023 00:19:52 +0200 Subject: [PATCH] tools/cleanup-archive.pl: add Remove files older than a year and links older than a week in the directory provided as the first argument. --- tools/cleanup-archive.pl | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tools/cleanup-archive.pl diff --git a/tools/cleanup-archive.pl b/tools/cleanup-archive.pl new file mode 100644 index 0000000..5d4f066 --- /dev/null +++ b/tools/cleanup-archive.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl +use warnings; +use strict; +use Data::Dumper; + +my $base_dir=shift; +-d $base_dir or die "Usage: $0 \n"; + +my $now = time; +my $day = 24 *60 * 60; + +sub remove{ + my ($file) = @_; + print "remove $file\n"; + unlink $file; +} + +sub cleanup_files{ + my ($dir) = @_; + opendir my $dh, $base_dir or die "Could not open '$base_dir' for reading: $!\n"; + while (my $file = readdir $dh) { + next if $file eq '.' or $file eq '..'; + my $path="$base_dir/$file"; + if (-l $path){ + my $age = ($now-(lstat $path)[9])/$day; + remove $path if $age > 7; + }elsif (-f $path){ + my $age = ($now-(stat $path)[9])/$day; + remove $path if $age > 360; + } + } +} + +sub cleanup_tmp_files{ + my ($dir) = @_; + opendir my $dh, $dir or die "Could not open '$dir' for reading: $!\n"; + while (my $file = readdir $dh) { + next if $file eq '.' or $file eq '..'; + my $path="$base_dir/$file"; + my $age = ($now-(stat $path)[9])/$day; + remove $path if $age > 7; + } +} + +cleanup_files($base_dir); +cleanup_tmp_files("$base_dir/tmp"); +