Skip to content

Instantly share code, notes, and snippets.

@onstatus
Last active August 2, 2020 17:48
Show Gist options
  • Save onstatus/8a0e99e6f3830794e7c81cc87c3f0d89 to your computer and use it in GitHub Desktop.
Save onstatus/8a0e99e6f3830794e7c81cc87c3f0d89 to your computer and use it in GitHub Desktop.
Backup and/or sync Maildir between old and new mail server. This script use Dovecot Dsync configuration.
#!/bin/bash
# This script require Dsync configuration
# https://wiki2.dovecot.org/Migration/Dsync
# - IMAP migration configuration
# - POP3 migration configuration
# Dsync can use master authentication (https://wiki2.dovecot.org/Authentication/MasterUsers)
NOW=$(date +"%m-%d-%Y")
SUBJECT="Email Backed Up - "$NOW""
EMAIL="[email protected]"
EMAILMESSAGE="migration_log.txt"
echo [`date`] Migration Started >> $EMAILMESSAGE
for f in $(cat email_migrate_list.txt)
do
ACCOUNT=$(postmap -q [email protected] ldap:/etc/postfix/ldap_virtual_recipients.cfg)
if [ ! -z "$ACCOUNT" ] && [ "$ACCOUNT" == "[email protected]" ]
then
echo >> $EMAILMESSAGE 2>&1
echo [`date`] Migration start for [email protected] >> $EMAILMESSAGE 2>&1
touch /home/$f/syncstate
if [ -s /home/$f/syncstate ]
then
doveadm -o mail_fsync=never backup -s $(cat /home/$f/syncstate) -R -u [email protected] imapc: > /home/$f/syncstate
echo [`date`] Using state for [email protected] >> $EMAILMESSAGE 2>&1
else
doveadm -o mail_fsync=never backup -s $(cat /home/$f/syncstate) -f -R -u [email protected] imapc: > /home/$f/syncstate
echo [`date`] Creating state for [email protected] >> $EMAILMESSAGE 2>&1
fi
#doveadm sync -u [email protected] imapc: >> $EMAILMESSAGE 2>&1
#doveadm backup -s -R -u [email protected] imapc: >> $EMAILMESSAGE 2>&1
echo [`date`] Migration done for [email protected] >> $EMAILMESSAGE 2>&1
echo >> $EMAILMESSAGE 2>&1
else
echo >> $EMAILMESSAGE 2>&1
echo [`date`] Account [email protected] is missing. Check account in FreeIPA. >> $EMAILMESSAGE 2>&1
fi
done
echo >> $EMAILMESSAGE 2>&1
echo [`date`] Migration Completed >> $EMAILMESSAGE
mail -s "$SUBJECT" "$EMAIL" < "$EMAILMESSAGE"
server_host = ldap://ipa.demo1.freeipa.org:389
search_base = cn=users,cn=accounts,dc=demo1,dc=freeipa,dc=org
query_filter = (&(objectClass=posixAccount)(uid=%u)(nsaccountlock=FALSE))
result_attribute = mail
bind_dn = uid=postfix,cn=users,cn=accounts,dc=demo1,dc=freeipa,dc=org
bind_pw = password
bind = yes
version = 3
scope = sub
@onstatus
Copy link
Author

onstatus commented May 24, 2018

Steps to setup Dsync
1 - Create master user for remote imap access on old mailserver (https://wiki2.dovecot.org/Authentication/MasterUsers)
1.1 - Enable master auth
[root@mail ~]# vim /etc/dovecot/conf.d/10-auth.conf
[...]
!include auth-master.conf.ext
[...]
1.2 - Enable passdb for master user
[root@mail ~]# vim /etc/dovecot/conf.d/auth-master.conf.ext
passdb {
driver = passwd-file
master = yes
args = /etc/dovecot/master-users
}
1.3 - Create master account
[root@mail ~]# htpasswd -b -c -s /etc/dovecot/master-users master password
Adding password for user master
[root@mail ~]# cat /etc/dovecot/master-users
master:{SHA}LzJb4MIcJcoOefdgrecasqdsvretertretrJE=
[root@mail ~]# service dovecot restart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment