prune_b2.pl
1	#!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 my $DRY_RUN = 1;
7
8 if ( defined $ARGV[0] && $ARGV[0] eq "--run" ) {
9 $DRY_RUN = 0;
10 }
11
12 sub find_bin($) {
13 my $bin_name = shift;
14 my $bin_path = `which $bin_name`;
15 chomp $bin_path;
16 if ( ! defined $bin_path || $bin_path eq "" ) {
17 print("Couldn't find $bin_name\n");
18 exit 1;
19 }
20
21 return $bin_path;
22 }
23
24 my $RBIN = find_bin("rclone");
25
26 my $RPATH = "spw-b2:spw01Backups1/spwbk-site-backups/";
27
28 my $DBIN = find_bin("date");
29
30 my $DATE = `$DBIN +%Y%m%d`;
31 chomp $DATE;
32
33 foreach my $file ( split("\n", `$RBIN ls $RPATH | awk \'{print \$2}\'`) ) {
34 if ( $file =~ m/bk_([0-9]{4}-[0-9]{2}-[0-9]{2})/ ) {
35 my $file_date = $1;
36 $file_date =~ tr/-//d;
37 if ( $file_date < $DATE ) {
38 my $cmd = "$RBIN delete $RPATH$file";
39 print("$cmd\n");
40 if ( $DRY_RUN eq 0 ) {
41 system($cmd) == 0 or die
42 "Failed to delete $file, exiting...\n";
43 }
44 }
45 }
46 }