Skip to content

Instantly share code, notes, and snippets.

@EmaLinuxawy
Created January 22, 2020 17:57
Show Gist options
  • Save EmaLinuxawy/8df9e0908efcfc25fb1a22c53d9ddee1 to your computer and use it in GitHub Desktop.
Save EmaLinuxawy/8df9e0908efcfc25fb1a22c53d9ddee1 to your computer and use it in GitHub Desktop.
Use this bash script to backup mysql databases
#!/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