WordPress

How to Create a Backup of Your WordPress Website in cPanel using Terminal

Working on WordPress locally before deploying changes to your live site is a best practice for safe and efficient development. Whether you’re testing new themes, debugging plugins, or experimenting with design changes, having a local copy of your WordPress site ensures you can work without affecting your live visitors.

If you’re on one of Ecenica’s Managed WordPress Hosting plans, or Ecenica Shared Hosting and Managed VPS plans, you already benefit from automated daily off-server backups for disaster recovery. Those backups protect your live site and can be restored through your Ecenica Dashboard.

This guide shows you how to create a complete exportable copy of your live WordPress site specifically for local development. The script explained below allows you to:

  • Export your entire WordPress installation (files + database) in minutes
  • Choose between a FULL export (with all uploads) or SMALL export (faster, excludes large media files)
  • Download a single compressed archive via cPanel File Manager
  • Import into your local WordPress development environment (XAMPP, Local by Flywheel, MAMP, etc.)
  • Test changes safely before deploying to your live Ecenica Managed WordPress Hosting site

The export is stored in a secure, private folder and uses maximum compression to save space and reduce download time. Once downloaded, you’ll have everything needed to replicate your live WordPress site locally for development and testing.

What This Script Does

This automated bash script creates a complete snapshot of your WordPress site by:

  1. Extracting database credentials from your wp-config.php file
  2. Exporting your WordPress database with all content, posts, settings, users, and configuration
  3. Archiving your WordPress files (themes, plugins, uploads, core files, and custom code)
  4. Offering two export options: FULL (complete replica) or SMALL (excludes wp-content/uploads for faster downloads)
  5. Compressing everything into a single .tar.gz file using maximum compression
  6. Storing securely in a private folder (_private_backups) that’s inaccessible via web browsers

After running the script, you can download the export file through cPanel File Manager and extract it in your local WordPress development setup. Your live site on Ecenica’s hosting remains completely unchanged throughout the process.

Prerequisites

Before starting, you will need:

  • A WordPress website already installed and running on your Ecenica hosting account
  • A Ecenica hosting plan with SSH access enabled (check your plan details to confirm SSH availability)
  • Basic familiarity with SSH and command-line usage
  • Access to cPanel or an equivalent hosting control panel
  • Sufficient free disk space in your hosting account to store the backup files. As a guide, you should have at least the same amount of free space as the total size of your WordPress website and its database combined.

Step 1: Create a Private Backup Folder

Your backup files should be kept in a location that is not publicly accessible through the web. This prevents anyone from downloading your backups directly.

  1. Log in to your cPanel account.
  2. Open File Manager.
  3. Navigate to your home directory (for example, /home/username/).
  4. Click + Folder and create a folder called _private_backups.

This folder will store your backup securely, as it is outside the public_html directory.

Step 2: Create the Backup Script

  1. Open File Manager or connect to your hosting account using SSH.
  2. In your home directory, click + File and name it wp_full_backup.sh.
  3. Paste the following script into the file.
#!/bin/bash

# ============================================================================
# WordPress Site Export Script for Local Development
# ============================================================================
# This script creates a complete copy of your live WordPress site for local
# development. It packages both your WordPress files and database into a single
# compressed archive that you can download and import into your local environment.
#
# Ideal for: Local WordPress development, staging environments, testing changes
# before deploying to your live Ecenica Managed WordPress Hosting site.
#
# What this script does:
# 1. Exports your WordPress database with all content, settings, and data
# 2. Archives your WordPress files (themes, plugins, uploads, core files)
# 3. Gives you two options: FULL export or SMALL export (excludes large uploads)
# 4. Creates a single compressed .tar.gz file you can download via cPanel
# 5. Stores the export in a private folder (_private_backups) for security
#
# After running this script, download the generated .tar.gz file from
# ~/private_backups/ and extract it in your local WordPress development setup.
# ============================================================================

# === Self-chmod to ensure script is executable ===
SCRIPT_PATH="${BASH_SOURCE[0]}"
if [[ -f "$SCRIPT_PATH" ]]; then
  chmod 700 "$SCRIPT_PATH" 2>/dev/null
fi

# === CONFIGURATION ===
USER_HOME="$HOME"
WP_PATH="$USER_HOME/public_html"
BACKUP_PATH="$USER_HOME/_private_backups"
mkdir -p "$BACKUP_PATH"
DATE=$(date +"%Y-%m-%d_%H-%M-%S")
BACKUP_FILE="wp_backup_${DATE}.tar"

# === Get DB credentials ===
WP_CONFIG="$WP_PATH/wp-config.php"
DB_NAME=$(grep DB_NAME "$WP_CONFIG" | cut -d "'" -f 4)
DB_USER=$(grep DB_USER "$WP_CONFIG" | cut -d "'" -f 4)
DB_PASS=$(grep DB_PASSWORD "$WP_CONFIG" | cut -d "'" -f 4)
DB_HOST=$(grep DB_HOST "$WP_CONFIG" | cut -d "'" -f 4)

echo "=== WordPress Site Export for Local Development ==="
echo "=== User: $(whoami) | Host: Ecenica Managed WordPress Hosting ==="
echo ""
echo "Select export type:"
echo "1) FULL export (complete site including all uploads - best for exact replica)"
echo "2) SMALL export (excludes wp-content/uploads - faster download for development)"
echo ""
read -p "Enter choice (1 or 2): " BACKUP_TYPE

# Validate input
if [[ "$BACKUP_TYPE" != "1" && "$BACKUP_TYPE" != "2" ]]; then
  echo "Invalid choice. Please enter 1 or 2."
  exit 1
fi

if [[ "$BACKUP_TYPE" == "1" ]]; then
  echo "✓ Selected: FULL export (complete WordPress site)"
  BACKUP_FILE="wp_export_FULL_${DATE}.tar"
else
  echo "✓ Selected: SMALL export (WordPress core + themes + plugins, no uploads)"
  BACKUP_FILE="wp_export_SMALL_${DATE}.tar"
fi

# === Check space ===
SITE_SIZE_MB=$(du -sm "$WP_PATH" | awk '{print $1}')
DB_SIZE_MB=$(mysql -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASS" -e \
  "SELECT CEIL(SUM(data_length + index_length)/1024/1024) FROM information_schema.tables WHERE table_schema='${DB_NAME}';" \
  2>/dev/null | tail -n1)
if [[ -z "$DB_SIZE_MB" ]]; then DB_SIZE_MB=100; fi
REQUIRED_MB=$(( ($SITE_SIZE_MB + $DB_SIZE_MB) * 120 / 100 ))
AVAILABLE_MB=$(df -Pm "$BACKUP_PATH" | awk 'NR==2 {print $4}')

if (( AVAILABLE_MB < REQUIRED_MB )); then
  echo "Not enough free space. Need ${REQUIRED_MB}MB, have ${AVAILABLE_MB}MB"
  exit 1
fi

# === Step 1: Dump DB ===
DB_DUMP_FILE="db_${DB_NAME}_${DATE}.sql.gz"
echo "Dumping database..."
mysqldump -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" \
  --single-transaction --quick --lock-tables=false | gzip -9 > "$BACKUP_PATH/$DB_DUMP_FILE" || {
  echo "Database dump failed"; exit 1; }

# === Step 1: Dump DB (Auto-detect charset and compress) ===
DB_DUMP_FILE="db_${DB_NAME}_${DATE}.sql.gz"

# Detect charset from MySQL
DB_CHARSET=$(mysql -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASS" -Nse \
  "SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name='${DB_NAME}';" 2>/dev/null)

# Fallback if detection fails
if [[ -z "$DB_CHARSET" ]]; then
  DB_CHARSET="utf8mb4"
fi

# === Step 1: Dump DB (Auto-detect charset and compress safely) ===
DB_DUMP_FILE="db_${DB_NAME}_${DATE}.sql.gz"

# Detect charset from MySQL
DB_CHARSET=$(mysql -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASS" -Nse \
  "SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name='${DB_NAME}';" 2>/dev/null)

# Fallback if detection fails
if [[ -z "$DB_CHARSET" ]]; then
  DB_CHARSET="utf8mb4"
fi

echo "──────────────────────────────────────────────"
echo "Dumping database (charset: $DB_CHARSET, compressed)..."
echo "This will NOT lock or freeze your live site."
echo "──────────────────────────────────────────────"

# Perform the dump safely
mysqldump -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" \
  --default-character-set="$DB_CHARSET" \
  --single-transaction --quick --lock-tables=false \
  --routines --events --triggers \
  | gzip -9 > "$BACKUP_PATH/$DB_DUMP_FILE"

# Check result
if [[ $? -ne 0 ]]; then
  echo "❌ Database dump failed"
  exit 1
else
  echo "✅ Database dump complete: $BACKUP_PATH/$DB_DUMP_FILE"
fi

# === Step 2: Create TAR of WordPress files ===
echo "Exporting WordPress files from your Ecenica hosting..."

if [[ "$BACKUP_TYPE" == "1" ]]; then
  # FULL export - complete site for exact local replica
  tar --exclude='cache/*' --exclude='*.log' \
      -cvf "$BACKUP_PATH/$BACKUP_FILE" -C "$WP_PATH" . || {
      echo "Export failed"; exit 1; }
else
  # SMALL export - WordPress development essentials without large uploads
  tar --exclude='wp-content/uploads/*' \
      --exclude='wp-content/backup*' \
      --exclude='wp-content/backups/*' \
      --exclude='*.zip' \
      --exclude='*.tar.gz' \
      --exclude='*.sql' \
      --exclude='*.sql.gz' \
      --exclude='node_modules/*' \
      --exclude='cache/*' \
      --exclude='*.log' \
      -cvf "$BACKUP_PATH/$BACKUP_FILE" -C "$WP_PATH" . || {
      echo "Export failed"; exit 1; }
fi

# === Step 3: Add DB dump ===
tar -rf "$BACKUP_PATH/$BACKUP_FILE" -C "$BACKUP_PATH" "$DB_DUMP_FILE"

# === Step 4: Compress final TAR ===
echo "Compressing archive..."
gzip -9 "$BACKUP_PATH/$BACKUP_FILE"

# === Step 5: Cleanup ===
rm -f "$BACKUP_PATH/$DB_DUMP_FILE"

FINAL_FILE="$BACKUP_PATH/${BACKUP_FILE}.gz"
echo "✅ WordPress export complete! File saved to: $FINAL_FILE"
echo "📦 Export size: $(du -h "$FINAL_FILE" | awk '{print $1}')"
echo ""
echo "Next steps for local WordPress development:"
echo "1. Download this file via cPanel File Manager from: _private_backups/"
echo "2. Extract the .tar.gz file in your local WordPress directory"
echo "3. Import the .sql.gz database file into your local MySQL/MariaDB"
echo "4. Update wp-config.php with your local database credentials"
echo "5. Search-replace your live domain with localhost in the database"
echo "6. Delete this file via cPanel File Manager from: _private_backups/"
echo ""
echo "Your Ecenica Managed WordPress Hosting site remains unchanged."
  1. Save the file and close the editor.

This ensures that only your user can run the script.

Step 3: Run the Backup Script

Run the script manually using:

./wp_full_backup.sh

The process may take several minutes, depending on the size of your website. Once completed, the backup will be saved in your _private_backups folder. You can download it safely using cPanel’s File Manager or SFTP.

Was this guide helpful?

Need help with this?

Open a ticket and link to this guide so our team can see what you have tried.

View support tickets

Power your business with Ecenica Hosting

Built for WordPress and serious websites. Fast, secure and supported by real people in the UK.

Ecenica hosting services represented by a connected red route across London