Automating Database Backups with MySQLDump

By Raman Kumar

Updated on Oct 02, 2024

In this tutorial, we'll explain how to automating database backups with MySQLDump.

Backing up your MySQL databases is critical for data security, and mysqldump is a powerful utility that allows you to easily export and automate backups of your databases. In this step-by-step guide, we'll walk through the process of creating a MySQL database, inserting some sample data, and automating backups using mysqldump.

Prerequisites

  • A Linux-based dedicated server or KVM VPS with MySQL installed
  • MySQL user credentials with appropriate access rights
  • Basic terminal knowledge

Automating Database Backups with MySQLDump

Step 1: Install MySQL (if not already installed)

If you don't have MySQL installed, you can install it by running the following command:

sudo apt update
sudo apt install mysql-server

Once installed, ensure the MySQL service is running:

sudo systemctl start mysql
sudo systemctl enable mysql

Step 2: Log in to MySQL and Create a Database

Log in to the MySQL shell as the root user:

mysql -u root -p

Once logged in, create a new database:

CREATE DATABASE mydatabase;

Now, switch to the newly created database:

USE mydatabase;

Step 3: Create a Table and Insert Sample Data

Next, create a sample table named customers:

CREATE TABLE customers (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Now, insert some sample data into the customers table:

INSERT INTO customers (name, email)
VALUES 
('John Doe', 'john@example.com'),
('Jane Smith', 'jane@example.com'),
('Alice Brown', 'alice@example.com');

Verify that the data has been inserted:

SELECT * FROM customers;

Output:

+----+-------------+-------------------+---------------------+
| id | name        | email             | created_at          |
+----+-------------+-------------------+---------------------+
|  1 | John Doe    | john@example.com  | 2024-10-02 03:07:53 |
|  2 | Jane Smith  | jane@example.com  | 2024-10-02 03:07:53 |
|  3 | Alice Brown | alice@example.com | 2024-10-02 03:07:53 |
+----+-------------+-------------------+---------------------+
3 rows in set (0.00 sec)

Once you're sure the table and data are correct, exit the MySQL shell:

EXIT;

Step 4: Test the mysqldump Command Manually

Now that you have a database with sample data, let's use mysqldump to manually back it up. The syntax for using mysqldump is as follows:

mysqldump -u root -p mydatabase > mydatabase_backup.sql
  • -u root: The MySQL username.
  • -p: Prompt for a password.
  • mydatabase: The name of the database you want to back up.
  • > mydatabase_backup.sql: Export the backup to a file called mydatabase_backup.sql.

Enter your MySQL password when prompted. This will create a backup file mydatabase_backup.sql in the current directory.

Step 5: Automating the Backup with a Shell Script

Next, let's automate this process by creating a shell script that runs mysqldump on a schedule.

Create a script file named mysql_backup.sh:

nano mysql_backup.sh

Add the following code to the file:

#!/bin/bash

# MySQL user credentials
USER="root"
PASSWORD="yourpassword"
DATABASE="mydatabase"

# Backup destination
BACKUP_DIR="/path/to/backup_directory"
LOG_FILE="/path/to/backup_log.txt"
DATE=$(date +%Y-%m-%d_%H-%M-%S)
BACKUP_FILE="$BACKUP_DIR/mydatabase_backup_$DATE.sql"

# Perform the backup
mysqldump -u $USER -p$PASSWORD $DATABASE > $BACKUP_FILE

# Optional: Remove backups older than 7 days
find $BACKUP_DIR -type f -name "*.sql" -mtime +7 -exec rm {} \;

# Output a message
echo "Backup completed: $BACKUP_FILE"

In this script:

  • Replace yourpassword with your actual MySQL root password.
  • Set BACKUP_DIR to the directory where you want to store your backups.
  • The find command removes backups older than 7 days to free up space.

Make the script executable:

chmod +x mysql_backup.sh

Step 6: Automate Backups with Cron

To schedule automated backups, you can use cron, which is a time-based job scheduler in Unix-like systems.

Open the crontab editor:

crontab -e

Add the following line to schedule the backup script to run daily at 2 AM:

0 2 * * * /path/to/mysql_backup.sh

This will execute the mysql_backup.sh script daily at 2:00 AM. You can adjust the timing as per your needs.

Step 7: Verify the Backups

To verify that your backups are working, check the backup directory for newly created .sql files. You can also test the integrity of a backup by restoring it to a new database:

Log in to MySQL:

mysql -u root -p

Create a new database:

CREATE DATABASE test_restore;

Restore the backup:

mysql -u root -p test_restore < /path/to/your/backup.sql

This will load the backup into the new test_restore database, and you can verify the data.

Configure the Backup Script to Transfer MySQL Database Backup to a Remote Server

Step 1: Set Up SSH Access to the Remote Server

Before automating the transfer, you need to set up SSH access to the remote server.

Generate SSH Keys on Local Machine: To automate the process, SSH key-based authentication is needed. Generate SSH keys without a password (or with a password and use ssh-agent for automation):

ssh-keygen -t rsa -b 4096

Press Enter to save the keys to the default location (~/.ssh/id_rsa), and skip the passphrase for automation.

Copy the Public Key to the Remote Server: Transfer the public key to the remote server so that you can log in without a password:

ssh-copy-id user@remote_server_ip

Replace user with the remote server’s username and remote_server_ip with its IP address or domain name.

Step 2: Modify the Backup Script to Transfer the Backup File

Here’s the updated backup script that includes the transfer to a remote server using scp. You can use rsync for more advanced synchronization if needed.

nano mysql_backup.sh

Modify following content:

#!/bin/bash

# MySQL user credentials
USER="root"
PASSWORD="yourpassword"
DATABASE="mydatabase"

# Backup destination on local server
BACKUP_DIR="/path/to/backup_directory"
LOG_FILE="/path/to/backup_log.txt"
DATE=$(date +%Y-%m-%d_%H-%M-%S)
BACKUP_FILE="$BACKUP_DIR/mydatabase_backup_$DATE.sql"

# Remote server details
REMOTE_USER="remoteuser"
REMOTE_SERVER="remote_server_ip"
REMOTE_DIR="/path/to/remote/backup_directory"

# Perform the backup and log the output
{
    echo "Backup started at: $DATE"
    if mysqldump -u $USER -p$PASSWORD $DATABASE > $BACKUP_FILE; then
        echo "Backup completed successfully: $BACKUP_FILE"
        
        # Transfer the backup file to the remote server
        scp $BACKUP_FILE $REMOTE_USER@$REMOTE_SERVER:$REMOTE_DIR
        
        # Verify if the transfer was successful
        if [ $? -eq 0 ]; then
            echo "Backup successfully transferred to remote server."
        else
            echo "Backup transfer failed!"
        fi
        
    else
        echo "Backup failed!"
    fi
    echo "-----------------------------------------------------"
} >> $LOG_FILE 2>&1

# Optional: Remove backups older than 7 days
find $BACKUP_DIR -type f -name "*.sql" -mtime +7 -exec rm {} \;

Explanation:

The scp command transfers the backup file to the remote server:

scp $BACKUP_FILE $REMOTE_USER@$REMOTE_SERVER:$REMOTE_DIR

Replace REMOTE_USER with your remote server’s username, REMOTE_SERVER with the IP or domain of the server, and REMOTE_DIR with the directory on the remote server where you want to store the backup.

After the scp command, the script checks the exit status ($?). If it’s 0, the transfer was successful; otherwise, it failed.

Step 3: Test the Script

Run the script manually to ensure everything is working as expected:

bash mysql_backup.sh

Ensure that:

  • The MySQL database is successfully backed up.
  • The backup file is correctly transferred to the remote server.
  • The log file reflects the process.

Step 4: Automate the Backup Script with Cron

To automate the backup and transfer process, schedule the script using cron.

Open the crontab editor:

crontab -e

Add a cron job to run the script daily (e.g., at 2 AM):

0 2 * * * /path/to/mysql_backup.sh

This will run the backup script daily at 2 AM, and the backup file will be transferred to the remote server automatically.

Step 5: Securing Your Setup

SSH Key Passphrase: If you added a passphrase to your SSH key, use ssh-agent to store the key passphrase securely in memory and automate logins without prompting.
Backup Encryption: For additional security, consider encrypting the backups before transferring them to the remote server using tools like gpg or openssl.

Optional: Use rsync for More Efficient Transfers

Instead of scp, you can use rsync, which is more efficient because it only transfers the changes in the file:

rsync -avz $BACKUP_FILE $REMOTE_USER@$REMOTE_SERVER:$REMOTE_DIR

Conclusion

We have seen how to automating database backups with MySQLDump. By following these steps, you've set up a simple and reliable system for automating MySQL database backups using mysqldump. This ensures that your data is regularly backed up, and old backups are removed to save disk space. Be sure to monitor the backup directory regularly and test your backups by restoring them to ensure they are working correctly.

By setting up automated database backups with mysqldump and transferring them to a remote server, you ensure your data is safe and backed up off-site. This setup minimizes the risk of data loss due to hardware failures or other issues with the local server.