Automation

Automating MySQL Backups with a Shell and Task Scheduler

Learn to create an automated MySQL backup script for Linux and Windows using shell scripts and task scheduling.

4 min read

Learn how to automate MySQL backups on both Linux and Windows using shell or batch scripts combined with task scheduling tools like cron jobs and Task Scheduler. This guide covers everything you need to create, schedule, and verify a production-ready MySQL backup solution, ensuring your database is safe and recoverable.

Prerequisites for Automating MySQL Backups

Before getting started, ensure the following requirements are met:

prerequisites

  • Install and configure MySQL database on your Linux or Windows system.
  • Familiarity with shell scripting (Linux) or batch scripting (Windows).
  • Knowledge of task scheduling with cron (Linux) or Task Scheduler (Windows).
  • A defined backup rotation strategy to manage disk space efficiently.

Creating a Manual MySQL Backup

We will begin with performing a manual backup to understand the essentials.

steps

  1. Create a directory to store your backups.

    bash
    mkdir ~/mysql-backups
  2. Use mysqldump to export a single database:

    bash
    mysqldump -u root -p YOUR_DATABASE_NAME > ~/mysql-backups/backup.sql
    # Replace YOUR_DATABASE_NAME with the actual database name.
  3. Verify the backup file:

    bash
    ls -lh ~/mysql-backups/backup.sql
    # Inspect the file or open it to ensure data is dumped.

Automated Backup Script for Linux Systems

Next, we’ll automate backups with a shell script and schedule it using cron.

steps

  1. Create the shell script for automated backups:

    bash
    nano ~/backup_mysql.sh
    # Add the following content:
    #!/bin/bash
    BACKUP_DIR="/path/to/backup/dir"
    DB_USER="root"
    DB_PASS="YourPassword"
    DB_NAME="YOUR_DATABASE_NAME"
    mkdir -p "$BACKUP_DIR"
    find "$BACKUP_DIR" -type f -mtime +7 -exec rm {} \;  # Keep backups for 7 days
  2. Make the script executable:

    bash
    chmod +x ~/backup_mysql.sh
  3. Schedule the script to run daily at 2 AM:

    bash
    crontab -e
    # Add the following line:
    0 2 * * * /bin/bash ~/backup_mysql.sh

Automated Backup Script for Windows Systems

For Windows, we’ll use a batch script and Task Scheduler to automate backups.

steps

  1. Locate the MySQL bin directory, e.g., C:\Program Files\MySQL\MySQL Server 8.0\bin.

  2. Create a batch script for backups: Save the following content in a .bat file, e.g., mysql_backup.bat:

    bash
    @echo off
    set BACKUP_DIR=C:\mysql-backups
    set DB_USER=root
    set DB_PASS=YourPassword
    set DB_NAME=YOUR_DATABASE_NAME
    mkdir "%BACKUP_DIR%"
    forfiles /p %BACKUP_DIR% /s /d -7 /c "cmd /c del @path"
  3. Schedule the script using Task Scheduler:

    • Open Task Scheduler and create a new task.
    • Set a daily trigger to run at the desired time.
    • Under "Actions," specify the batch file created earlier.
    • Save and test the task.

Restoring MySQL Backups

Learn how to restore data from backups in case of data loss.

steps

  1. Create an empty database for restoration:

    bash
    mysql -u root -p -e "CREATE DATABASE NEW_DATABASE_NAME;"
  2. Restore the backup:

    bash
    mysql -u root -p NEW_DATABASE_NAME < /path/to/backup.sql
  3. Verify the restored data:

    bash
    mysql -u root -p -e "USE NEW_DATABASE_NAME; SHOW TABLES;"

Tips for Production-Ready MySQL Backups

FAQ

What is the best way to configure MySQL backup retention?

Use find (Linux) or forfiles (Windows) commands in your automation scripts to remove old backups after a specified number of days.

Can I automate backups for multiple MySQL databases at once?

Yes, modify the script to loop through all databases:

bash
databases=$(mysql -u root -pYourPassword -e "SHOW DATABASES;" | grep -vE "(Database|information_schema|performance_schema)")
for db in $databases; do
done
How do I handle large database backups?

For large databases, consider using options like --quick and --single-transaction with mysqldump to prevent memory issues. Compress backups with gzip to reduce their size.

Should I back up user privileges and stored procedures separately?

Yes, to fully restore a database, don’t forget to back up the mysql database, which contains users and privileges. Specify --routines and --triggers to include stored procedures and triggers.


Official reference: MySQL documentation.