Skip to content

Instantly share code, notes, and snippets.

@remoharsono
Forked from skarllot/backup_mysqldb.sh
Created April 12, 2020 12:36
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 remoharsono/c1364995b18fa3f81c2def5eefb8ef19 to your computer and use it in GitHub Desktop.
Save remoharsono/c1364995b18fa3f81c2def5eefb8ef19 to your computer and use it in GitHub Desktop.
Bash script to backup all mysql databases thru mysqldump
#!/bin/bash
# Destiny folder where backups are stored
DEST=/tmp/bacula/server01
CURRDATE=$(date +"%F")
# Hostname where MySQL is running
HOSTNAME="srv-mysql"
# User name to make backup
USER="root"
# File where has the mysql user password
PASS="$(cat /root/etc/mysqlpass)"
DATABASES=$(mysql -h $HOSTNAME -u $USER -p$PASS -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)
[ ! -d $DEST ] && mkdir -p $DEST
for db in $DATABASES; do
FILE="${DEST}/$db.sql.gz"
FILEDATE=
# Be sure to make one backup per day
[ -f $FILE ] && FILEDATE=$(date -r $FILE +"%F")
[ "$FILEDATE" == "$CURRDATE" ] && continue
[ -f $FILE ] && mv "$FILE" "${FILE}.old"
mysqldump --single-transaction --routines --quick -h $HOSTNAME -u $USER -p$PASS -B $db | gzip > "$FILE"
chown bacula:disk "$FILE"
rm -f "${FILE}.old"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment