
Update note (November 2026): WordPress 7.1.5 is a security release with fixes backported to older branches. Take a full backup of your files and database before you update, then update as soon as possible.
Why Backing Up a WordPress Site is Essential
To back up WordPress properly, you need a copy of both the database and the site files. The official WordPress documentation on backups explains why: the database holds every post, comment and link on your site, and if it is erased or corrupted you could lose everything you have written. Hacks, failed updates, crashes and mistakes can all force a rollback.
This guide covers the files, the database, safe storage, automation and restore testing.
What a complete WordPress backup includes
- The database: posts, pages, comments, users and settings, stored in MySQL or MariaDB, not in the site folder.
- The files: WordPress core, themes, plugins, media uploads,
wp-config.phpand.htaccess.
WordPress recommends treating both as one backup set, made around the same time: export the database first, then copy the files.
How Often Should You Back Up WordPress?
The WordPress documentation suggests weekly backups for smaller sites and daily backups for busy ones. It also says to back up always before an upgrade or a move to a new host, and to keep at least 3 to 5 recent backups in different locations.
Treat every core, plugin and theme update the same way. Updates fix security issues, so install them promptly, but they can also break a site. Take a fresh backup, check that it is not empty, then click Update.
Follow the 3-2-1 Backup Rule
CISA, the US Cybersecurity and Infrastructure Security Agency, recommends the 3-2-1 backup rule: 3 copies of important files, on 2 different types of storage (such as a hard drive and the cloud), with 1 copy kept off-site.
For WordPress, that could be the live site, a cloud or backup-server copy, and a copy on an external drive. Keep at least one copy offline. CISA’s #StopRansomware Guide warns that ransomware actors try to delete or encrypt any backups they can reach, and that automated cloud sync alone may copy encrypted files over good ones.
How to Back Up WordPress Step by Step
These commands assume a Linux server with WordPress in /var/www/html. Adjust paths and names to suit.
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 it with mysqldump, using a dedicated backup user instead of the root account.
First, create a user that can only read the WordPress database. Run these commands in the MySQL shell and choose your own long, random password:
CREATE USER 'wp_backup'@'localhost' IDENTIFIED BY 'use-a-long-random-password';
GRANT SELECT, SHOW VIEW, TRIGGER ON wordpress_db.* TO 'wp_backup'@'localhost';
Then run the export. Replace wordpress_db with your database name. You will be prompted for the backup user’s password:
mysqldump -u wp_backup -p --single-transaction --no-tablespaces wordpress_db > wordpress-db-backup.sql
The privileges above are the minimum mysqldump needs for this command. Using –single-transaction avoids the need to lock tables on InnoDB tables, and –no-tablespaces avoids the need for the PROCESS privilege.
Step 3: Store Backups Securely
Never leave backup archives in the web root (/var/www/html). Anyone who guesses the file name might download your database and wp-config.php, which holds your database credentials. Keep backups outside the web root, readable only by your backup user.
Encrypt backups before they leave the server, as CISA recommends. GnuPG asks for a passphrase and creates an encrypted .gpg copy of each file:
gpg --symmetric --cipher-algo AES256 wordpress-files-backup.tar.gz
gpg --symmetric --cipher-algo AES256 wordpress-db-backup.sql
Keep the passphrase in a password manager, not on the server. Then copy the backups to a separate backup server or cloud storage:
scp wordpress-files-backup.tar.gz.gpg wordpress-db-backup.sql.gpg user@remote-server:/path/to/backup/
Step 4: Automate WordPress Backups
To make backups regular, set up a cron job. Never put the database password in the cron command: anyone who can list running processes may see it, and it is stored in plain text in your crontab. Store it in a protected option file instead.
Create the option file in the home directory of the user that runs the job, and restrict it so only that user can read it:
nano ~/.wp-backup.cnf
chmod 600 ~/.wp-backup.cnf
Put the following in the file:
[client]
user=wp_backup
password=use-a-long-random-password
Open the crontab editor with crontab -e (use sudo crontab -e if your user cannot read every file in /var/www/html). Add the following lines to back up the files and the database daily. Use the full path to the option file instead of ~, because the shell does not expand a ~ that follows an equals sign:
0 2 * * * tar -czvf /path/to/backup/wordpress-files-$(date +\%F).tar.gz /var/www/html
5 2 * * * mysqldump --defaults-file=/home/youruser/.wp-backup.cnf --single-transaction --no-tablespaces wordpress_db > /path/to/backup/wordpress-db-$(date +\%F).sql
The backslash before % is required, because cron treats an unescaped % as a newline. Backups contain user data, so also store them in a folder that only your backup user can read.
CISA advises being able to roll back at least seven days, so keep a few weeks of daily backups. Before adding a cleanup job, run it with -print to list what it would remove:
find /path/to/backup -name "wordpress-*" -mtime +30 -print
Restoring a WordPress Site
WordPress recommends restoring the files first, then the database. If database credentials changed, update wp-config.php to match. Follow these steps:
- Extract the WordPress files backup:
sudo tar -xzvf wordpress-files-backup.tar.gz -C / - Import the database backup using an account that is allowed to create and change tables:
mysql -u root -p wordpress_db < wordpress-db-backup.sql - Reset file permissions. WordPress recommends 755 for directories and 644 for files:
sudo find /var/www/html -type d -exec chmod 755 {} \;andsudo find /var/www/html -type f -exec chmod 644 {} \;. Set ownership withsudo chown -R www-data:www-data /var/www/html.
Test your restores
Test a restore on a staging site now and then. A backup you have never restored is not proven to work. CISA advises testing full and partial restores and documenting the steps.
- Restore the latest backup set to a staging server.
- Check that pages, media and the admin dashboard load.
- Note how long it took and fix any unclear step.
Should You Use a Backup Plugin or Your Host’s Backups?
You do not have to use the command line. WordPress documentation points to backup plugins in the Plugin Directory and to backup tools most hosts provide. It notes that getting a copy from your host can take time, and suggests an occasional manual backup to confirm automated ones work. Keep at least one copy outside your hosting account.
By following this guide, you can recover your WordPress site quickly when something goes wrong. For a more comprehensive guide on backing up an entire server, check out our Ubuntu Server Backup Guide.
References
- WordPress Advanced Administration Handbook: Backups
- WordPress Advanced Administration Handbook: Hardening WordPress (file permissions)
- CISA: Back Up Business Data (3-2-1 rule, encryption, restore testing)
- CISA: #StopRansomware Guide (offline, encrypted backups)
- MySQL 8.4 Reference Manual: mysqldump
- MySQL 8.4 Reference Manual: End-User Guidelines for Password Security
- crontab(5) manual page
Last verified: 21 November 2026.
