Created
August 13, 2021 07:25
-
-
Save lanuma/1244f3bfe1097a67d5ade50b8b69f214 to your computer and use it in GitHub Desktop.
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 | |
DEST=/root/backup/databases | |
CURRDATE=$(date +"%F") | |
HOSTNAME=$(hostname) | |
USER="root" | |
PASS="$(cat /root/backup/pass)" | |
DATABASES=$(mysql -u $USER -p$PASS -e "SHOW DATABASES;" | tr -d "| " | grep -v Database) | |
if [ ! -d $DEST ]; then | |
mkdir -p $DEST | |
fi | |
for db in $DATABASES; do | |
FILE="${DEST}/$db-${CURRDATE}.sql.gz" | |
FILEDATE= | |
if [ -f $FILE ]; then | |
FILEDATE=$(date -r $FILE +"%F") | |
fi | |
[ "$FILEDATE" == "$CURRDATE" ] && continue | |
[ -f $FILE ] && mv "$FILE" "${FILE}.old" | |
mysqldump --single-transaction --routines --quick -u $USER -p$PASS -B $db | gzip > "$FILE" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment