How to backup a WordPress site via SSH

To make a complete local backup of a remote WordPress install over SSH, you need to perform two distinct steps:

  1. Download the files (using rsync).
  2. Export and download the database (using mysqldump piped through ssh).

Here is the step-by-step guide.

Prerequisites

  • SSH Access: You need the username, host (IP or domain), and password (or SSH key) for the remote server.
  • WordPress Path: You need to know where WordPress is installed (e.g., /var/www/html or /home/user/public_html).
  • Database Credentials: You will need the database name, user, and password. (I will show you how to find these below).

Step 1: Find Database Credentials (Optional)

If you don't know your database login details, run this command to read them from your remote wp-config.php file: ssh user@remote-server.com "cat /path/to/wordpress/wp-config.php" | grep 'DB_'

Note the DB_NAME, DB_USER, and DB_PASSWORD for Step 2.


Step 2: Download the Database (SQL Dump)

You can run mysqldump on the remote server and pipe the output directly to a file on your local machine. This avoids creating a large temporary file on the remote server.

Run this command on your local terminal: ssh user@remote-server.com "mysqldump -u DB_USER -p'DB_PASSWORD' DB_NAME" > local_backup.sql

  • Important: There is no space between -p and the password (e.g., -p'mypassword').
  • Security Note: If you prefer not to type the password in the command (so it doesn't show in history), use -p without the password. SSH will try to prompt you, but this often fails without a pseudo-terminal. A safer alternative is to rely on the remote user's .my.cnf if configured, or just be aware of the history risk.

Step 3: Download the WordPress Files

Use rsync to download the entire directory. It is faster and more reliable than FTP.

Run this command on your local terminal: rsync -avzP user@remote-server.com:/path/to/wordpress/ ./local_folder/
Flag Explanation:

  • -a: Archive mode (preserves permissions, owners, dates).
  • -v: Verbose (shows you what is happening).
  • -z: Compresses data during transfer (saves bandwidth).
  • -P: Shows progress bar and allows resuming interrupted transfers.

Script

If you do this frequently, you can save this script as backup_wp.sh. It automatically extracts the credentials from wp-config.php so you don't have to look them up.

#!/bin/bash

# Configuration - FILL THESE IN YOURSELF
REMOTE_USER="user"
REMOTE_HOST="remote-server.com"
REMOTE_PATH="/remote/server/path/to/wordpress/site/folder" 
#that's the folder that contains wp-config.php
LOCAL_BACKUP_DIR="/path/to/local/backup/folder"

mkdir -p "$LOCAL_BACKUP_DIR"

echo "Extracting DB credentials..."
# Read wp-config.php remotely to get DB details
DB_NAME=$(ssh $REMOTE_USER@$REMOTE_HOST "grep DB_NAME $REMOTE_PATH/wp-config.php | cut -d \"'\" -f 4")
DB_USER=$(ssh $REMOTE_USER@$REMOTE_HOST "grep DB_USER $REMOTE_PATH/wp-config.php | cut -d \"'\" -f 4")
DB_PASS=$(ssh $REMOTE_USER@$REMOTE_HOST "grep DB_PASSWORD $REMOTE_PATH/wp-config.php | cut -d \"'\" -f 4")

echo "Backing up Database: $DB_NAME..."
ssh $REMOTE_USER@$REMOTE_HOST "mysqldump -u $DB_USER -p'$DB_PASS' $DB_NAME" > "$LOCAL_BACKUP_DIR/db_backup.sql"

echo "Syncing Files..."
rsync -avzP --exclude 'wp-content/cache' $REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH/ "$LOCAL_BACKUP_DIR/files/"

echo "Backup Complete!"