Backup Mysql Database to AWS S3 storage - Script

Hi ,

Below is the mysql backup script -  where you can backup it to the AWS S3 storage.

Add this script to the cron tab for automated backups daily.
 

#!/bin/bash
# Basic variables
mysqluser=" "
mysqlpass=" "
bucket="s3://bucket-name"
AWSPATH="/usr/local/bin/aws"

# Timestamp (sortable AND readable)
stamp=`date +"%s - %A %d %B %Y @ %H%M"`

# List all the databases
databases=`mysql -u$mysqluser -p$mysqlpass -e "SHOW DATABASES;" | tr -d "| " | grep -v "\(Database\|information_schema\|performance_schema\|mysql\|test\)"`

# Feedback
echo -e "Dumping to \e[1;32m$bucket/$stamp/\e[00m"

# Loop the databases
for db in $databases; do

# Define our filenames
filename="$stamp - $db.sql.gz"
tmpfile="/tmp/$filename"
object="$bucket/$stamp/$filename"

# Dump and zip
echo -e "  creating \e[0;35m$tmpfile\e[00m"
mysqldump -u$mysqluser -p$mysqlpass --force --opt --databases "$db" | gzip -c > "$tmpfile"

# Upload
echo -e "  uploading..."
 $AWSPATH s3 cp "$tmpfile" "$object"

# Delete
rm -f "$tmpfile"
done;

echo -e  "Jobs had a good run"


Please comment if you have any questions .


Comments

Popular posts from this blog

Important GIT COMMANDS

Docker Compose YML - Application , REDIS , MongoDB

Sendy Nginx Configuration