Automated cPanel Backup & Restore Guide with CLI

This method automatically detects all cPanel users, domains, and databases from the server without manually specifying usernames.


1. Generate Website Backups Automatically

Login to the old server via SSH as root.

Go to /home:

cd /home

Automatically detect all cPanel users and create compressed backups:

for file in /var/cpanel/users/*; do
    user=$(basename "$file")
    domain=$(grep "^DNS=" "$file" | cut -d= -f2)

    echo "Creating backup for $user -> $domain"

    tar -czf "/home/${domain}.tar.gz" "/home/${user}"
done

Generated backups:

/home/domain.com.tar.gz

Example:

/home/agrotechgn.com.tar.gz
/home/anafic.gn.tar.gz

2. Automatically Backup All cPanel MySQL Databases

Create backup directory:

mkdir -p /home/db-backups

Automatically dump all databases for all cPanel users:

for file in /var/cpanel/users/*; do
    user=$(basename "$file")

    echo "Processing databases for $user"

    mysql -e "SHOW DATABASES LIKE '${user}\_%';" | grep "^${user}_" | while read db
    do
        echo "Backing up database: $db"

        mysqldump --single-transaction --quick --lock-tables=false "$db" \
        | gzip > "/home/db-backups/${db}.sql.gz"
    done
done

Database backups location:

/home/db-backups/

3. Optional: Archive All SQL Backups

cd /home
tar -czf all-db-backups.tar.gz db-backups

4. Transfer Backups to the New Server

Using rsync:

rsync -avP /home/*.tar.gz root@NEW_SERVER_IP:/home/
rsync -avP /home/db-backups root@NEW_SERVER_IP:/home/

Or using scp:

scp /home/*.tar.gz root@NEW_SERVER_IP:/home/
scp -r /home/db-backups root@NEW_SERVER_IP:/home/

5. Automatically Get Username & Domain List

To generate a list of all cPanel users and domains:

for file in /var/cpanel/users/*; do
    user=$(basename "$file")
    domain=$(grep "^DNS=" "$file" | cut -d= -f2)

    echo "$user -> $domain"
done

Example output:

agrotechgncom -> agrotechgn.com
anaficgn -> anafic.gn

Save to a file:

for file in /var/cpanel/users/*; do
    user=$(basename "$file")
    domain=$(grep "^DNS=" "$file" | cut -d= -f2)

    echo "$user,$domain"
done > /root/cpanel-users.csv

6. Create Accounts on the New Server

In WHM:

WHM -> Account Functions -> Create a New Account

Create accounts using the same:

  • Username

  • Domain


7. Restore Website Files Automatically

On the new server:

cd /home

for archive in *.tar.gz; do
    echo "Extracting $archive"

    tar -xzf "$archive"
done

8. Automatically Fix Ownership

for file in /var/cpanel/users/*; do
    user=$(basename "$file")

    echo "Fixing ownership for $user"

    chown -R ${user}:${user} /home/${user}
done

9. Restore Databases Automatically

Restore all SQL backups:

for sql in /home/db-backups/*.sql.gz; do
    db=$(basename "$sql" .sql.gz)

    echo "Restoring $db"

    zcat "$sql" | mysql "$db"
done

10. Rebuild cPanel & Apache Cache

/scripts/updateuserdatacache
/scripts/rebuildhttpdconf
systemctl restart httpd

11. Verify Databases

mysql -e "SHOW DATABASES;"

12. Verify Account Ownership

for user in $(ls /var/cpanel/users); do
    grep "^DNS=" /var/cpanel/users/$user
done

13. Cleanup Backup Files

After successful migration:

rm -f /home/*.tar.gz
rm -rf /home/db-backups

Only remove backups after verifying that all websites and databases are functioning correctly.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Domain names are not showing in WHM

We were unable to see any domains listed under WHM -> List Accounts. However, we can see the...

How to Start/Stop or Restart Apache server

Apache is the HTTP server that is freely available over the internet. It is a kind of software or...

How to install Attracta SEO Tools plugin

RequirementscPanel/WHM needs to be installed on your server. Step 1: SSH into your server and go...

How to Install CpCleaner in cPanel through SSH?

Installation Run the following shell commands as root via SSH: wget -O cpc-1.0.3.tar...

How to Secure a cPanel/WHM Web Server

Here are a few basic steps that you should keep in mind for keeping a server secure. 1) Strong...