summaryrefslogtreecommitdiff
path: root/scripts/mail/compare_backup_msgids.pl
blob: 4218218a0e861ecef2e7b74dfa7044d59a171650 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use 5.028;
use strict;
use warnings;

use File::Temp "tempfile";
use Mail::Box::Manager;
use IO::Uncompress::Gunzip qw(gunzip $GunzipError);

# This can't help ensure that we have every copy of a message, just that we
# have at least one copy, up to equality of Message-ID.

our @live   = grep !/\/annex\z/, <$ENV{HOME}/.fmail/*>;
our @backup = grep !/\/annex\z/, <$ENV{HOME}/local/big/restore/*>;
@live and @backup or die;
# our @archive = <$ENV{HOME}/annex/mail/*-jan-2023.gz>;

my $mgr = Mail::Box::Manager->new;
my ($skipped, $found, $missing, %live) = (0, 0, 0);

foreach my $dir (@live) {
    my $md = $mgr->open(
	$dir,
	access    => "r",
	create    => 0,
	keep_dups => 1,
	type      => "maildir"
    );
    printf "Reading %d msgids from %s\n", scalar @$md, $dir;
    $live{ $_->messageId }++ for @$md;
}

# foreach my $archive (@archive) {
#     my (undef, $temp) = tempfile DELETE => 1;
#     gunzip $archive, $temp or die "gunzip failed: $GunzipError\n";
#     my $mb = $mgr->open(
# 	$temp,
# 	access    => "r",
# 	create    => 0,
# 	keep_dups => 1,
# 	type      => "mbox"
#     );
#     printf "Reading %d msgids from %s\n", scalar @$mb, $archive;
#     $live{ $_->messageId }++ for @$mb;
# }

foreach my $dir (@backup) {
    my $md = $mgr->open($dir, access => "rw", create => 0, type => "maildir");
    printf "Processing %d msgs from %s\n", scalar @$md, $dir;
    for my $msg (@$md) {
	my $id = $msg->messageId;
	if (!defined $id) {
	    $skipped++;
	    say "Skipping message lacking Message-ID: " . $msg->file;
	} elsif ($live{$id}) {
	    $found++;
	    $msg->delete;
	} else {
	    $missing++;
	    say "$id missing from live mailboxes";
	}
    }
}

# deletING because only when we reach the end of the program does
# Mail::Box::Manager actually remove any files
say "deleting $found messages from backups, and $missing were not found";