BACKUPS
June 24, 2026

How to Schedule Automatic Database Backups with Cron

9 min read
Author
CloudStick Team
Backend Developer
Share this article
Schedule Database Backups with Cron
CloudStick
Automated DB Backup with Cron

Cron for Database Backups: What You Need to Know

Cron is the standard Linux task scheduler — it runs commands at fixed times with no user intervention. For database backups, it is the simplest way to get a reliable dump running without external dependencies. You write a cron expression, point it at a script, and the backup runs whether you remember to or not.

Cron expression format: minute hour day-of-month month day-of-week command

# Common cron schedules for database backups
0 3 * * * # Daily at 3am
0 */6 * * * # Every 6 hours
0 * * * * # Every hour
*/30 * * * * # Every 30 minutes (high-frequency WooCommerce)
0 3 * * 0 # Weekly on Sunday at 3am

A Production-Ready Database Backup Script

This script dumps multiple databases, compresses the output, and logs success/failure:

#!/bin/bash
# /usr/local/bin/db-backup.sh
set -euo pipefail
BACKUP_DIR="/var/backups/databases"
DATE=$(date +%Y%m%d-%H%M)
LOG="/var/log/db-backup.log"
DATABASES="site1_db site2_db site3_db"
mkdir -p "$BACKUP_DIR"
for DB in $DATABASES; do
FILE="$BACKUP_DIR/${DB}-${DATE}.sql.gz"
if mysqldump --single-transaction --quick "$DB" | gzip > "$FILE"; then
echo "$(date): OK $DB -> $FILE" >> "$LOG"
else
echo "$(date): FAILED $DB" >> "$LOG"
exit 1
fi
done

The script uses the ~/.my.cnf credentials file so no password appears in the script. Credentials are stored in /root/.my.cnf with chmod 600 permissions. See the mysqldump article for the credential file setup.

Automatic Rotation to Manage Disk Space

# Delete backups older than 14 days at end of backup script
find "$BACKUP_DIR" -name "*.sql.gz" -mtime +14 -delete
# Or keep only the last 30 files per database (sorted by name/date)
for DB in $DATABASES; do
ls -t "$BACKUP_DIR/${DB}-"*.sql.gz | tail -n +31 | xargs -r rm
done

Alert on Backup Failure

A backup that silently fails is worse than no backup at all — it gives you false confidence. Add a dead man's switch using a free monitoring service like healthchecks.io:

# At the END of the script, ping the healthcheck URL on success
curl -fsS --retry 3 https://hc-ping.com/YOUR-UUID > /dev/null
# If the script exits early (set -e), the ping never happens
# healthchecks.io emails you if it does not hear from the script

You will receive an alert if the script fails, if the server is down and cron doesn't run, or if the schedule changes and the job stops firing. This one addition turns a silent failure mode into a monitored process.

CloudStick Handles Scheduling and Rotation Automatically

CloudStick's Database Backups feature lets you configure schedule and retention per database from the dashboard, with no script writing required. See the CloudStick knowledge base: How to Enable Database Backup for the step-by-step walkthrough of enabling backup per database, setting the schedule, and configuring the retention period.

Leave a comment
Full Name
Email Address
Message
Contents

We use cookies to improve your experience

CloudStick uses cookies to personalise content, analyse traffic and keep you signed in. Cookie Policy · Terms of Service

Manage cookies