Bash Shell script for MySQL backup and restore

11 mar 2024 2 min di lettura
Bash Shell script for MySQL backup and restore
Indice dei contenuti

Introduction

In today's data-driven world, safeguarding MySQL databases through reliable backup and recovery practices is non-negotiable. This guide introduces two scripts that automate backups and facilitate restores, ensuring data integrity and availability. Below, we dive deeper into configuring and using scripts, giving you the knowledge to implement these solutions effectively.

Configuration essentials

Before diving into scripts, let's set the stage by setting up our environment. Both scripts require initial setup to tailor them to your specific needs.

Database Details - Specify your MySQL database credentials. This includes the username ( DB_USER ) and password ( DB_PASSWORD ). For added security, consider using environment variables or secure vaults to store sensitive information.

Backup Storage - Define the local path ( BACKUP_PATH ) where backups will be stored. Additionally, it specifies the AWS S3 bucket name ( S3_BUCKET_NAME ) for offsite backup storage, providing an additional layer of data protection.

Logging: Determines the path to your log file ( LOG_FILE ), where scripts will log their operations. This is critical for monitoring script activity and troubleshooting any issues.

Automating MySQL backups

Our journey begins with automating MySQL database backups. Backup_script.sh takes center stage here, expertly handling daily, weekly, and monthly backups. This script not only takes local backups, but also seamlessly uploads them to an AWS S3 bucket, providing an additional level of redundancy. Its design emphasizes flexibility and ease of use, allowing for customizable retention policies that meet individual needs.

Characteristics:

  • Automated backups with daily, weekly and monthly frequencies.
  • Dual storage in local directories and AWS S3.
  • Customizable retention policies for efficient storage management.

Usage:

To start a backup, simply run:

./mysql_backup_script.sh DB1 DB2

To schedule regular backups, consider adding a cron job:

0 2 * * * /path/to/backup_script.sh DB1 DB2

This example schedules daily backups at 2:00 am.

Restoring databases from backup

Equally important is the ability to restore databases from these backups, a task handled by Restore_script.sh. This script gives you the flexibility to automatically restore the latest backup or manually select a specific backup based on frequency and date. In scenarios where a local backup is unavailable, recovery from S3 is cleverly used, ensuring that your data is always within reach.

Characteristics:

  • Supports restores from local and S3 backups.
  • It offers automatic restoration of the latest backup or manual selection for more control.

Usage:

For automatic recovery (using the latest backup):

./mysql_restore_script.sh my_database

For manual selection, which allows you to choose the backup frequency and date:

./mysql_restore_script.sh --manual my_database

In-depth study on manual restoration

Manual mode is especially useful when you need precise control over the recovery process. When invoking manual mode, the script will list the available backup frequencies. Once you select a frequency, the available dates for that frequency will be displayed, allowing you to choose the exact backup to restore.

Protect and contributeConcluding thoughts

Empowering yourself with robust backup and restore scripts is a significant step in ensuring the safety of your data and your peace of mind. By integrating these scripts into your workflow, you not only protect your data from accidental loss or corruption, but also streamline your database management tasks.

For more detailed instructions, advanced configurations, and to contribute to the ongoing development of these scripts, visit our GitHub repository or contact us through our community channels.

Buy me a coffeeBuy me a coffee

Supportaci se ti piacciono i nostri contenuti. Grazie.

Successivamente, completa il checkout per l'accesso completo a Noviello.it.
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a Noviello.it.
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.