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.
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.
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:
It’s efficient, hands-off, and remarkably effective.
To use this script, you’ll need:
mysqldump
, tar
, cat
, grep
, cut
, and find
should be available on your server (which they almost certainly are)./var/www/<your.site.com>/public_html
and that your backups will be stored in /var/www/<your.site.com>/backups.
You can adjust the SRC_PATH
variable if your setup differs.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 {} \;
backup_mysite.sh
, in a secure location on your server (e.g., /scripts/
or a dedicated ~/scripts/
directory).chmod +x /scripts/backup_mysite.sh
(adjust path as needed)./scripts/backup_mysite.sh your.site.com
Replace your.site.com
with your actual domain name.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, passing saumyapadhi.com
as the site argument. Remember to change saumyapadhi.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!).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.
This script stands out for its simplicity and effectiveness, offering several key advantages:
wp-config.php
file.public_html
WordPress folder and your vital MySQL database.To use this script, you’ll need:
mysqldump
, tar
, cat
, grep
, cut
, and find
should be available on your server (which they almost certainly are)./var/www/<your.site.com>/public_html
and that your backups will be stored in /var/www/<your.site.com>/backups
. You can adjust the SRC_PATH
variable if your setup differs.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 {} \;
backup_mysite.sh
, in a secure location on your server (e.g., /scripts/
or a dedicated ~/scripts/
directory).chmod +x /scripts/backup_mysite.sh
(adjust path as needed)./scripts/backup_mysite.sh your.site.com
Replace your.site.com
with your actual domain name.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, passing saumyapadhi.com
as the site argument. Remember to change saumyapadhi.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!).Explore Oracle Hybrid Columnar Compression (HCC) through a hands-on lab comparing OLTP, Query, and Archive…
Revisit my popular, hands-on workshop guiding you through Oracle 19c's Automatic Indexing! I'll show you…
Unlock the power of Oracle 19c's multitenant architecture by converting your existing non-CDB databases into…
Learn how Oracle 19c simplifies privilege management with its built-in analysis tools, ensuring a 'least…
After 18 enriching years in the tech world, I recently took a deliberate break, a…
Cleared following 3 Oracle Cloud Infrastructure exams in a row in three days. Thanks to…