/ Bash Scripts

Backup and restore MySQL databases

This documentation details a Bash script to backup and restore MySQL databases. This script allows you to provide custom options and settings, thereby enhancing usability and flexibility.

Prerequisites

  • Access to a Linux environment
  • Installed MySQL/MariaDB Server
  • Basic understanding of Bash scripting and MySQL commands

Steps to Backup and Restore MySQL Databases

1. Database Backup Script

Here is a sample script that performs the backup of a MySQL database.

#!/bin/bash

# Define MySQL parameters
MYSQL_USER=""
MYSQL_PASSWORD=""
MYSQL_HOST=""
MYSQL_DB=""

# Define backup directory
BACKUP_DIR=""

# MySQL dump command
mysqldump -h ${MYSQL_HOST} -u ${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DB} > ${BACKUP_DIR}/${MYSQL_DB}_$(date +%Y%m%d%H%M%S).sql

Replace the empty string in MYSQL_USER, MYSQL_PASSWORD, MYSQL_HOST, MYSQL_DB, and BACKUP_DIR with your actual MySQL credentials, hostname, database name, and the backup directory path respectively.

This script will create a backup of your MySQL database and save it with a timestamp in the specified backup directory.

2. Database Restore Script

Here is a sample script to restore a MySQL database from the backup.

#!/bin/bash

# Define MySQL parameters
MYSQL_USER=""
MYSQL_PASSWORD=""
MYSQL_HOST=""
MYSQL_DB=""

# Define backup directory
BACKUP_DIR=""
BACKUP_FILE=""

# MySQL restore command
mysql -h ${MYSQL_HOST} -u ${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DB} < ${BACKUP_DIR}/${BACKUP_FILE}

Replace the empty string in MYSQL_USER, MYSQL_PASSWORD, MYSQL_HOST, MYSQL_DB, BACKUP_DIR, and BACKUP_FILE with your actual MySQL credentials, hostname, database name, backup directory, and backup file respectively.

This script will restore your MySQL database from the specified backup file.

Customizing the Script

You can enhance these scripts by adding the ability to pass parameters to the script. The enhanced scripts are as follows:

1. Enhanced Backup Script

#!/bin/bash

# Define MySQL parameters
MYSQL_USER=$1
MYSQL_PASSWORD=$2
MYSQL_HOST=$3
MYSQL_DB=$4

# Define backup directory
BACKUP_DIR=$5

# MySQL dump command
mysqldump -h ${MYSQL_HOST} -u ${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DB} > ${BACKUP_DIR}/${MYSQL_DB}_$(date +%Y%m%d%H%M%S).sql

To run this script, you can use the following command:

bash backup_script.sh [MySQL User] [MySQL Password] [MySQL Host] [Database Name] [Backup Directory]

2. Enhanced Restore Script

#!/bin/bash

# Define MySQL parameters
MYSQL_USER=$1
MYSQL_PASSWORD=$2
MYSQL_HOST=$3
MYSQL_DB=$4

# Define backup directory
BACKUP_DIR=$5
BACKUP_FILE=$6

# MySQL restore command
mysql -h ${MYSQL_HOST} -u ${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DB} < ${BACKUP_DIR}/${BACKUP_FILE}

To run this script, you can use the following command:

bash restore_script.sh [MySQL User] [MySQL Password] [MySQL Host] [Database Name] [Backup Directory] [Backup File]

Additional Considerations

  • Security: Avoid storing your MySQL credentials in the script. It’s more secure to pass them as parameters or environment variables.
  • Error Handling: The current scripts do not contain error handling. In a production environment, consider adding error checking and reporting mechanisms.
  • Automated Backup: Consider scheduling the backup script with a cron job to automate the backup process.

Conclusion

These scripts serve as a basic guide for backing up and restoring MySQL databases. Depending on your specific requirements, you may need to adjust or expand these scripts.

Was this helpful?

Thanks for your feedback!