Ensuring the safety and continuity of your WordPress website is critical. One of the best ways to achieve this is by creating a complete local backup that mirrors your site. A backup includes the website files (themes, plugins, uploads, etc.) and the database, which stores content and configurations. This guide walks you through the process of backing up your WordPress website and creating a local mirror.
Why Create a Local Backup?
- Disaster Recovery: Quickly restore your site if it gets hacked or corrupted.
- Testing Environment: Experiment with updates, plugins, and themes locally before applying changes to the live site.
- Peace of Mind: A reliable backup ensures you can recover your website anytime.
1. Backing Up WordPress Files
The first step is to back up the files that power your website.
Option A: Using wget
wget
is a powerful command-line tool for downloading website files. To create a local mirror of your website:
- Open your terminal or Termux app.
- Run the following command:
wget --mirror --convert-links --adjust-extension --page-requisites --no-parent https://example.com
--mirror:
Enables recursive downloads and timestamps.--convert-links:
Modifies the links to make the site functional offline.--adjust-extension:
Ensures files have the proper extensions (e.g., .html).--page-requisites:
Downloads all necessary resources, such as images, CSS, and JavaScript.--no-parent:
Prevents wget from downloading files outside the target directory.
The mirrored website will be saved in a folder named after your domain, e.g., example.com
.
Note: This method only backs up the static content. Dynamic WordPress functionality (posts, comments, user data, etc.) requires the database.
2. Backing Up the WordPress Database
The database is the heart of your WordPress website. It stores all the content, user accounts, settings, and more.
Step 1: Access Your Server via SSH
Use SSH to log into your server:
ssh user@your-server-ip
Step 2: Locate Your Database Credentials
Your database credentials are stored in the wp-config.php
file:
nano /path/to/your/wordpress/wp-config.php
Look for the following lines:
define('DB_NAME', 'your_database_name');
define('DB_USER', 'your_database_user');
define('DB_PASSWORD', 'your_database_password');
define('DB_HOST', 'localhost');
Step 3: Export the Database
Run the following command to create a database dump:
mysqldump -u your_database_user -p your_database_name > wordpress_backup.sql
-u:
Specifies the database username.-p:
Prompts for the database password.wordpress_backup.sql:
The output file containing your database backup.
Step 4: Download the Backup
To download the database backup to your local system:
scp user@your-server-ip:/path/to/wordpress_backup.sql .
3. Combining Files and Database
Once you have the website files and database, you can recreate your site locally.
Step 1: Set Up a Local Server
Install a local server environment like:
- XAMPP: Windows, macOS, Linux
- MAMP: macOS, Windows
- LAMP: Linux
Step 2: Place Files in the Web Server Directory
Move the downloaded files to the appropriate folder:
- For XAMPP:
/xampp/htdocs/example.com
- For LAMP:
/var/www/html/example.com
Step 3: Import the Database
Import the database into your local MySQL server:
mysql -u your_local_user -p your_local_database_name < wordpress_backup.sql
Step 4: Update wp-config.php
Edit the wp-config.php
file to use your local database credentials:
define('DB_NAME', 'your_local_database_name');
define('DB_USER', 'your_local_user');
define('DB_PASSWORD', 'your_local_password');
define('DB_HOST', 'localhost');
4. Automating Backups
To avoid manual work, automate your backups with a script:
Backup Script Example
Create a shell script, backup_wordpress.sh
:
#!/bin/bash
DATE=$(date +%F)
BACKUP_DIR="/path/to/backup"
DB_USER="your_database_user"
DB_PASS="your_database_password"
DB_NAME="your_database_name"
# Backup files
tar -czf $BACKUP_DIR/wordpress-files-$DATE.tar.gz /path/to/wordpress/
# Backup database
mysqldump -u $DB_USER -p$DB_PASS $DB_NAME > $BACKUP_DIR/wordpress-db-$DATE.sql
Schedule Backups with cron
- Open the crontab editor:
crontab -e
- Add this line to schedule daily backups at 2 AM:
0 2 * * * /path/to/backup_wordpress.sh
5. Using Plugins for Backups
For users who prefer GUI-based solutions, WordPress plugins can simplify the process.
Recommended Plugins
- UpdraftPlus: Backs up files and databases. Supports cloud storage like Google Drive, Dropbox, etc.
- Duplicator: Creates a full site backup. Easy to use for migration and restoration.
- All-in-One WP Migration: Exports both files and database in a single file.
6. Testing Your Backup
To ensure your backup is valid:
- Verify the files and database dump.
- Set up a local test environment.
- Import the files and database.
- Confirm that your website works as expected.
7. Advanced Tools for Backups
For additional flexibility, consider these tools:
- rsync: For syncing files between local and remote systems.
- scp: Securely copies files between systems.
- tar: Archives files and compresses them.
Example of syncing with rsync:
rsync -avz user@your-server-ip:/path/to/wordpress/ /local/path/to/wordpress/
Conclusion
Creating a local mirror and backup of your WordPress website ensures you’re prepared for any issues. Whether you use command-line tools, automation, or plugins, the key is consistency. Set up regular backups and test them frequently to keep your site secure and functional.
Start backing up your WordPress site today to safeguard your hard work!