Created
January 22, 2020 17:57
-
-
Save EmaLinuxawy/8df9e0908efcfc25fb1a22c53d9ddee1 to your computer and use it in GitHub Desktop.
Use this bash script to backup mysql databases
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 | |
# Backup Destination Folder | |
DEST=/backup | |
date=$(date +"%I:%M%p-%d-%m-%Y") | |
DATABASES=$(mysql -e "SHOW DATABASES;" | tr -d "| " | grep -v Database) | |
[ ! -d $DEST ] && mkdir -p $DEST | |
for db in $DATABASES; do | |
FILE="${DEST}/$db-$date.sql" | |
FILEDATE= | |
# Be sure to make one backup per day | |
[ -f $FILE ] && FILEDATE=$(date -r $FILE +"%F") | |
[ -f $FILE ] && mv "$FILE" "${FILE}.old" | |
mysqldump --single-transaction --routines --quick -B $db > "$FILE" | |
# Delete files older than 3 days | |
find $DEST/* -mtime +3 -exec rm {} \; | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment