Backup Both WordPress Database plus Site using simple Bash script.
After a fresh restore and restarting my blog from scratch, one thing became crystal clear: while there are countless WordPress backup plugins out there, many can leave you in a lurch when you truly need them most – during a critical restoration. I’ve been down that road, and it’s frustrating.
That’s why I’ve come to appreciate the power of a basic, yet incredibly effective, backup script. This isn’t about fancy interfaces or complex setups; it’s about pure, reliable functionality that ensures your WordPress site and its precious MySQL database are safe and sound.
Why a Script Over Plugins?
I’ve tested a fair share of WordPress backup plugins. While they offer convenience, some fall short when it comes to the crunch – the moment you actually need to restore your entire site and database. This simple script, however, provides a direct, low-level backup that can be a lifesaver.
My Simple Solution: Cron + CLI Access
The magic lies in scheduling a custom script via crontab
on your server. This approach requires direct access to your server’s command-line interface (CLI) – a common capability if you’re hosting your blog on a Virtual Private Server (VPS), as I do.
This little script performs two essential tasks:
- Backs up your entire WordPress site files.
- Dumps your MySQL database.
- Automatically prunes old backups, keeping your storage clean by deleting anything older than 5 days.
It’s efficient, hands-off, and remarkably effective.
Prerequisites
To use this script, you’ll need:
- CLI Access: You need shell access to your server (e.g., via SSH).
- Standard Tools:
mysqldump
,tar
,cat
,grep
,cut
, andfind
should be available on your server (which they almost certainly are). - Specific Folder Structure: The script assumes your WordPress installation is located at
/var/www/<your.site.com>/public_html
and that your backups will be stored in/var/www/<your.site.com>/backups.
You can adjust theSRC_PATH
variable if your setup differs.
The Backup Script Snippet
You can copy and modify this code snippet to fit your specific setup. Just remember to replace the placeholder values with your actual database credentials and paths.
##################################################################
# Backup WordPress site both database and public_html folder.
# Execute instruction: <filename.sh> <www.mysite.com>
# Default Folders:
# /var/www/<www.mysite.com>/public_html
# /var/www/<www.mysite.com>/backups
#
# Change only SRC_PATH env variable as per respective root location.
#
# Author: Saumya Padhi (sa**********@*****ok.com, ad***@*********hi.com)
# LinkedIn: www.linkedin.com/in/saumyap
#
# Crontab Example:
# 00 12 * * * /bin/sh /scripts/backup_mysite.sh saumyapadhi.com >> /dev/null
#
##################################################################
#!/bin/sh
# The site domain is passed as the first argument to the script
SITE=$1
# Define the source path for your WordPress installation
# Adjust this variable if your WordPress root is different
SRC_PATH=/var/www/$SITE
# Extract database credentials from wp-config.php
DBNAME=`cat $SRC_PATH/public_html/wp-config.php | grep DB_NAME | cut -d \' -f 4`
DBUSER=`cat $SRC_PATH/public_html/wp-config.php | grep DB_USER | cut -d \' -f 4`
DBPASS=`cat $SRC_PATH/public_html/wp-config.php | grep DB_PASSWORD | cut -d \' -f 4`
DBHOST=`cat $SRC_PATH/public_html/wp-config.php | grep DB_HOST | cut -d \' -f 4`
# Define backup file names with date stamp
DBBACKUP=$SRC_PATH/backups/$SITE-DB-`date "+%Y-%m-%d"`.sql
SITEBACKUP=$SRC_PATH/backups/$SITE-site-`date "+%Y-%m-%d"`.tar.gz
L0BACKUP=$SRC_PATH/backups/FULL-$SITE-`date "+%Y-%m-%d"`.tar.gz
# Ensure the backup directory exists
mkdir -p $SRC_PATH/backups
# 1. Perform MySQL Database Backup
# Uses mysqldump to export the database.
/usr/bin/mysqldump -u $DBUSER -p$DBPASS $DBNAME > $DBBACKUP
# 2. Perform WordPress Site Files Backup
# Compresses the public_html folder.
/bin/tar -zcvf $SITEBACKUP $SRC_PATH/public_html
# 3. Create a Full Combined Backup and Clean Up Individuals
# Creates a single combined tar.gz archive of both the site and DB backups.
# Crucially, the --remove-files flag deletes the individual DBBACKUP and SITEBACKUP
# files AFTER they have been successfully added to the L0BACKUP archive.
/bin/tar -zcvf $L0BACKUP $SITEBACKUP $DBBACKUP --remove-files
# 4. Delete Old Full Backups
# Finds and removes full backups older than 8 days from the backup directory.
find $SRC_PATH/backups/ -name "FULL-${SITE}*" -mtime +8 -exec rm {} \;
How to Implement and Schedule Your Backup
- Save the Script: Save the code above into a file, for example,
backup_mysite.sh
, in a secure location on your server (e.g.,/scripts/
or a dedicated~/scripts/
directory). - Make it Executable: Grant execution permissions to the script:
chmod +x /scripts/backup_mysite.sh
(adjust path as needed). - Run Manually (for testing): You can test the script by running it manually from your CLI:
/scripts/backup_mysite.sh your.site.com
Replaceyour.site.com
with your actual domain name. - Schedule with Crontab: To automate the process, schedule it using a cron job. Open your crontab for editing:
crontab -e
Then, add the following line (for example, to run daily at 12:00 PM):00 12 * * * /bin/sh /scripts/backup_mysite.sh saumyapadhi.com >> /dev/null
00 12 * * *
: This sets the schedule to run at 12:00 PM every day./bin/sh /scripts/backup_mysite.sh saumyapadhi.com
: This is the command that executes your script, passingsaumyapadhi.com
as the site argument. Remember to changesaumyapadhi.com
to your actual domain!>> /dev/null
: This redirects all standard output and errors from the script to/dev/null
, preventing cron from sending you emails for every successful backup (which can quickly fill your inbox!).
Supercharge Your WordPress Backups: An Automated Script for Seamless Protection
When it comes to WordPress, having a robust backup strategy isn’t just a good idea – it’s absolutely essential. While many plugins offer backup solutions, sometimes the most reliable method is a straightforward script that gives you full control and peace of mind.
I’ve put together a lean, mean, backup machine in the form of a shell script that handles both your WordPress files and your MySQL database, automatically, with just a few lines of code. This is perfect for those running their sites on a VPS or dedicated server, where you have command-line (CLI) access.
What Makes This Script Awesome?
This script stands out for its simplicity and effectiveness, offering several key advantages:
- Automated Credential Extraction: No need to hardcode your database username or password! The script intelligently pulls these details directly from your
wp-config.php
file. - Comprehensive Backups: It captures both your entire
public_html
WordPress folder and your vital MySQL database. - Single Combined Archive: After backing up individually, it rolls both the site files and database into a single, convenient “FULL” archive file.
- Intelligent Cleanup: Keeps your server tidy by automatically deleting full backups older than 8 days.
- Flexible Execution: Designed to be run with your site’s domain as an argument, making it adaptable for multiple sites.
Prerequisites
To use this script, you’ll need:
- CLI Access: You need shell access to your server (e.g., via SSH).
- Standard Tools:
mysqldump
,tar
,cat
,grep
,cut
, andfind
should be available on your server (which they almost certainly are). - Specific Folder Structure: The script assumes your WordPress installation is located at
/var/www/<your.site.com>/public_html
and that your backups will be stored in/var/www/<your.site.com>/backups
. You can adjust theSRC_PATH
variable if your setup differs.
The WordPress Backup Script
Here’s the script. Copy it and modify the SRC_PATH
if your WordPress root directory is located elsewhere.Bash
##################################################################
# Backup WordPress site both database and public_html folder.
# Execute instruction: <filename.sh> <www.mysite.com>
# Default Folders:
# /var/www/<www.mysite.com>/public_html
# /var/www/<www.mysite.com>/backups
#
# Change only SRC_PATH env variable as per respective root location.
#
# Author: Saumya Padhi (sa**********@*****ok.com, ad***@*********hi.com)
# LinkedIn: www.linkedin.com/in/saumyap
#
# Crontab Example:
# 00 12 * * * /bin/sh /scripts/backup_mysite.sh saumyapadhi.com >> /dev/null
#
##################################################################
#!/bin/sh
# The site domain is passed as the first argument to the script
SITE=$1
# Define the source path for your WordPress installation
# Adjust this variable if your WordPress root is different
SRC_PATH=/var/www/$SITE
# Extract database credentials from wp-config.php
DBNAME=`cat $SRC_PATH/public_html/wp-config.php | grep DB_NAME | cut -d \' -f 4`
DBUSER=`cat $SRC_PATH/public_html/wp-config.php | grep DB_USER | cut -d \' -f 4`
DBPASS=`cat $SRC_PATH/public_html/wp-config.php | grep DB_PASSWORD | cut -d \' -f 4`
DBHOST=`cat $SRC_PATH/public_html/wp-config.php | grep DB_HOST | cut -d \' -f 4`
# Define backup file names with date stamp
DBBACKUP=$SRC_PATH/backups/$SITE-DB-`date "+%Y-%m-%d"`.sql
SITEBACKUP=$SRC_PATH/backups/$SITE-site-`date "+%Y-%m-%d"`.tar.gz
L0BACKUP=$SRC_PATH/backups/FULL-$SITE-`date "+%Y-%m-%d"`.tar.gz
# Ensure the backup directory exists
mkdir -p $SRC_PATH/backups
# 1. Perform MySQL Database Backup
# Uses mysqldump to export the database.
/usr/bin/mysqldump -u $DBUSER -p$DBPASS $DBNAME > $DBBACKUP
# 2. Perform WordPress Site Files Backup
# Compresses the public_html folder.
/bin/tar -zcvf $SITEBACKUP $SRC_PATH/public_html
# 3. Create a Full Combined Backup and Clean Up Individuals
# Creates a single combined tar.gz archive of both the site and DB backups.
# Crucially, the --remove-files flag deletes the individual DBBACKUP and SITEBACKUP
# files AFTER they have been successfully added to the L0BACKUP archive.
/bin/tar -zcvf $L0BACKUP $SITEBACKUP $DBBACKUP --remove-files
# 4. Delete Old Full Backups
# Finds and removes full backups older than 8 days from the backup directory.
find $SRC_PATH/backups/ -name "FULL-${SITE}*" -mtime +8 -exec rm {} \;
How to Implement and Schedule Your Backup
- Save the Script: Save the code above into a file, for example,
backup_mysite.sh
, in a secure location on your server (e.g.,/scripts/
or a dedicated~/scripts/
directory). - Make it Executable: Grant execution permissions to the script:
chmod +x /scripts/backup_mysite.sh
(adjust path as needed). - Run Manually (for testing): You can test the script by running it manually from your CLI:
/scripts/backup_mysite.sh your.site.com
Replaceyour.site.com
with your actual domain name. - Schedule with Crontab: To automate the process, schedule it using a cron job. Open your crontab for editing:
crontab -e
Then, add the following line (for example, to run daily at 12:00 PM):00 12 * * * /bin/sh /scripts/backup_mysite.sh saumyapadhi.com >> /dev/null
00 12 * * *
: This sets the schedule to run at 12:00 PM every day./bin/sh /scripts/backup_mysite.sh saumyapadhi.com
: This is the command that executes your script, passingsaumyapadhi.com
as the site argument. Remember to changesaumyapadhi.com
to your actual domain!>> /dev/null
: This redirects all standard output and errors from the script to/dev/null
, preventing cron from sending you emails for every successful backup (which can quickly fill your inbox!).
CRITICAL WARNINGS & CONSIDERATIONS:
- TEST BEFORE USE! READ DISCLAIMER
Recent Comments