Created
December 12, 2017 21:46
-
-
Save imerr/8d2ba29c267830ee16848af8b8646f28 to your computer and use it in GitHub Desktop.
MySQL Backup Script
This file contains 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 | |
######################## | |
######## CONFIG ######## | |
######################## | |
databases=("database1" "database2" "database3") | |
mysql_host="localhost" | |
mysql_user="asdf" | |
mysql_password="pass" | |
storage_dir="/backups" | |
name="mysql" | |
################## | |
d=$(date +%F_%H.%M) | |
dir=`mktemp -d` | |
extrasFile=`mktemp` | |
trap "rm -rf $dir $extrasFile" EXIT | |
echo "Dir $dir ExtrasFile $extrasFile" | |
# pass password safely to mysqldump | |
chmod 600 $extrasFile | |
echo '[client]' >> $extrasFile | |
echo "password=$mysql_password" >> $extrasFile | |
for db in "${databases[@]}"; do | |
mysqldump --defaults-extra-file=$extrasFile $db -h$mysql_host -u$mysql_user --single-transaction | gzip > $dir/$db.sql.gz | |
echo $db | |
done | |
tar -C $dir -cvf $storage_dir/${name}-$d.tar . | |
# delete backups older than 30 days | |
find $dir -maxdepth 1 -mtime +30 -exec rm {} \; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment