Skip to content

Instantly share code, notes, and snippets.

@kangmasjuqi
Last active September 7, 2018 09:39
Show Gist options
  • Save kangmasjuqi/081bfd466df19cee3cd70c3f37a15634 to your computer and use it in GitHub Desktop.
Save kangmasjuqi/081bfd466df19cee3cd70c3f37a15634 to your computer and use it in GitHub Desktop.
working with mysql database backup
####################################################################################################
####################################################################################################
#
# ON-TIME BACKUP
#
# assuming we have large enough "/data" folder
# and we use root user without password
# then database will zipped using GZIP
#! /bin/bash
BACKUP_DIR="/data"
MYSQL_USER="root"
MYSQL=/usr/bin/mysql
MYSQLDUMP=/usr/bin/mysqldump
databases=`$MYSQL --user=$MYSQL_USER -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"`
for db in $databases; do
$MYSQLDUMP --force --opt --user=$MYSQL_USER --databases $db | gzip > "$BACKUP_DIR/$db.gz"
done
####################################################################################################
####################################################################################################
#
# SCHEDULED BACKUP
#
# assuming we have large enough "/data" folder
# databases will stored under "today folder"
# then database will zipped using GZIP
#! /bin/bash
TIMESTAMP=$(date +"%F")
BACKUP_DIR="/data"
MYSQL_USER="mysql_user"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="mysql_password"
MYSQLDUMP=/usr/bin/mysqldump
mkdir -p "$BACKUP_DIR/$TIMESTAMP"
databases=`$MYSQL --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"`
for db in $databases; do
$MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip > "$BACKUP_DIR/$TIMESTAMP/$db.gz"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment