Skip to content

Instantly share code, notes, and snippets.

@kgorskowski
Created March 19, 2015 15:02
Show Gist options
  • Save kgorskowski/ebfb9a66c801963129ff to your computer and use it in GitHub Desktop.
Save kgorskowski/ebfb9a66c801963129ff to your computer and use it in GitHub Desktop.
mysqldum script with sync to s3
#!/bin/bash
#Variables
MySQLUser="root"
TIMESTAMP=$(date +"%Y-%m-%d-%H-%M")
LOGDIR=
LOGFILE=
FILENAME=backup_$TIMESTAMP.sql.gz
# export database
mysqldump --single-transaction --quick --lock-tables=false --all-databases --master-data=2 -u${MySQLUser} | gzip > /var/backups/sp_mysql/$FILENAME
EXITSTATUS=${PIPESTATUS[0]}
if [ "$EXITSTATUS" -ne "0" ]; then
echo "ERROR while creating Backup" >> $LOGDIR/$LOGFILE
exit 0
else
FILESIZE=$(du -h /var/backups/sp_mysql/$FILENAME | awk -F'.' '{print$1}')
if [ $FILESIZE -lt 5 ]
then
echo "ERROR - Database-Backup is smaller than 5GB, please check!" >> $LOGDIR/$LOGFILE
exit 0
else
NOW=$(date +"%Y-%m-%d-%H-%M")
echo "Backup of $FILENAME finished successfully at $NOW" >> $LOGDIR/$LOGFILE
fi
# remove backups older than 3 days
find /var/backups/sp_mysql/$FILENAME* -mtime +3 -delete
# sync mysql backup to amazon s3
/usr/local/bin/aws s3 sync --only-show-errors /var/backups/sp_mysql/ s3://s3bucket >> $LOGDIR/$LOGFILE
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment