Automated WordPress Backups on Ubuntu
A website without a backup is a disaster waiting to happen. Hardware can fail, updates can go wrong, and security breaches can occur. An automated backup is your ultimate safety net, allowing you to restore your site to a working state in minutes.
This guide will walk you through creating a Bash script to back up your WordPress files and database, and then automating it with cron.
1. The Two Components of a WordPress Backup
A complete WordPress backup consists of two distinct parts:
- The Files: This includes your themes, plugins, and uploaded media (images, videos). These are located in
/var/www/html. - The Database: This is where your posts, pages, comments, and user data are stored.
Our script will handle both.
2. The Backup Script
This script will perform three actions: dump the database, archive the files, and clean up old backups to save space.
-
Create a script file:
sudo nano /usr/local/bin/backup_wp.sh -
Paste the following code:
- Crucial: Replace
your_strong_passwordwith the actual database password you created in the WordPress installation guide.
#!/bin/bash # --- Configuration --- DB_NAME="wordpress" DB_USER="wp_user" DB_PASS="your_strong_password" # <-- IMPORTANT: Use your real password WP_PATH="/var/www/html" BACKUP_DIR="/var/backups/wordpress" DATE=$(date +"%Y-%m-%d_%H%M%S") # --- Script Logic --- echo "Starting WordPress backup..." # 1. Ensure backup directory exists mkdir -p $BACKUP_DIR # 2. Dump the MySQL database echo " [1/3] Dumping database..." mysqldump -u $DB_USER -p$DB_PASS $DB_NAME > $BACKUP_DIR/db_backup_$DATE.sql # 3. Archive the WordPress files echo " [2/3] Archiving files..." tar -czf $BACKUP_DIR/files_backup_$DATE.tar.gz $WP_PATH # 4. Clean up backups older than 7 days echo " [3/3] Cleaning up old backups..." find $BACKUP_DIR -type f -mtime +7 -name '*.sql' -delete find $BACKUP_DIR -type f -mtime +7 -name '*.tar.gz' -delete echo "Backup complete! Files saved in $BACKUP_DIR" - Crucial: Replace
-
Make the script executable:
sudo chmod +x /usr/local/bin/backup_wp.sh
You can now run your first backup manually: sudo /usr/local/bin/backup_wp.sh
3. Automating with Cron
cron is a time-based job scheduler in Linux. We'll tell it to run our script automatically every night.
-
Open the root crontab:
sudo crontab -e(If prompted, choose
nanoas your editor). -
Add the schedule: Go to the bottom of the file and add this line. It tells
cronto run the script every day at 3:00 AM.0 3 * * * /usr/local/bin/backup_wp.sh -
Save and exit (
Ctrl+O,Enter,Ctrl+X). Your backups are now automated.
4. Restoring From a Backup
If the worst happens, here is how you restore your site:
- Find your latest backup files in
/var/backups/wordpress. - Restore the files:
# First, clear the old site files (be careful!) sudo rm -rf /var/www/html/* # Then, extract the backup sudo tar -xzf /var/backups/wordpress/files_backup_YYYY-MM-DD_HHMMSS.tar.gz -C / - Restore the database:
mysql -u wp_user -p wordpress < /var/backups/wordpress/db_backup_YYYY-MM-DD_HHMMSS.sql - Fix Permissions: After restoring, always reset the file permissions.
- Reference: Securing a WordPress Installation
5. Pro Tip: Off-Site Backups
For true disaster recovery, you should store a copy of your backups on a different machine or in the cloud. Tools like rclone can be added to your script to sync the /var/backups/wordpress directory with services like Google Drive, Dropbox, or Backblaze B2.