Created
April 8, 2018 06:54
-
-
Save mkdesignn/c540a66e74abe68d65c1dde06012b331 to your computer and use it in GitHub Desktop.
mysql backup shell script
This file contains hidden or 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 | |
# Basic configuration: datestamp e.g. YYYYMMDD | |
DATE=$(date +"%Y%m%d") | |
# Location of your backups (create the directory first!) | |
BACKUP_DIR="/home/backup/database" | |
# MySQL login details | |
MYSQL_USER="root" | |
MYSQL_PASSWORD="sys" | |
# MySQL executable locations (no need to change this) | |
MYSQL=/usr/bin/mysql | |
MYSQLDUMP=/usr/bin/mysqldump | |
# MySQL databases you wish to skip | |
SKIPDATABASES="Database|information_schema|performance_schema|mysql|sys" | |
# Number of days to keep the directories (older than X days will be removed) | |
RETENTION=14 | |
# ---- DO NOT CHANGE BELOW THIS LINE ------------------------------------------ | |
# | |
# Create a new directory into backup directory location for this date | |
mkdir -p $BACKUP_DIR/$DATE | |
# Retrieve a list of all databases | |
databases=`$MYSQL -u$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "($SKIPDATABASES)"` | |
# Dumb the databases in seperate names and gzip the .sql file | |
for db in $databases; do | |
echo $db | |
$MYSQLDUMP --force --routines --events -E --triggers --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD --skip-lock-tables --d$ | |
done | |
# Remove files older than X days | |
find $BACKUP_DIR/* -mtime +$RETENTION -delete | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment