Created
April 22, 2016 19:53
-
-
Save leemason/b963295ece803de40f04838e1cfb7776 to your computer and use it in GitHub Desktop.
Generate a db backup of every db in the mysql server, place in the /etc/cron.daily directory to run automatically everyday.
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 | |
#backup all databases on server every day to a backups folder | |
PATH=/usr/sbin:/usr/bin:/sbin:/bin | |
# Timestamp (sortable AND readable) | |
stamp=`date +"%s-%A_%d_%B_%Y@%H_%M"` | |
# List all the databases | |
databases=`mysql -u {{user}} -p{{password}} -e "SHOW DATABASES;" | tr -d "| " | grep -v "\(Database\|performance_schema\|information_schema\|mysql\|test\)"` | |
# Loop the databases | |
for db in $databases; do | |
# Define our filenames | |
filename="$db.sql.gz" | |
filepath="/backups/mysql/$stamp-$filename" | |
# Dump and zip | |
mysqldump -u {{user}} -p{{password}} --force --opt --databases "$db" | gzip -c > "$filepath" | |
done; | |
#delete files older than 7 days | |
find /backups/mysql -type f -mtime +7 -exec rm {} \; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment