Skip to content

Instantly share code, notes, and snippets.

@djoudi
Forked from daif/mysql-backup.sh
Created October 9, 2020 13:47
Show Gist options
  • Save djoudi/d26b1e0c9634172e16712c6d7ba7ba2d to your computer and use it in GitHub Desktop.
Save djoudi/d26b1e0c9634172e16712c6d7ba7ba2d to your computer and use it in GitHub Desktop.
MySQL backup tool
#!/bin/bash
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
########################################################################
#
# Project: mysql-backup
# Version: 1.0.0
# Date: 2020-05-15
# Author: Daif Alazmi <[email protected]>
#
########################################################################
# Backup configurations
BACKUP_DIR='/mnt/databases'
BACKUP_USR='root'
BACKUP_PWD=''
BACKUP_DBS=`mysqlshow | grep "|" | tr -d ' ' | tr -d '|' | egrep -v Databases`
BACKUP_LIN=' '
########################################################################
# 1 - Create backup directory if not existed.
########################################################################
if [ ! -d "$BACKUP_DIR" ]; then
mkdir -p "$BACKUP_DIR";
fi
[ ! -d "$BACKUP_DIR" ] && echo -e "\033[0;31m \n>\n> Error: Backup directory $BACKUP_DIR is not exists! ... \n>\n\033[0m" && exit 1
########################################################################
# 2 - Start backup
########################################################################
echo -e "\033[0;33m \n### Start backup `date` ###\n\033[0m"
for db in $BACKUP_DBS
do
printf "\033[0;32m>> %s %s \033[0m" $db "${BACKUP_LIN:${#db}}"
mysqldump -u $BACKUP_USR -p$BACKUP_PWD "$db" | gzip -9 > "$BACKUP_DIR/$db-$(date +%F).sql.gz"
printf "\033[0;32m[Done]\n\033[0m"
done
echo -e "\033[0;33m \n### End backup `date` ###\n\033[0m"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment