
Why Backing Up a WordPress Site is Essential
Backing up a WordPress site is critical to protect your website from data loss caused by hacking, server crashes, or accidental deletions. Imagine this: a company I worked with lost their entire WordPress site after a ransomware attack. They had no backup, which resulted in months of lost content and a significant hit to their business. A proper backup plan could have saved them from this disaster.
In this guide, we’ll walk you through how to backup a WordPress site, including its files and database, so you can restore it quickly if needed.
Step 1: Backup WordPress Files
WordPress files include themes, plugins, and media uploads, typically stored in /var/www/html
. Use the following command to back them up:
sudo tar -czvf wordpress-files-backup.tar.gz /var/www/html
This creates a compressed file named wordpress-files-backup.tar.gz
.
Step 2: Backup the WordPress Database
WordPress uses a MySQL or MariaDB database to store content and settings. Export the database using mysqldump
:
sudo mysqldump -u root -p wordpress_db > wordpress-db-backup.sql
Replace wordpress_db
with your database name. You’ll be prompted for the database root password.
Step 3: Store Backups Securely
Transfer the backup files to a secure location, such as an external drive or cloud storage:
scp wordpress-files-backup.tar.gz wordpress-db-backup.sql user@remote-server:/path/to/backup/
Step 4: Automate WordPress Backups
To ensure regular backups, set up a cron job:
- Open the crontab editor:
crontab -e
- Add the following lines to back up files and the database daily:
0 2 * * * tar -czvf /path/to/backup/wordpress-files-$(date +\%F).tar.gz /var/www/html 0 2 * * * mysqldump -u root -p'yourpassword' wordpress_db > /path/to/backup/wordpress-db-$(date +\%F).sql
Restoring a WordPress Site
To restore your WordPress site, follow these steps:
- Extract the WordPress files backup:
sudo tar -xzvf wordpress-files-backup.tar.gz -C /
- Import the database backup:
mysql -u root -p wordpress_db < wordpress-db-backup.sql
- Adjust file permissions:
sudo chown -R www-data:www-data /var/www/html sudo chmod -R 755 /var/www/html
By following this guide, you can ensure your WordPress site is always protected. For a more comprehensive guide on backing up an entire server, check out our Ubuntu Server Backup Guide.
Leave a Reply