Tibor's Musings

Troubleshooting Mbsync Duplicate UID Errors

I've been using excellent msync tool to synchronise IMAP mailboxes between cloud and laptop. (mbsync seems both faster and less resource-hungry than offlineimap.) Once, probably due to a wrong move operation, the synchronisation started to report "Maildir error: duplicate UID" messages. How to quickly repair this situation?

Here is the error message:

$ mbsync -a
[...]
Synchronizing...
Selecting master INBOX...
Selecting slave INBOX...
Loading master...
Loading slave...
Maildir error: duplicate UID 2.

This means the INBOX folder has problematic messages with UID=2. How to find them:

$ cd ~/Local/mbsyncmail/INBOX # where my INBOX lives
$ find . -name "*U=2:*" -exec ls -l {} \;
-rw------- 1 simko simko  1523 Dec 20 21:20 ./cur/1419106858.5661_2.pcuds06,U=2:2,S
-rw------- 1 simko simko 39500 Feb 13 10:20 ./cur/1423819205.29514_1.pcuds06,U=2:2,S

Note the ,U=2: part of the filename: the second message got somehow the same UID as the first one. (Probably by wrong move between folders without changing file name.)

The fix consists in keeping the first (older) message as is, and changing the second (newer) message file name to remove the ,U=... suffix part:

$ mv ./cur/1423819205.29514_1.pcuds06,U=2:2,S ./cur/1423819205.29514_1.pcuds06
[...]
-rw------- 1 simko simko  1523 Dec 20 21:20 ./cur/1419106858.5661_2.pcuds06,U=2:2,S
-rw------- 1 simko simko 39500 Feb 13 10:20 ./cur/1423819205.29514_1.pcuds06

This deduplicates the problematic UID and forces mbsync to create new UID for the second message at its next run.

Was this message the only problematic one?

$ ls -lR cur | grep -o 'U=.*:' | sort | uniq -d
U=38:
U=39:
U=4:
U=7:

Nope, there are four more messages to fix in the same manner. Once done, the synchronisation works well again:

$ mbsync -a | grep -i error | wc -l
0

unix