Skip to content

Instantly share code, notes, and snippets.

@janxb
Last active October 7, 2017 17:43
Show Gist options
  • Save janxb/7108d7202fc04e6394cfbf16c911052e to your computer and use it in GitHub Desktop.
Save janxb/7108d7202fc04e6394cfbf16c911052e to your computer and use it in GitHub Desktop.
This Scripts migrates a legacy mailcow instance to a new mailcow.dockerized one. Domains, Users, SOGo-Settings, Forwardings and Mails are migrated.
#!/bin/bash
set -e
cd ~/mailcow-dockerized
DBUSER=mailcow
DBPASS=xxxxxxxxxxxxxxxxxxxx
DBNAME=mailcow
DBHOST=mailcow.lxd
TBLOLD=mailbox
TBLNEW=mailbox_new
IMPORTTBLNAMES="sogo_user_profile alias alias_domain domain domain_admins $TBLNEW quota2 sender_acl"
SSHPASS=xxxxxxxxxxxxxxxx
SQLFILE=/tmp/mailcow_legacy_import.sql
[email protected]:/var/vmail
NEWMAILPATH=/var/lib/docker/volumes/mailcowdockerized_vmail-vol-1/_data
function syncmail {
sshpass -p $SSHPASS rsync -azP --delete $OLDMAILPATH/$1/ $NEWMAILPATH/$1/
sshpass -p $SSHPASS rsync -azP --delete $OLDMAILPATH/sieve/ $NEWMAILPATH/sieve/
}
mysql -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME -e "drop table if exists $TBLNEW"
mysql -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME -e "create table $TBLNEW like $TBLOLD"
mysql -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME -e "insert $TBLNEW select * from $TBLOLD;"
mysql -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME -e "ALTER TABLE $TBLNEW MODIFY COLUMN tls_enforce_in TINYINT(1) NOT NULL DEFAULT '0' AFTER domain, MODIFY COLUMN tls_enforce_out TINYINT(1) NOT NULL DEFAULT '0' AFTER tls_enforce_in;"
mysql -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME -e 'ALTER TABLE '$TBLNEW' ADD COLUMN `kind` VARCHAR(255) NOT NULL AFTER `tls_enforce_out`, ADD COLUMN `multiple_bookings` TINYINT(1) NOT NULL DEFAULT '0' AFTER `kind`, ADD COLUMN `wants_tagged_subject` TINYINT(1) NOT NULL DEFAULT '0' AFTER `multiple_bookings`;'
mysqldump --replace --no-create-info --default-character-set=utf8mb4 \
--host ${DBHOST} -u${DBUSER} -p${DBPASS} ${DBNAME} \
$IMPORTTBLNAMES > $SQLFILE
mysql -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME -e "drop table $TBLNEW"
sed -i -e 's/'$TBLNEW'/'$TBLOLD'/g' $SQLFILE
source mailcow.conf
docker exec -i $(docker-compose ps -q mysql-mailcow) mysql -u${DBUSER} -p${DBPASS} ${DBNAME} < $SQLFILE
rm $SQLFILE
#rm $NEWMAILPATH/* -rf
docker-compose exec dovecot-mailcow doveadm quota recalc -A
syncmail /
docker-compose exec dovecot-mailcow doveadm quota recalc -A
docker-compose restart
cd ~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment