Created
August 1, 2021 07:25
-
-
Save trgino/9aef5cb4bc4d5b33886fbdbad3a0fe9a to your computer and use it in GitHub Desktop.
Amazon S3 MYSQL Backupper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M") | |
BACKUP_DIR="/home/mysql_backups" | |
#local mysql backup folder | |
MYSQL_USER="root" | |
#mysql root user for all databases backup | |
MYSQL=/usr/bin/mysql | |
#mysql full path | |
MYSQL_PASSWORD="passwd" | |
#mysql root password | |
MYSQLDUMP=/usr/bin/mysqldump | |
#mysqldump full path | |
DAYS=8 | |
#local mysql backups will delete older than this days | |
S3DAYS=30 | |
#aws s3 mysql backups will delete older than this s3days | |
FIND=/usr/bin/find | |
#find full path | |
FINDRM=/usr/bin/rm | |
#rm full path | |
AWSEXEC=/usr/local/bin/aws | |
#aws cli full path | |
S3BUCKET="bucket-name" | |
#aws s3 bucket name | |
S3FOLDER="bucket-folder" | |
#aws s3 bucket mysql backups folder name | |
databases=`$MYSQL --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|test|cyberpanel|mysql|information_schema|performance_schema)"` | |
#getting mysql database list without test,cyberpanel,mysql,information_schema and performance_schema | |
for db in $databases; do | |
$MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip > "$BACKUP_DIR/$db-$TIMESTAMP.sql.gz" | |
echo "$db backup created" | |
$AWSEXEC s3 cp "$BACKUP_DIR/$db-$TIMESTAMP.sql.gz" s3://$S3BUCKET/$S3FOLDER/ | |
echo "$db backup uploaded s3" | |
done | |
#local mysql backups created and uploded to aws s3 bucket | |
$FIND $BACKUP_DIR -mtime +$DAYS -exec $FINDRM -f {} \; | |
#local mysql backups deleting older than x days | |
$AWSEXEC s3 ls s3://$S3BUCKET/$S3FOLDER/ | while read -r line; | |
do | |
createDate=`echo $line|awk {'print $1" "$2'}` | |
createDate=`date -d"$createDate" +%s` | |
olderThan=`date --date "$S3DAYS days ago" +%s` | |
if [[ $createDate -lt $olderThan ]] | |
then | |
fileName=`echo $line|awk {'print $4'}` | |
if [[ $fileName != "" ]] | |
then | |
$AWSEXEC s3 rm s3://$S3BUCKET/$S3FOLDER/$fileName | |
echo "$fileName older than $DAYS -- Deleted --" | |
fi | |
fi | |
done | |
#aws s3 mysql backups deleted older thna x s3days |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment