App-tmclean

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN


# SYNOPSIS

    % tmclean [--dry-run] [--days=300/--before=2018/01/01]

## Options

- --dry-run
- --days

    Delete backups that before the specified number of days (default: 366)

- --before

    Delete backups before the specified date

# DESCRIPTION

tmclean is command line utility for cleanup TimeMachine.

# INSTALLATION

    % cpanm App::tmclean

## Homebrew

lib/App/tmclean.pm  view on Meta::CPAN

    $opt{dry_run} = delete $opt{'dry-run'};
    return (\%opt, \@ARGV);
}

sub run {
    my $self = shift;

    if (!$self->dry_run && $ENV{USER} ne 'root') {
        die "tmutil requires root privileges\n";
    }
    $self->cmd(qw/tmutil stopbackup/);
    $self->cmd(qw/tmutil disable/); # need sudo

    my @targets = $self->backups2delete;
    unless (@targets) {
        logf 'no deletion targets found';
        return 0;
    }
    my $mount_point = $self->mount_point;

    logf "following backups to be deleted:\n  %s", join("\n  ", @targets);
    for my $bak (@targets) {
        $self->cmd(qw/tmutil delete/, $bak); # need sudo
    }
    my $dev_name = dev_name($targets[0]);
    $self->cmd(qw/hdiutil detach/, $dev_name);

    my $backupbundle_path = sprintf '%s/%s.sparsebundle', $mount_point, $self->machine_name;
    if (! -d $backupbundle_path) {
        # backupbundle path is changed after Catalina
        $backupbundle_path =~ s/\.sparsebundle$/.backupbundle/;
    }
    $self->cmd(qw/hdiutil compact/, $backupbundle_path); # need sudo
    $self->cmd(qw/tmutil enable/); # need sudo
}

sub backups2delete {
    my $self = shift;
    my @backups = `tmutil listbackups`;
    if ($? != 0) {
        die "failed to execute `tmutil listbackups`: $?\n";
    }
    # e.g. /Volumes/Time Machine Backup/Backups.backupdb/$machine/2018-01-07-033608
    return grep {
        chomp;
        my @paths = split m!/!, $_;
        my $backup_date = eval { Time::Piece->strptime($paths[-1], '%Y-%m-%d-%H%M%S') };
        $backup_date && $self->before_tp > $backup_date;
    } @backups;
}

sub mount_point {
    my $self = shift;

    $self->{mount_point} ||= sub {
        my @lines = `tmutil destinationinfo`;
        if ($? != 0) {
            die "failed to execute `tmutil destinationinfo`: $?\n";
        }

script/tmclean  view on Meta::CPAN

    % tmclean [--dry-run] [--days=300/--before=2018/01/01]

=head2 Options

=over

=item --dry-run

=item --days

Delete backups that before the specified number of days (default: 366)

=item --before

Delete backups before the specified date

=back

=head1 DESCRIPTION

tmclean is command line utility for cleanup TimeMachine.

=head1 INSTALLATION

    % cpanm App::tmclean



( run in 1.161 second using v1.01-cache-2.11-cpan-49f99fa48dc )