6

I'm migrating my mails from an old server to a new one, the configuration is good (mysql virtual accounts & imap only), with dovecot and postfix. It's already running for other users, I just want to merge old accounts from the old server to the new one.

The migration went fine, I just wanted to know how I could "show" all the old mails in UA Clients. I mean, is there any dovecot index file or such that could be re-created, destroyed, so that all old mails are "flagged" as new ?

lovethebomb
  • 268
  • 1
  • 2
  • 8

4 Answers4

8

I tried the accepted answer and it failed - the dovecot versions in play are probably too far apart and we also switched the underlying MTA. So here is a more robust solution: doveadm import

Assuming you're hosting emails for the domain hosted.tld and an account exists for the login local.account@hosted.tld (or maybe simply ruth) and the backup from the previous server is in MailDir format and available inside a folder structure like /tmp/TRANSFER/domain/account/Maildir then you can import them to your new dovecot hosting with

$ doveadm import -u local.account@hosted.tld maildir:/tmp/TRANSFER/hosted.tld/local.account/Maildir "" all

more generally speaking:

$ doveadm import -u LOCAL_USER FORMAT:PATH "" all

All of which can be gleened from the fine manual. It took two tries to find out simply using "" (empty destination mailbox) was what we really wanted ;-)

You may even have more luck than us without needing to transfer the files beforehand - if old and new server are running at the same time and your accounts are set up appropriately using doveadm sync.

flowtron
  • 215
  • 2
  • 5
  • I know this is a while ago but.. do you know how to specify a root level destination folder other than "INBOX" (i.e. "")? If I try "Test" it automatically gets appended with ".INBOX". So the destination folder is ".Test.INBOX" and in the UI (Roundcube) I get a folder hierarchy: Test -> INBOX. I was just hoping for Test!! – racitup Jun 13 '22 at 00:25
  • For anyone else, since the mails were already in Maildir format, I ended up just moving the folder to ".Test" in the users mailbox and 'chown -R mail:mail' ing the destination – racitup Jun 13 '22 at 00:36
5

For Maildir messages, such flags are stored in the file name. For example:

1328040798.M558634P29803.equal,S=17876,W=18294:2,FS

The letters FS after the comma mean Flagged and Seen. To mark the message as "unseen", either remove the S flag...

for msg in maildir/cur/*; do
    msgbase=${msg%,*}
    flags=${msg##*,}
    flags=${flags//S/}
    mv -v "$msg" "$msgbase,$flags"
done

...or simply throw the messages into the new folder:

mv maildir/cur/* maildir/new/
user1686
  • 8,717
  • 25
  • 38
4

The Maildir format consists of a series of directories - matching the IMAP folder structure, within which are the emails, one file per email.

In order to copy emails from one email system to another, you can simply copy the directories and files, and ensure the permissions for those directories and files are correct at the destination.

The Maildir structure looks like this:

mail/cur/
mail/new/
mail/tmp/
mail/.personal/cur/
mail/.personal/new/
mail/.personal/tmp/

This shows the INBOX folder (cur, new, tmp) and another folder called "personal". Note the dot prefix showing that this folder is hidden, so this should be accounted for in your transfer.

The new folder contains any emails that have not been seen by a client, and the cur folder contains current emails. The tmp folder should be empty if the mail server is not operating.

Paul
  • 1,228
  • 12
  • 24
  • 1
    @grawity - just so you are aware, the actual name of the mail directory varies across distributions - I used the placeholder "Maildir" to signify the maildir folder, not necessarily that it is called that (I should have noted this of course, and incidentally, on some of my mail servers it actually is Maildir) – Paul Jan 31 '12 at 23:20
  • @Paul: Many configurations *do* call it "Maildir", which often causes confusion -- people think that Maildir-format mailboxes are always located in ~/Maildir, or they don't know when someone is talking about the path versus the format... that's the reason why I changed your example. (On the second thought, I probably shouldn't have.) – user1686 Jan 31 '12 at 23:37
  • @grawity No, hang on, you are right - I generally have mailboxes in `~` but this is definitely not always the case and I didn't consider that. The edit makes sense. – Paul Jan 31 '12 at 23:45
  • After playing with files remember to delete dovecot-uidlist and courierimapuiddb files otherwise mail clients won't see changes inmediately – Marco Marsala Oct 21 '15 at 15:56
  • Just make sure file permissions / owner:group match – Bogdan Feb 26 '20 at 13:35
1

Maildir actually uses a unique format that makes this quite easy. Simply place the mail in new/ directory inside the mail users mailroot if you want it to show up as new. Otherwise it goes in the cur/ directory.

Garrett
  • 1,272
  • 10
  • 16
  • 1
    Is the ,S flag appended to the file name that makes the Seen status for IMAP and POP3. cur and new folders are kept only for efficiency matters – Marco Marsala Oct 20 '15 at 20:44
  • I suppose it depends on the MUA implementation but as I understand it (admittedly not having read the source for Dovecot) is that flags such as `S` are not read when emails are in `new/` and are instead added when the MUA moves the file from `new/` to `cur/`. I've not come across anything that considers these two directories as vestigial; every Maildir implementation I've run into uses them. See: http://cr.yp.to/proto/maildir.html – Garrett Oct 21 '15 at 00:29
  • 1
    while all these comments re:filenames+folder-structure are pretty valid, they're not universally true - from one dovecot to the next it went from ".Sent" to "Sent" (no dot) for our setups. **dovecot import** and **dovecot sync** should be more robust solutions. See my answer @ https://serverfault.com/a/866298/300859 – flowtron Aug 01 '17 at 14:51