Backing up your WordPress site is one of the most crucial tasks for maintaining your site. With WP-CLI (WordPress Command Line Interface), the process becomes faster and more reliable, particularly for advanced users or those handling numerous websites.
In this guide, you will learn how to back up both your WordPress database and site files using WP-CLI commands, along with the steps to automate regular backups.
Table of Contents:
Why Use WP-CLI for WordPress Backups?
WP-CLI offers a command-line method to manage your WordPress installation without using the web dashboard.
It allows faster backups, automation through cron jobs, and smooth integration with scripts, ideal for system admins and developers who prefer terminal efficiency.
Steps to back up your WordPress site
Step 1: Back Up the WordPress Database
- First, to export your WordPress database, execute the following command:
wp db export backup.sql
- This command creates a file named ‘backup.sql’ in your current working directory.
- To specify a custom path or filename, use:
wp db export /path/to/backup/your-db.sql
Tip: Include the date in your filename for easier version tracking.
Step 2: Back Up WordPress Files
- Now, to archive your entire WordPress directory, use the ‘tar’ command as given below:
tar -czf wordpress-files-backup.tar.gz /path/to/your/wordpress/
- Replace ‘/path/to/your/wordpress/’ with the actual path of your WordPress installation.
- This command creates a compressed ‘.tar.gz’ file containing your themes, plugins, uploads, and core files.
Step 3: Automate Regular Backups with a Script
- You can combine the database and file backup commands into a single shell script, using the following command:
#!/bin/bash cd /path/to/your/wordpress/ wp db export db-$(date +%F).sql tar -czf wp-backup-$(date +%F).tar.gz . db-$(date +%F).sql rm db-$(date +%F).sql
- Save this script and schedule it through a cron job to automate backups at regular intervals.
Best Practices for Backup Management
- Schedule regular backups using cron to ensure up-to-date data recovery points.
- Store backups offsite, such as on cloud storage or a remote server.
- Test restore procedures periodically to confirm backup integrity.
- Use secure permissions to prevent unauthorised access to backup files.
Conclusion
This way, you can easily back up WordPress using WP-CLI. It guarantees speed, control, and automation, making it a good choice for technical users and hosting professionals. Regular, tested backups are your best defence against data loss or unexpected site issues.
Need a full site backup? Learn How to back up a MySQL database in cPanel