Automate Your VPS Database Backups Like a Pro: A Complete MySQL Backup Script

Automate Your VPS Database Backups Like a Pro: A Complete MySQL Backup Script

posted 3 min read

Managing a VPS (Virtual Private Server) with a MySQL database? Then you know how important **regular backups** are!

A single accidental command, server crash, or unexpected failure can **wipe out critical data** in seconds. That’s why automating backups is a must!

In this guide, you’ll learn how to:
  • ✅ Automate MySQL backups on your VPS
  • ✅ Compress backups to save space
  • ✅ Automatically delete old backups
  • ✅ Schedule backups using cron jobs

By the end of this guide, you’ll have a **fully automated, hands-free backup solution** running on your server!

Why You Need Automated MySQL Backups

Relying on **manual backups** is risky and inefficient. Here's why automation is essential:

  • ❌ **Mistakes happen** – A single DROP DATABASE command can erase years of data.
  • ❌ **Downtime is expensive** – Losing customer data can damage your reputation and business.
  • ❌ **Disk failures are unpredictable** – Outdated backups won’t help if your server crashes.
  • ✅ **Automation ensures regular backups** – Set it once, and let your VPS handle the rest!

️ Prerequisites

Before we start, make sure you have:

  • ✔️ A VPS running **Debian/Ubuntu**
  • ✔️ MySQL/MariaDB installed
  • ✔️ Sudo privileges

Step 1: Download the MySQL Backup Script

Clone the backup script from GitHub:

git clone https://github.com/giftbalogun/vpsMySQLBackup.git
cd vpsMySQLBackup

⚙️ Step 2: Configure the Backup Script

Open the script and edit your MySQL credentials and backup settings:

nano backup_mysql.sh

Modify these lines:

MYSQL_USER="your_mysql_username"
MYSQL_PASSWORD="your_mysql_password"
DAYS_TO_KEEP=5        # Set how many days to keep backups (0 to keep forever)
GZIP=1                # Set to 1 for compressed backups, 0 for raw SQL
BACKUP_PATH="/root/backup/mysql"

**Tip:** Make sure your MySQL user has the correct privileges!

Step 3: Make the Script Executable & Run It

Give the script execution permission:

chmod +x backup_mysql.sh

Run the script manually to test it:

sudo ./backup_mysql.sh

✅ If everything is set up correctly, you’ll see output like this:

--------------------------------------------------------
      MySQL Backup Script - Copyright © 2024 Gift Balogun
--------------------------------------------------------
[INFO] Backing up database: my_database (compressed)
[SUCCESS] Backup completed: my_database
[INFO] Removing backups older than 5 days...
[INFO] Cleanup completed.
[INFO] MySQL Backup Process Completed Successfully!

Step 4: Automate Backups with Cron Jobs

To ensure backups run automatically, we’ll use **cron jobs**.

1️⃣ Open the cron job editor:

crontab -e

2️⃣ Add this line at the bottom:

0 2 * * * /path/to/backup_mysql.sh >> /var/log/mysql_backup.log 2>&1

This schedules the backup **every day at 2 AM** and logs the output.

3️⃣ Save and exit (CTRL + X, then Y and ENTER).

Done! Your server now automatically backs up MySQL databases daily.

️ Step 5: Restore a Backup (When Needed)

In case of data loss, restoring a backup is simple:

1️⃣ Find your backup file:

ls -lh /root/backup/mysql/

2️⃣ Restore it to MySQL:

mysql -u your_mysql_username -p your_database_name < /root/backup/mysql/your_backup.sql

If your backup is compressed (.sql.gz), use:

gunzip -c /root/backup/mysql/your_backup.sql.gz | mysql -u your_mysql_username -p your_database_name

️ Troubleshooting

  • **Permission Denied?** Run the script with sudo or check file permissions.
  • **Crontab Not Running?** Ensure the script has execute permission and use the full path in crontab -e.
  • **Backup Files Missing?** Double-check the BACKUP_PATH directory and DAYS_TO_KEEP settings.

Final Thoughts

Automating MySQL backups **saves time**, **prevents data loss**, and ensures **peace of mind**.

Want the full script? **Get it on GitHub:** View Full Code Here

Need Help or Have Questions? Drop a comment below!

If you read this far, tweet to the author to show them you care. Tweet a Thanks
Gift automating MySQL backups is nice nobody wants to wake up to a crashed server and no backups. Love how you broke it down step by step, especially the cron job part! few questions—what’s your take on storing backups offsite (like AWS S3 or Google Drive)? Would adding that to the script be a hassle or pretty straightforward?
Thanks for that Ben, you reminded me.
There is a second part of the script where it will be used in your local computer or any offsite NAS or any form of storage device.
I will create another part of the script to backup to local and AWS.
Greate Article

More Posts

Automating MySQL Backups on a VPS with Telegram Alerts Part 2

Gift Balogun - Mar 22

Database Interaction using Python: Introduction to SQLite/MySQL/PostgreSQL

Muzzamil Abbas - Mar 19

Ever wondered how to automate your development workflow effortlessly?

Vignesh J - Feb 18

Setup a LAMP Server on a VPS in Under an Hour: The Ultimate Guide to Web Hosting

Gift Balogun - Feb 19

Setting Up Next.js Authentication with Supabase: A Complete Guide

Anuj Kumar Sharma - Jan 22
chevron_left