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
|
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";
}
}
}
# deleING because only now do we actually remove the files
say "deleting $found messages from backups, and $missing were not found";
|