It is important to back up your WordPress site and the associated database, and it is often overlooked. Here is a way to back up your WordPress site and database in 3 simple steps, and no plugin required.
Most of the hosting company provides some sort of backup and storage services. However, if you are self-hosted, you would need setup a backup mechanism yourself.
If you google "how to backup WordPress site", you will be overwhelmed by various articles and so-called tutorials that suggesting using some sort of WordPress plugin. Most of the plugins are slow as they are running on PHP rather than relying on system level of services. They are also unnecessary bloated and not necessary reliable. This article is how I create a backup solution with a simple bash script and a crontab setup.
Step 1 - Create a backup bash script
First, create a simple bash script at
/home/user/ directory that do two things:
- Compressed the entire site using
tarand save a copy;
- Execute an
mysqldumpof the WordPress database.
tar.gz file and the
database.sql will be saved with timestamp appended to the filename. Create the bash script using your preferred editor and named it as
#!/bin/bash # Backup my website data #START tar -czf /home/user/wordpress-$(date +%Y%m%d).tar.gz /var/www/html/* mysqldump -u username -ppassword database_name > database_name_$(date +'%Y%m%d').sql #END
Remember to replace username and password with your actual database access credentials, and replace database_name with actual database name you used. Your /home/user/ with the actual user directory you used (e.g. in my case it will be /home/hcheung as hcheung is the name I used for login into the server).
Save the file and make the file executable:
chmod +x /home/user/backupmysite.sh
You may or may not necessary to change the ownership of the file depend on your user permission.
You can execute
/home/user/backupmysite.sh to ensure the script will be run properly. We now need to setup the schedule so that it will backup the data automatically according to a preset schedule.
Step 2 - Setup a cron entry
Cron is a utility tool for configuring scheduled tasks on Unix systems. It setup schedules to run commands or scripts periodically and at fixed intervals. Run the following command to edit the cron table:
The first time you run crontab you'll be prompted to select a text editor; choose
nano by pressing Enter.
A cron entry consists of six parts: minute, hour, day of month, month of year, day of week, and the command to be executed.
# m h dom mon dow command # * * * * * command to execute # ┬ ┬ ┬ ┬ ┬ # │ │ │ │ │ # │ │ │ │ │ # │ │ │ │ └───── day of week (0 - 7) (0 to 6 are Sunday to Saturday, Sunday is 0 or 7) # │ │ │ └────────── month (1 - 12) # │ │ └─────────────── day of month (1 - 31) # │ └──────────────────── hour (0 - 23) # └───────────────────────── min (0 - 59)
Move the cursor to the end of the file to input a cron entry as show below:
0 3 * * 0 /home/user/backupmysite.sh
This cron entry would run the
/home/user/backupmysite.sh script every Sunday at 03:00. You can adjust the frequency of backup based on your needs, but once a week is sufficient for my site. Save the file and quit the editor.
Tips: For testing purpose, you could use this cron entry
*/5 * * * * /home/user/backupmysite.shas it will run the script every 5 minutes before you change it to the longer permanent weekly schedule
You can view your currently saved schedule with:
Step 3 - Download the backup to another computer
By now you have a working backup solution that run once a week and save the files at
/home/user/ directory. You could download the backup files regularly (weekly, monthly at your wish) to your local computer using any SFTP or FTP client.
Here you have it, a simple WordPress backup solution in 3 steps.