Skip to content

Instantly share code, notes, and snippets.

@lanuma
Created August 13, 2021 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lanuma/1244f3bfe1097a67d5ade50b8b69f214 to your computer and use it in GitHub Desktop.
Save lanuma/1244f3bfe1097a67d5ade50b8b69f214 to your computer and use it in GitHub Desktop.
#!/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