first commit
This commit is contained in:
commit
423f55408f
|
@ -0,0 +1,150 @@
|
|||
#!/bin/bash
|
||||
##############################################################################################
|
||||
# #
|
||||
# 88 #
|
||||
# 88 #
|
||||
# 88 #
|
||||
# ,adPPYba, 8b,dPPYba, ,adPPYba, 88 ,adPPYba, ,adPPYba, 88,dPYba,,adPYba, #
|
||||
# I8[ "" 88P' `"8a a8P_____88 88 a8" "" a8" "8a 88P' "88" "8a #
|
||||
# `"Y8ba, 88 88 8PP""""""" 88 8b 8b d8 88 88 88 #
|
||||
# aa ]8I 88 88 "8b, ,aa 88 888 "8a, ,aa "8a, ,a8" 88 88 88 #
|
||||
# `"YbbdP"' 88 88 `"Ybbd8"' 88 888 `"Ybbd8"' `"YbbdP"' 88 88 88 #
|
||||
# #
|
||||
# #
|
||||
##############################################################################################
|
||||
# #
|
||||
# Author: Snel.com - Yavuz Aydin #
|
||||
# E-mail: support@snel.com #
|
||||
# (c) Snel.com - all rights reserved #
|
||||
# #
|
||||
##############################################################################################
|
||||
# #
|
||||
# Script: backup_mysql.sh #
|
||||
# License: This file is licensed under the GPLv3 License #
|
||||
# Purpose: Create local MySQL backups #
|
||||
# #
|
||||
##############################################################################################
|
||||
# #
|
||||
# Changelog: #
|
||||
# #
|
||||
##############################################################################################
|
||||
# #
|
||||
# 2019/01/21 1.5 Fixed creation of databases with dot (.) in name #
|
||||
# 2018/02/11 1.4 Fixed detection of mydumper #
|
||||
# 2017/11/09 1.3 Fixed multiple backups on a day support #
|
||||
# 2017/07/23 1.2 Added mydumper / myloader support #
|
||||
# 2016/02/19 1.1 Added --events to suppress warning #
|
||||
# 2016/02/06 1.0 First release #
|
||||
# #
|
||||
##############################################################################################
|
||||
# #
|
||||
# Restore #
|
||||
# #
|
||||
##############################################################################################
|
||||
# #
|
||||
# Assuming you want to restore mytestdb from your backups made on 20190121-1540: #
|
||||
# #
|
||||
# mysqldump: #
|
||||
# #
|
||||
# DB='mytestdb' #
|
||||
# BACKUPDIR='/var/backup/mysql/20190121-1540' #
|
||||
# mysql -e "CREATE DATABASE ${DB}" #
|
||||
# cd ${BACKUPDIR}/${DB} #
|
||||
# for table in *; do gunzip -c $table | mysql ${DB}; done #
|
||||
# #
|
||||
# With myloader (backups should have been created with mydumper): #
|
||||
# #
|
||||
# DB='mytestdb' #
|
||||
# BACKUPDIR='/var/backup/mysql/20190121-1540' #
|
||||
# myloader --directory=${BACKUPDIR}/ --overwrite-tables -s ${DB} #
|
||||
# #
|
||||
##############################################################################################
|
||||
# #
|
||||
# CONFIGURATION #
|
||||
# #
|
||||
##############################################################################################
|
||||
|
||||
# Get list of Databases except the pid file
|
||||
DBS_LIST=$(mysql -s -N -e "show databases;")
|
||||
# Log file
|
||||
BAKUP_LOG=/var/backup/log/mysql-backup.log
|
||||
# Backup Base directory
|
||||
BASE_BAK_FLDR=/var/backup/mysql
|
||||
# Backup rotation period.
|
||||
RM_FLDR_DAYS="+30"
|
||||
|
||||
##############################################################################################
|
||||
# #
|
||||
# DO NOT EDIT BELOW THIS #
|
||||
# #
|
||||
##############################################################################################
|
||||
|
||||
USE_MYDUMPER=0
|
||||
which mydumper &> /dev/null
|
||||
if [ "$?" == "0" ]; then
|
||||
MYDUMPER=$(which mydumper)
|
||||
USE_MYDUMPER=1
|
||||
fi
|
||||
|
||||
index=0
|
||||
# Check if we can connect to the mysql server; otherwise die
|
||||
if [ ! "$(id -u -n)" = "root" ]; then
|
||||
echo -e "Error:: $0 : Only user 'root' can run this script"
|
||||
exit 100
|
||||
fi
|
||||
PING=$(mysqladmin ping 2>/dev/null)
|
||||
if [ "$PING" != "mysqld is alive" ]; then
|
||||
echo "Error:: Unable to connected to MySQL Server, exiting !!"
|
||||
exit 101
|
||||
fi
|
||||
# Backup process starts here.
|
||||
START_DATETIME=$(date +%Y%m%d-%H%M)
|
||||
|
||||
# Flush logs prior to the backup.
|
||||
mysql -e "FLUSH LOGS"
|
||||
|
||||
if [ $USE_MYDUMPER -eq 1 ]; then
|
||||
# Use mydumper
|
||||
DB_BKP_FLDR=$BASE_BAK_FLDR/${START_DATETIME}
|
||||
[ ! -d $DB_BKP_FLDR ] && mkdir -p $DB_BKP_FLDR && chmod 700 $DB_BKP_FLDR
|
||||
$MYDUMPER -o $DB_BKP_FLDR -G -E -R --less-locking -r 500000 -c -e -L $DB_BKP_FLDR/mydumper.log -v 3
|
||||
else
|
||||
# Use traditional mysqldump
|
||||
# Loop through the DB list and create table level backup,
|
||||
# applying appropriate option for MyISAM and InnoDB tables.
|
||||
for DB in $DBS_LIST; do
|
||||
DB_BKP_FLDR=$BASE_BAK_FLDR/${START_DATETIME}/$DB
|
||||
[ ! -d $DB_BKP_FLDR ] && mkdir -p $DB_BKP_FLDR && chmod 700 $DB_BKP_FLDR
|
||||
# Get the schema of database with the stored procedures.
|
||||
# This will be the first file in the database backup folder
|
||||
mysqldump -R -d --single-transaction $DB | gzip -c > $DB_BKP_FLDR/000-DB_SCHEMA.sql.gz
|
||||
index=0
|
||||
#Get the tables and its type. Store it in an array.
|
||||
table_types=($(mysql -e "show table status from $DB" | awk '{ if ($2 == "MyISAM" || $2 == "InnoDB") print $1,$2}'))
|
||||
table_type_count=${#table_types[@]}
|
||||
# Loop through the tables and apply the mysqldump option according to the table type
|
||||
# The table specific SQL files will not contain any create info for the table schema.
|
||||
# It will be available in SCHEMA file
|
||||
while [ "$index" -lt "$table_type_count" ]; do
|
||||
START=$(date +%s)
|
||||
TYPE=${table_types[$index + 1]}
|
||||
table=${table_types[$index]}
|
||||
#echo -en "$(date) : backup $DB : $table : $TYPE "
|
||||
if [ "$TYPE" = "MyISAM" ]; then
|
||||
DUMP_OPT="$DB --no-create-info --tables --events --quote-names "
|
||||
else
|
||||
DUMP_OPT="$DB --no-create-info --single-transaction --tables --events --quote-names "
|
||||
fi
|
||||
mysqldump $DUMP_OPT $table |gzip -c > $DB_BKP_FLDR/$table.sql.gz
|
||||
index=$(($index + 2))
|
||||
#echo -e " - Total time : $(($(date +%s) - $START))\n"
|
||||
done
|
||||
done
|
||||
fi
|
||||
|
||||
# Rotating old backup. according to the 'RM_FLDR_DAYS'
|
||||
if [ ! -z "$RM_FLDR_DAYS" ]; then
|
||||
#echo -en "$(date) : removing folder : "
|
||||
find $BASE_BAK_FLDR/ -maxdepth 1 -mtime $RM_FLDR_DAYS -type d -exec rm -rf {} \;
|
||||
#echo
|
||||
fi
|
Loading…
Reference in New Issue