How-to: big cPanel email-only server migration

smtalk

Administrator
Staff member
Joined
Aug 22, 2006
Messages
8,209
Location
LT, EU
We've received some requests to write a guide on how to migrate email-only server. First step would be to create a cPanel backup without email data, but with email accounts. Something like this might help:
https://forums.cpanel.net/threads/scripts-pkgacct-with-email-accounts-and-without-public_html.264611/

You could then use the same "pkgacct" command for all the accounts, just like mentioned in https://forum.directadmin.com/showthread.php?t=58059.

When you get the account backup ready, please use https://forum.directadmin.com/showthread.php?t=58059&p=296439#post296439 to migrate it (DA might support direct cPanel backup restores in GUI soon, but CLI is the only option now).

With these steps done, please make sure you see the accounts in DirectAdmin. Then you may use rsync on your DA server to sync FROM cpanel. A script would look like this (just wrote, so, would need some testing):
Code:
#!/bin/sh
[B]#Change IP to the real one, make sure we're able to login without password to it (SSH keys)[/B]
CPANEL_SERVER_IP=[B]8.8.8.8[/B]

for u in `ls /usr/local/directadmin/data/users`; do
{
  echo "doing ${u}"
  for d in `grep ": ${u}$" /etc/virtual/domainowners | cut -d: -f1`; do
  {
    echo "domain: ${d}";

    for e in `cat /etc/virtual/${d}/passwd | cut -d':' -f1`; do {
        if [ ! -d /home/${u}/imap/${d}/${e}/Maildir ]; then
            mkdir -p /home/${u}/imap/${d}/${e}/Maildir
        fi
        rscync --delete -at root@${CPANEL_SERVER_IP}:/home/${u}/mail/${d}/${e}/ /home/${u}/imap/${d}/${e}/Maildir/
        /usr/local/directadmin/scripts/set_permissions.sh maildir ${u} /home/${u}/imap/${d}/${e}/Maildir
        echo "${e}@${d} has been synchronized!"
    };
    done;
  };
  done;

  echo "";
};
done;
For single account (to test), you can just run the following instead of the script for all the account (replace uppercase strings with real ones):
Code:
rscync --delete -at root@[B]YOUR_SERVER_IP[/B]:/home/[B]USER[/B]/mail/[B]DOMAIN[/B]/[B]EMAIL[/B]/ /home/[B]USER[/B]/imap/[B]DOMAIN[/B]/[B]EMAIL[/B]/Maildir/
Example, to sync info@yourdomain.com, if account name is myuser and server IP is 8.8.8.8:
Code:
rscync --delete -at root@[B]8.8.8.8[/B]:/home/[B]myuser[/B]/mail/[B]yourdomain.com[/B]/[B]info[/B]/ /home/[B]myuser[/B]/imap/[B]yourdomain.com[/B]/[B]info[/B]/Maildir/
Troubleshooting:
-----
If you notice any issues, these might be permissions related, to fix them:
Code:
/usr/local/directadmin/scripts/set_permissions.sh maildir [B]myuser[/B] /home/[B]myuser[/B]/imap/[B]yourdomain.com[/B]/[B]info[/B]/Maildir
If something is still not okay (shouldn't be), you can simply remove dovecot index files:
rm -f /home/myuser/imap/yourdomain.com/info/Maildir/dovecot*
-----

Steps:
1) Backup cPanel account without email data
2) Restore it in DirectAdmin
3) Run rsync command, wait for it to finish
4) Can do internal testing now to see if everything is here
5) You can now re-run rsync command, it'd just transfer the differences if there are any, and you're done :)

Thank you!
 

chelssi

Verified User
Joined
Aug 2, 2019
Messages
22
Hi smtalk,

Great script!!! I was looking for how to move only emails accounts.

This is also valid if I have already created manually the emails accounts, isn't it?

I only jump to step three and run the script, correct?

Thank you!!!
 

smtalk

Administrator
Staff member
Joined
Aug 22, 2006
Messages
8,209
Location
LT, EU
Hi smtalk,

Great script!!! I was looking for how to move only emails accounts.

This is also valid if I have already created manually the emails accounts, isn't it?

I only jump to step three and run the script, correct?

Thank you!!!
Yes, if you just need to sync the data :)
 
Top