Shell script - Take MySQL backup and push to aws s3
![](/posts/linux/images/shell-script.png)
Provided below is an example script designed to dynamically back up a MySQL database hosted within a Docker environment. The backup is subsequently pushed to an AWS S3 bucket.
Feel free to utilize this script for your database backup requirements.
MySQL Backup and Push to S3 Bucket
1#!/bin/bash
2
3S3_bucketName=my-database-backup
4DB_Username=root
5DB_Password=pass
6DB_Name=testing
7mysqlIpAddress=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' mysql-idp-server)
8filename='my-server-' + $(date +%Y-%m-%d-%H.%M.%S)
9
10mysqldump -h $mysqlIpAddress -u$DB_Username -p$DB_Password $DB_Name > $filename.sql
11
12#Create bucket
13aws s3 mb s3://$S3_bucketName
14aws s3 cp $filename.sql s3://$S3_bucketName/
15rm -rf $filename.sql