✅ Create MariaDB 11.4 DB Backup Every 30 Minutes on cPanel (AlmaLinux 9 / CloudLinux 9)

Here is a tailored and production-ready version of your MySQL/MariaDB 11.4 automated backup script for cPanel servers running AlmaLinux 9 or CloudLinux 9, with improvements for compatibility and security:


📁 Step 1: Create Backup Directory

mkdir -p /home/mysqlbackups

This location avoids interfering with cPanel system backups.


📝 Step 2: Create the Backup Script

nano /home/mysqlbackups/mysqlbackup_30min.sh

Paste the following updated and secure script:

#!/bin/bash

# Script to create MariaDB/MySQL backups every 30 minutes on cPanel + AlmaLinux/CloudLinux 9

# === Configuration ===
mysql_host="localhost"
mysql_database="username_dbname"
mysql_username="username_username"
mysql_password='randompassword'  # Use .my.cnf for better security if possible

backup_path="/home/mysqlbackups"
expired=3  # Number of days to keep
today=$(date +%F)
timestamp=$(date +%H%M)

backup_dir="$backup_path/$today"
backup_file="$backup_dir/$mysql_database-$timestamp.sql"

# === Ensure today's backup directory exists ===
mkdir -p "$backup_dir"

# === Run backup ===
/usr/bin/mysqldump --host="$mysql_host" --user="$mysql_username" --password="$mysql_password" "$mysql_database" > "$backup_file"

# === Remove backups older than N days ===
find "$backup_path" -mindepth 1 -maxdepth 1 -type d -mtime +$expired -exec rm -rf {} \;

Save and exit, then set the right permissions:

chmod 700 /home/mysqlbackups/mysqlbackup_30min.sh

🔐 Optional: Use .my.cnf for safer password handling

Instead of storing the password in the script:

  1. Create ~/.my.cnf for the user running the script (e.g. root):
nano /root/.my.cnf
  1. Add:
[client]
user=username_username
password=randompassword
  1. Restrict permissions:
chmod 600 /root/.my.cnf
  1. Update script to remove password flags:
/usr/bin/mysqldump --host="$mysql_host" "$mysql_database" > "$backup_file"

🕓 Step 3: Create Cron Job (as root)

Edit root’s crontab:

crontab -e

Add the job:

*/30 * * * * /bin/bash /home/mysqlbackups/mysqlbackup_30min.sh >/dev/null 2>&1

🔁 Step 4: Restart Cron (if needed)

systemctl restart crond

📂 Example Result After One Day

/home/mysqlbackups
├── 2025-07-09
│   ├── username_dbname-0000.sql
│   ├── username_dbname-0030.sql
│   ├── ...
├── 2025-07-10
│   ├── username_dbname-0030.sql
│   ├── ...

✅ Compatible With

  • ✅ cPanel 126+
  • ✅ AlmaLinux 9 / CloudLinux 9
  • ✅ MariaDB 11.4 (installed via cPanel)
  • ✅ SELinux disabled or permissive (common on cPanel)

🛡️ Optional Enhancements

  • Add GZIP compression: /usr/bin/mysqldump ... | gzip > "$backup_file.gz"
  • Add logging: echo "$(date): Backup complete: $backup_file" >> /var/log/mysqlbackup.log

Scroll to Top