Automate WordPress backups with a Linux bash script

Automate WordPress backups with a bash script

Automatic backups are the most important safety net of any site, but hosting snapshots do not always run often enough and do not always restore cleanly. Your own script gives full control: what is saved, how often, where, and for how long. A complete WordPress backup has two parts, the files (core, themes, plugins, uploads) and the database, and both are needed.

Problem: Make a regular automatic backup of the WordPress files and database without hosting snapshots or paid plugins.

Solution: A bash script that dumps the database with mysqldump, packs the files with tar into a folder with a timestamp, deletes old copies, and runs every night from cron.

The database credentials

Do not put the password into the script or on the command line, where other users can see it in the process list. Create an option file for the user who runs the backup, ~/.my.cnf:

[mysqldump]
user=your_db_user
password=your_db_password

and make it readable only by this user: chmod 600 ~/.my.cnf. mysqldump reads it automatically.

The script

Save it as /usr/local/bin/wp_backup.sh:

#!/bin/bash
set -euo pipefail

WP_ROOT="/var/www/html"          # WordPress root
BACKUP_DIR="/backups/wordpress"  # where to keep the archives
DB_NAME="your_db_name"
KEEP_DAYS=14                     # delete backups older than this

TIMESTAMP=$(date +"%Y-%m-%d_%H-%M")
DEST="${BACKUP_DIR}/${TIMESTAMP}"
mkdir -p "$DEST"

echo "[$(date)] Starting WordPress backup..."

# 1. Database
mysqldump --single-transaction --quick --no-tablespaces "$DB_NAME" \
    | gzip > "${DEST}/db_${TIMESTAMP}.sql.gz"

# 2. Files, without cache and temporary folders
tar -czf "${DEST}/files_${TIMESTAMP}.tar.gz" \
    --exclude="${WP_ROOT}/wp-content/cache" \
    --exclude="${WP_ROOT}/wp-content/upgrade" \
    -C "$(dirname "$WP_ROOT")" "$(basename "$WP_ROOT")"

# 3. Old backups
find "$BACKUP_DIR" -mindepth 1 -maxdepth 1 -type d -mtime +"${KEEP_DAYS}" -exec rm -rf {} +

echo "[$(date)] Backup finished: $DEST"

set -euo pipefail stops the script on the first error, including a failed mysqldump inside the pipe, so a broken dump is not silently packed as a “successful” backup. --single-transaction dumps InnoDB tables consistently without locking the site, and --no-tablespaces avoids the PROCESS privilege error on servers with limited database users.

Make it executable and run it once by hand:

chmod 700 /usr/local/bin/wp_backup.sh
/usr/local/bin/wp_backup.sh

Schedule it

crontab -e and add a line for every night at 2:00, with a log:

0 2 * * * /usr/local/bin/wp_backup.sh >> /var/log/wp_backup.log 2>&1

Off-site copy

A backup on the same server is lost together with the server. Copy the folder to another machine, for example with rsync over SSH (with a key, see adding a private key permanently):

rsync -az /backups/wordpress/ backup@backup-server:/backups/example.com/

NOTE: A backup that has never been restored is only a hope. From time to time restore the latest archive on a test server: unpack the files, import the dump with gunzip < db.sql.gz | mysql db_name, and open the site.