Last active
September 7, 2018 09:39
-
-
Save kangmasjuqi/081bfd466df19cee3cd70c3f37a15634 to your computer and use it in GitHub Desktop.
working with mysql database backup
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
#################################################################################################### | |
#################################################################################################### | |
# | |
# 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