Skip to content

Restoring only mail from a cPanel backup: targeted recovery

When a user deletes a mailbox or a sync wipes inboxes, you need the mail back without touching the database or web files. The targeted path extracts just the mail.

Mail-only restores are common: a user accidentally emptied their trash including unread items, a misconfigured IMAP client deleted local copies, or a sync between two devices wiped a folder. The full account restore is overkill — it would also revert blog posts published after the backup, e-commerce orders, and any other database content. The targeted path is to pull only the user’s mailbox directories out of the backup and copy them back into the live mail tree.

What’s happening

cPanel stores mail in /home/<user>/mail/<domain>/<mailbox>/ using Maildir format. Each Maildir is just a directory tree of plain files — cur/, new/, tmp/, and the index files. A backup contains an exact copy of that tree at the backup time. Restoring is as simple as extracting from the backup tar and rsyncing back. The catch: messages received after the backup but before the restore will be overwritten if you do a full directory replace, so the merge step matters.

How to verify

Find the mail in the backup:

# does the backup contain the mailbox?
tar -tzf /backup/<date>/accounts/<user>.tar.gz | grep "mail/<domain>/<mailbox>"
# extract just one mailbox to a working directory
mkdir -p /root/restore-mail
tar -xzf /backup/<date>/accounts/<user>.tar.gz \
    --wildcards "*/mail/<domain>/<mailbox>/*" -C /root/restore-mail
ls /root/restore-mail/*/mail/<domain>/<mailbox>/
# count messages in extracted vs live
find /root/restore-mail/*/mail/<domain>/<mailbox>/cur -type f | wc -l
find /home/<user>/mail/<domain>/<mailbox>/cur -type f | wc -l

The fix

  1. Stop IMAP for the user — whmapi1 disable_mailbox_login user=<user> email=<mailbox>@<domain>. Prevents the IMAP client from re-syncing during restore and conflicting.
  2. Merge rather than replace — rsync -av --ignore-existing /root/restore-mail/*/mail/<domain>/<mailbox>/ /home/<user>/mail/<domain>/<mailbox>/. The --ignore-existing flag prevents overwriting newer messages that arrived after the backup.
  3. Fix ownership — files extracted as root must be chown’d to the cPanel user: chown -R <user>:<user> /home/<user>/mail/<domain>/<mailbox>/. Maildir requires the cPanel user own the files; Dovecot refuses to deliver if ownership is wrong.
  4. Rebuild the Dovecot index — doveadm index -u <mailbox>@<domain> INBOX. The index file (dovecot.index) tracks unread state per IMAP session; rebuilding ensures the client sees the restored messages as unread or matching the original flags.
  5. Re-enable IMAP — whmapi1 enable_mailbox_login user=<user> email=<mailbox>@<domain>. Have the user re-sync their client; restored messages should appear.

For a full domain restore, the same shape applies but at the mail/<domain>/ level. Stack Harbor handles targeted mail recovery under cPanel server administration — start a conversation when only mail needs restoring.