There are many articles on the web on how to migrate, backup or sync mailboxes with dovecot. Very useful when migrating from one server to another or converting from one type to another.
But there are none on how to copy some specific mailboxes from one account to another, on the same mail server !
That took me in a rabbit hole today, as I got this request from a client.
Prologue
There are many accounts on that dovecot/postfix mail server. We need to copy (not share) about 120 specific folders from a colleague that is many years on the company to a new colleague.
Dsync
The proper way to do that is via dovecot sync or dsync command for short. The main problem with that is the majority of articles and howtos and even man pages or about of syncing mailboxes from one server to another.
The solution actually is pretty much straight forward and simply but it wasnt till I made a few mistakes, so here is the correct way!
dsync -Dv -m "mailbox_name" -u "source_email_address" backup dsync -u "destination_email_address"
- D is for debug
- v is for verbose
- m is for declaring the specific mailbox
- u is for the username/email address on the local server
We can use two verbs, backup and mirror. Backup is one way sync, Mirror is two way sync. I chose backup
Now the tricky part, here is the destination server and all the examples are mentioning a remote server. I wanted to transfer mailboxes on the same server and it took me more time that I would like to share with you to figure it out.
You just need to add the dsync command and the destination username/email address!
example:
dsync -Dv \
-m 'INBOX.1_Folder1 Rare Diseases' -u FirstName1.Lastname1@example.com \
backup \
dsync -u FirstName2.Lastname2@example.com
Caveats
Without getting into much details, you should use latin1 characters and avoid special characters. I requested some renames from the origin user and after that everything went smoothly with the backup/sync.
Control/Index/Subscriptions
That was an issue. I did something wrong and I could not subscribe to any folders. I’ve tried a couple of hours to figure this out but in the end I removed the related control/index
dovecot files from the destination user and restarted dovecot service.
It worked !
some commands
List all related folders/mailboxes under 1_company1
doveadm mailbox list -u FirstName1.Lastname1@example.com | grep -i 1_company1
oneliner
doveadm mailbox list -u FirstName1.Lastname1@example.com | grep -i 1_company1 | sort | grep -v Diseases | awk '{print "dsync -Dv -m \47"$0"\47 -u FirstName1.Lastname1@example.com backup dsync -u FirstName2.Lastname2@example.com}'
that’s it!