KB
Backup & Recovery

Restore Email of a Specific User from Backup in cPanel

3 min read569 words6 code blocks

At a glance#

  • Purpose: Restore a single user's mailbox from cPanel backups without affecting other accounts.
  • Applies to: cPanel & WHM with backups configured.
  • Risk: Medium - restoring over a live mailbox can overwrite newer mail.
  • Time: About 45 minutes.

This guide explains how to restore a specific user's email from a backup using rsync, fix file permissions, regenerate maildirsize files, and backup cPanel configurations. Follow the steps below carefully to ensure a successful email restoration process.

Prerequisites#

  • Root access to the server.
  • Backup files available in the specified backup directory.
  • The specific cPanel user's email folder that needs to be restored.

Steps to Restore User's Email#

1. Copy Emails from Backup to the Current User's Directory#

Use the rsync command to copy the email data from the backup to the active user's directory. This ensures that the emails are restored to the correct location.

bash
rsync -avz --times --perms /backup/2025-xx-xx/accounts/CPUSER/homedir/mail/example.com.np/USER/ /home/CPUSER/mail/example.com.np/USER/

Explanation of parameters:

  • a: Archive mode (preserves symlinks, permissions, timestamps, etc.).
  • v: Verbose output to show the progress.
  • z: Compress file data during the transfer.
  • -times: Preserve modification times.
  • -perms: Preserve permissions.

Note: Replace CPUSER with the correct cPanel username and adjust the paths as per your backup directory structure.


2. Set Directory Permissions#

After restoring the emails, you need to ensure the proper permissions for the user's email directory.

Run the following command to set the correct directory permissions:

bash
chmod -R 750 /home/CPUSER/mail/example.com.np/USER/

Explanation:

  • drwxr-x--x: The directory permissions where CPUSER is the owner, and the group can read and execute, while others have limited access.

Ensure that the directory is owned by the cPanel user (CPUSER).

bash
chown -R CPUSER:CPUSER /home/CPUSER/mail/example.com.np/USER/

3. Create Backup of maildirsize Files#

Before proceeding to regenerate maildirsize files, it’s important to back them up. This step ensures you can restore them if needed.

Run the following command to find and back up all maildirsize files:

bash
find /home/CPUSER/mail/ -type f -name maildirsize | while read line; do mv -v $line{,.bak}; done

Explanation:

  • This command locates all maildirsize files and renames them with a .bak extension to preserve them as backups.

You may adjust the paths according to the specific email directories used.


4. Regenerate maildirsize Files#

After backing up the maildirsize files, regenerate them using the generate_maildirsize script provided by cPanel. This will recalculate and recreate the necessary maildirsize files.

Run the following command as root:

bash
/usr/local/cpanel/scripts/generate_maildirsize --confirm CPUSER

Explanation:

  • This script forces the regeneration of maildirsize files for the specified user (CPUSER), ensuring accurate email size calculations.
  • The -confirm flag is used to bypass any confirmation prompts.

5. Back Up cPanel User's Email Configuration#

Finally, create a backup of the user's email configuration file before making any changes. This ensures you can restore the configuration in case of errors.

Run the following command to back up the user's email configuration:

bash
mv -v /home/CPUSER/.cpanel/email_accounts.json{,-bak}

Explanation:

  • This command renames the email_accounts.json file to email_accounts.json.bak, creating a backup before any modifications are made.

Conclusion#

Following these steps will restore the email data for a specific cPanel user from the backup while ensuring that permissions, configurations, and file sizes are correctly handled. If you encounter any issues, you can restore the .bak backup files created during the process.