Skip to content

Instantly share code, notes, and snippets.

@robinkaty
Last active June 19, 2023 03:22
Show Gist options
  • Save robinkaty/9a65648d2ee730c91a1ce8a5a4bef5ed to your computer and use it in GitHub Desktop.
Save robinkaty/9a65648d2ee730c91a1ce8a5a4bef5ed to your computer and use it in GitHub Desktop.
script for managing timemachine backups over multiple destinations
#!/usr/bin/env perl
#use warnings;;
use Data::Dump qw(dump);
# assuming you have the backup disk connected & root privileges:
# get the latest backup to exclude from deletion
my $latest = `sudo tmutil latestbackup`;
# the 4th line after the last space from `tmutil destinationinfo` output
# contains the mount disk name
my $destination_info = `sudo tmutil destinationinfo`;
my @lines = split("\n", $destination_info);
my $mount = '';
my @mounts = ();
my $ArrayNumber = 1;
my @inner_array = ();
my @backups = ();
foreach my $line (@lines) {
if ($line =~ /Mount Point/) {
$mount = (split(": ", $line))[1];
push @mounts,$mount ;
printf "%s\n", $mount;
printf "----------------------\n";
}
}
dump(@mounts);
foreach my $line (@mounts)
{
print "$line\n";
}
$i=0;
foreach my $m (@mounts) {
printf "tmutil listbackups -d %s \n" ,$m;
my @results = `tmutil listbackups -d \'$m\'` ;
s/\.backup//g for @results;
s/\n//g for @results;
# dump(@results);
push @backups, \@results;
}
#print "Elements: $s\n", $#backups;
#print "------------------";
#dump(@backups[0]);
#print "------------------";
#dump(@backups[1]);
for my $index (0..$#backups) {
my @inner_array=();
@inner_array = @{$backups[$index]}; # Retrieve the inner array
printf "Backups on device: %s\n", @mounts[$index],$index + 1, ":\n";
foreach $backups (@inner_array) {
print "$backups\n";
}
print "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment