Dump the code
Best ressources to improve your Python Skills

Automate MySQL backup to AWS S3

Created 1 year ago
Posted By admin
3min read
Automating MySQL backup to AWS S3 serves a dual purpose of enhancing efficiency and fortifying data resilience. The automation process eliminates the need for manual backups, ensuring a consistent and reliable schedule for data protection. By leveraging AWS S3, a secure and scalable cloud storage solution, users benefit from robust redundancy and accessibility.

To automate the backup of a MySQL database on Debian 10 and upload it to AWS S3, you can use a combination of "mysqldump" for database backup and the AWS Command Line Interface (AWS CLI) for uploading the backup to S3. Here are the steps to achieve this:

Step 1: Install necessary tools

Make sure you have the necessary tools installed:

sudo apt update
sudo apt install mysql-client
sudo apt install awscli

  • mysql-client is a package that provides the MySQL command-line client utilities. These utilities allow you to interact with a MySQL database server directly from the command line. The package typically includes tools such as mysql, mysqldump, and others.

  • awscli (The AWS Command Line Interface) is a unified tool provided by Amazon Web Services (AWS) that allows users to interact with various AWS services directly from the command line. It provides a command-line interface for managing AWS resources, automating tasks, and performing various operations on AWS services.

Step 2: Configure AWS CLI

Configure the AWS CLI with your AWS credentials:

aws configure

Step 3: Create a script for MySQL backup

Create a script to perform the MySQL database backup. Create a file, for example, "backup_mysql.sh":

#!/bin/bash

# MySQL database credentials
DB_USER="your_mysql_user"
DB_PASSWORD="your_mysql_password"
DB_NAME="your_database_name"

# AWS S3 bucket and path
S3_BUCKET="your_s3_bucket"
S3_PATH="your_s3_path"

# Backup directory
BACKUP_DIR="/tmp/mysql_backup"
DATE=$(date +"%Y%m%d%H%M%S")

# Create backup directory
mkdir -p $BACKUP_DIR

# Perform MySQL backup using mysqldump
mysqldump -u $DB_USER -p$DB_PASSWORD $DB_NAME | gzip > $BACKUP_DIR/$DB_NAME_$DATE.sql.gz

# Upload the backup to AWS S3
aws s3 cp $BACKUP_DIR/$DB_NAME_$DATE.sql.gz s3://$S3_BUCKET/$S3_PATH/

# Clean up local backup files
rm -rf $BACKUP_DIR

Make the script executable:

chmod +x backup_mysql.sh

Step 4: Test the script

Run the script to test if it performs the MySQL backup and uploads it to AWS S3:

./backup_mysql.sh

Step 5: Schedule the script with cron

Schedule the script to run automatically at specified intervals using cron. Edit the crontab file:

crontab -e

Add a line to schedule the script, for example, to run daily at 2 AM:

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

Save the crontab file.

Now, the script will run automatically at the specified time and perform MySQL database backups, uploading them to AWS S3. Make sure to replace placeholder values in the script with your actual MySQL and AWS S3 credentials and paths.
Topics

Mastering Nginx

27 articles

Bash script

2 articles

Crontab

2 articles