summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2023-02-04 14:04:54 -0700
committerSean Whitton <spwhitton@spwhitton.name>2023-02-04 14:05:01 -0700
commit4ec952ce2429925cf00f9012edbb39fd3f7b3212 (patch)
tree356929d503cf48609017ed82ffe2347a3c00ee67 /scripts
parent21f57f211a1ec5b40f92d040ac9d346cf9dd6552 (diff)
downloaddotfiles-4ec952ce2429925cf00f9012edbb39fd3f7b3212.tar.gz
add compare_backup_msgids.pl
Diffstat (limited to 'scripts')
-rw-r--r--scripts/mail/compare_backup_msgids.pl65
1 files changed, 65 insertions, 0 deletions
diff --git a/scripts/mail/compare_backup_msgids.pl b/scripts/mail/compare_backup_msgids.pl
new file mode 100644
index 00000000..c19389af
--- /dev/null
+++ b/scripts/mail/compare_backup_msgids.pl
@@ -0,0 +1,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";