dsync

介紹

 

* master/master replication
* replication is done asynchronously
* The replication is done by looking at Dovecot index files

NOTE: v2.2 is highly recommended for this. Earlier versions can't do incremental metadata syncing. This means that the more mails a mailbox has, the slower it is to sync it.

原理

Mailboxes have 128 bit globally unique IDs (GUID)

Dovecot v2.0 created a "mailbox log",

which adds a record with mailbox GUID and timestamp whenever mailbox is renamed or deleted.

* timestamp is looked up from the mailbox list index.

 * If mailbox exists only on one side, the other side checks if mailbox log
   contains a delete record for its GUID. If there is one, the mailbox is
   deleted from the other side. If there's not, the mailbox is created and
   synced.
   
 * Subscriptions and unsubscriptions are synced in a similar way. But because
   it's possible to be subscribed to nonexistent mailboxes, mailbox log can't
   contain mailbox GUIDs for them. Instead the first 128 bits of SHA1 of
   mailbox name are used.


Usage:

dsync [-u <user>] mirror <local mail_location> | [<user>@]<host> | <remote dsync command>

* dsync will merge the changes without losing any changes done on either side.
  (IMAP or a POP3 client shouldn't be able to notice any differences between the two mailboxes.)

* dsync can be run completely standalone.

 

Opts:

-c  config-file
-f  "full sync"
-m mailbox        Specifies the mailbox that should be synchronized

-R                !! Reverse backup direction !!
                  so mails in location2 are backed up to default mail location.

                  -v                verbose

-u user           # lookup for the given user should be done and used to set up the environment (uid, gid, home, etc.)
                  # By default the system user's current environment will be used.
                  
-m mailbox        # Specifies the mailbox that should be synchronized or from which mails should be converted.
                  # The default: all mailboxes.

-D                # Activates debug messages and makes dsync more verbose.

-o setting=value  # Overrides the configuration setting from /etc/dovecot/dovecot.conf
                  ( i.e. -o mail_location=mail_location setting )

 

Synopsis:

dsync [options] mirror location2
dsync [options] backup location2

Mirror
    Does a two-way synchronization between two mail locations.
    Any potential UID conflicts are resolved by giving them new UIDs

Backup(Any changes done in destination are discarded)
    Backup mails from default mail location to location2

 

Exit Status

0    Synchronization was done perfectly.
2    Synchronization was done without errors, but some changes couldn't be done
1, >2
    Synchronization failed.

    
Exampe:

<1> Sync Local Folder:

# mkdir /data/tester/Maildir
# chown vmail. /data/tester/Maildir
# chmod 770 /data/tester/Maildir

dsync -o mail_location=maildir:/var/vmail/vmail/???/tester/Maildir mirror maildir:/data/tester/Maildir

<2> Sync Mailbox to Folder:

# The first mail location is based on configuration(mail_location or userdb settings)

dsync -u tester@??? mirror maildir:/data/tester/Maildir

可能會遇到以下 Error:

dsync(root): Error: user tester@???: Auth USER lookup failed
dsync(root): Fatal: User lookup failed: Internal error occurred. Refer to server log for more information.

log:

Aug 04 18:22:00 auth-worker: Error: mysql: Query failed, retrying: Unknown column 'mailbox.enabledsync' in 'where clause'

解決:

ALTER TABLE `mailbox`
  ADD COLUMN `enabledsync` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'dsync' AFTER `enablelib-storage`;

問題2

dsync(root): Fatal: User lookup failed: Unknown user

解決:

mailbox.enabledsync 沒有 1

 

location2 ( location is on a remote server)

ssh mailuser@host dsync -u user

# If the location is on a remote server, dsync can ssh to it by giving host or user@host as the parameter.
# If user is specified, it's given as -u parameter to dsync, not to ssh. The ssh username is always the default.

 

<3> Mirror mailboxes to a remote server (By SSH).

a) The ssh username is always use as dsync default user

dsync -u username mirror [email protected]

b) another user

dsync -u username mirror ssh -i id_dsa.dovecot [email protected] dsync -u username

 


Script

#!/bin/bash

_MAILBOX='test1@???'
_SSHUSER='[email protected]'
_SSHKEY='/home/syncmail/.ssh/id_rsa'

######################################## Code
echo "Start sync `date`"

dsync -v -u $_MAILBOX mirror "ssh -C -i $_SSHKEY $_SSHUSER dsync -u $_MAILBOX"

echo "End Status: $?, `date`"

 


Output

 

# 在行 sync cmd 的地方建立 MyTest_Local, 在另一邊建立 MyTest_Remote

dsync-local(test1@???): Info: MyTest_Local: only in source (guid=3844a02d1f91e05394610000e3f46b7f)
dsync-local(test1@???): Info: MyTest_Remote: only in dest (guid=5e01961e4c8fe053ed28000087f07640)

 


Replication

http://wiki2.dovecot.org/Replication

 

2.0 版

# globally

mail_plugins = $mail_plugins notify replication

# started at startup
service replicator {
  process_min_avail = 1
}

# dsyncs can be run in parallel
replication_max_conns = 10


service aggregator {
  # replication-notify fifo and socket
  fifo_listener replication-notify-fifo {
    user = vmail
  }
  # notifies the replicator processes that there is work to be done
  unix_listener replication-notify {
    user = vmail
  }
}

dsync_remote_cmd = ssh -l%{login} %{host} doveadm dsync-server -u%u
plugin {
  mail_replica = remote:[email protected]
}

是行 dsync 時的 cmd

dsync_remote_cmd

 

2.2 版

doveadm-server:

 

# ssl_client_ca_dir = /etc/ssl/certs

service doveadm {
  inet_listener {
    # ssl = yes
    port = 12345
  }
}

doveadm_password = secret

doveadm-client:

doveadm_port = 12345

plugin {
  mail_replica = tcp:anotherhost.example.com # use doveadm_port
  #mail_replica = tcp:anotherhost.example.com:12345 # use port 12345 explicitly
}

* The replicas can't share the same quota database, since both will always update it

 

在 v2.2.9+ 版後, dovecot 支援叫 daemon 相即sync

doveadm sync

 

Creative Commons license icon Creative Commons license icon