=Add split file for fast restore db; fixed path; check scripts on macos
This commit is contained in:
parent
f812f1c8ba
commit
ee7c575e57
94
backup.sh
94
backup.sh
|
@ -24,38 +24,6 @@ f_log()
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
usage()
|
|
||||||
{
|
|
||||||
cat << EOF
|
|
||||||
|
|
||||||
This mysql backup engine.
|
|
||||||
|
|
||||||
Usage: $0 <[options]> or bash $0 <[options]>
|
|
||||||
|
|
||||||
Options:
|
|
||||||
-e= | --exclude= Exclude databases
|
|
||||||
--exclude-tables= Exclude tables
|
|
||||||
--exclude-data-tables= Exclude data tables
|
|
||||||
-c= | --compress= Compress with gzip or bzip2
|
|
||||||
-v | --verbose Add verbose into output
|
|
||||||
-l | --lifetime= Lifetime for dump files
|
|
||||||
--config= Config file of Debian format
|
|
||||||
--dir= Backup directory
|
|
||||||
-h | --help This text
|
|
||||||
|
|
||||||
Examples:
|
|
||||||
backup.sh --verbose --compress=
|
|
||||||
backup.sh --verbose --compress=zgip
|
|
||||||
backup.sh --verbose --compress=bzip2
|
|
||||||
backup.sh --verbose --compress= --exclude="mysql"
|
|
||||||
backup.sh --verbose --compress= --exclude="mysql" --lifetime="3 day ago"
|
|
||||||
backup.sh --verbose --config="/etc/mysql/debian.cnf" --exclude="mysql" --lifetime="1 day ago"
|
|
||||||
backup.sh --verbose --dir="/var/backups/mysql" --config="/etc/mysql/debian.cnf" --exclude="mysql" --lifetime="1 day ago"
|
|
||||||
backup.sh --verbose --dir="/home/backups/mysql" --exclude="mysql" --lifetime="1 day ago"
|
|
||||||
backup.sh --verbose --dir="/home/backups/mysql" --exclude="mysql" --exclude-tables="tbl_template" --lifetime="1 day ago"
|
|
||||||
EOF
|
|
||||||
}
|
|
||||||
|
|
||||||
prepaire_skip_expression()
|
prepaire_skip_expression()
|
||||||
{
|
{
|
||||||
local array_skip=( "${@}" )
|
local array_skip=( "${@}" )
|
||||||
|
@ -88,7 +56,7 @@ backup()
|
||||||
|
|
||||||
for BDD in $(mysql --defaults-extra-file=$CONFIG_FILE --skip-column-names -B -e "$query" | egrep -v "$database_exclude_expression"); do
|
for BDD in $(mysql --defaults-extra-file=$CONFIG_FILE --skip-column-names -B -e "$query" | egrep -v "$database_exclude_expression"); do
|
||||||
|
|
||||||
touch $DST/$BDD/error.log
|
touch $DST/$BDD/error.log
|
||||||
|
|
||||||
mkdir -p $DST/$BDD 2>/dev/null 1>&2
|
mkdir -p $DST/$BDD 2>/dev/null 1>&2
|
||||||
chown $USER:$GROUP $DST/$BDD
|
chown $USER:$GROUP $DST/$BDD
|
||||||
|
@ -154,19 +122,19 @@ backup()
|
||||||
|
|
||||||
if [ $COMPRESS == 'bzip2' ]; then
|
if [ $COMPRESS == 'bzip2' ]; then
|
||||||
|
|
||||||
if [ -f "$DST/$BDD/$TABLE.txt.bz2" ]; then
|
if [ -f "$DST/$BDD/$TABLE.txt.bz2" ]; then
|
||||||
rm $DST/$BDD/$TABLE.txt.bz2
|
rm $DST/$BDD/$TABLE.txt.bz2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
($COMPRESS $DST/$BDD/$TABLE.txt && chmod $FILEATTRIBUTES $DST/$BDD/$TABLE.txt.bz2 && chown $USER:$GROUP $DST/$BDD/$TABLE.txt.bz2) &
|
($COMPRESS $DST/$BDD/$TABLE.txt && chown $USER:$GROUP $DST/$BDD/$TABLE.txt.bz2 && chmod $FILEATTRIBUTES $DST/$BDD/$TABLE.txt.bz2) &
|
||||||
|
|
||||||
elif [ $COMPRESS == 'gzip' ]; then
|
elif [ $COMPRESS == 'gzip' ]; then
|
||||||
|
|
||||||
if [ -f "$DST/$BDD/$TABLE.txt.gz" ]; then
|
if [ -f "$DST/$BDD/$TABLE.txt.gz" ]; then
|
||||||
rm $DST/$BDD/$TABLE.txt.gz
|
rm $DST/$BDD/$TABLE.txt.gz
|
||||||
fi
|
fi
|
||||||
|
|
||||||
($COMPRESS $DST/$BDD/$TABLE.txt && chmod $FILEATTRIBUTES $DST/$BDD/$TABLE.txt.gz && chown $USER:$GROUP $DST/$BDD/$TABLE.txt.gz) &
|
($COMPRESS $DST/$BDD/$TABLE.txt && chown $USER:$GROUP $DST/$BDD/$TABLE.txt.gz && chmod $FILEATTRIBUTES $DST/$BDD/$TABLE.txt.gz) &
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -183,6 +151,38 @@ backup()
|
||||||
f_log " END "
|
f_log " END "
|
||||||
}
|
}
|
||||||
|
|
||||||
|
usage()
|
||||||
|
{
|
||||||
|
cat << EOF
|
||||||
|
|
||||||
|
This mysql backup engine.
|
||||||
|
|
||||||
|
Usage: $0 <[options]> or bash $0 <[options]>
|
||||||
|
|
||||||
|
Options:
|
||||||
|
-e= | --exclude= Exclude databases
|
||||||
|
--exclude-tables= Exclude tables
|
||||||
|
--exclude-data-tables= Exclude data tables
|
||||||
|
-c= | --compress= Compress with gzip or bzip2
|
||||||
|
-v | --verbose Add verbose into output
|
||||||
|
-l | --lifetime= Lifetime for dump files
|
||||||
|
--config= Config file of Debian format
|
||||||
|
--dir= Backup directory
|
||||||
|
-h | --help This text
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
backup.sh --verbose --compress=
|
||||||
|
backup.sh --verbose --compress=zgip
|
||||||
|
backup.sh --verbose --compress=bzip2
|
||||||
|
backup.sh --verbose --compress= --exclude="mysql"
|
||||||
|
backup.sh --verbose --compress= --exclude="mysql" --lifetime="3 day ago"
|
||||||
|
backup.sh --verbose --config="/etc/mysql/debian.cnf" --exclude="mysql" --lifetime="1 day ago"
|
||||||
|
backup.sh --verbose --dir="/var/backups/mysql" --config="/etc/mysql/debian.cnf" --exclude="mysql" --lifetime="1 day ago"
|
||||||
|
backup.sh --verbose --dir="/home/backups/mysql" --exclude="mysql" --lifetime="1 day ago"
|
||||||
|
backup.sh --verbose --dir="/home/backups/mysql" --exclude="mysql" --exclude-tables="tbl_template" --lifetime="1 day ago"
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
if [ $# = 0 ]; then
|
if [ $# = 0 ]; then
|
||||||
usage;
|
usage;
|
||||||
exit;
|
exit;
|
||||||
|
@ -194,12 +194,6 @@ EXCLUDE_DATA_TABLES=''
|
||||||
BIN_DEPS="mysql mysqldump $COMPRESS"
|
BIN_DEPS="mysql mysqldump $COMPRESS"
|
||||||
|
|
||||||
# === CHECKS ===
|
# === CHECKS ===
|
||||||
if [ -f '/etc/debian_version' ]; then
|
|
||||||
CONFIG_FILE='/etc/mysql/debian.cnf'
|
|
||||||
else
|
|
||||||
CONFIG_FILE='~/mysql_utils/etc/mysql/debian.cnf'
|
|
||||||
fi
|
|
||||||
|
|
||||||
for BIN in $BIN_DEPS; do
|
for BIN in $BIN_DEPS; do
|
||||||
which $BIN 1>/dev/null 2>&1
|
which $BIN 1>/dev/null 2>&1
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
|
@ -208,6 +202,12 @@ for BIN in $BIN_DEPS; do
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
if [ -f '/etc/debian_version' ]; then
|
||||||
|
CONFIG_FILE='/etc/mysql/debian.cnf'
|
||||||
|
else
|
||||||
|
CONFIG_FILE='~/mysql_utils/etc/mysql/debian.cnf'
|
||||||
|
fi
|
||||||
|
|
||||||
for i in "$@"
|
for i in "$@"
|
||||||
do
|
do
|
||||||
case $i in
|
case $i in
|
||||||
|
|
|
@ -14,6 +14,13 @@ CONFIG_FILE='/etc/mysql/debian.cnf'
|
||||||
|
|
||||||
if [ ! -n "$BASH" ] ;then echo Please run this script $0 with bash; exit 1; fi
|
if [ ! -n "$BASH" ] ;then echo Please run this script $0 with bash; exit 1; fi
|
||||||
|
|
||||||
|
# === FUNCTIONS ===
|
||||||
|
if [ -f '/etc/debian_version' ]; then
|
||||||
|
CONFIG_FILE='/etc/mysql/debian.cnf'
|
||||||
|
else
|
||||||
|
CONFIG_FILE='~/mysql_utils/etc/mysql/debian.cnf'
|
||||||
|
fi
|
||||||
|
|
||||||
# === FUNCTION ===
|
# === FUNCTION ===
|
||||||
f_log()
|
f_log()
|
||||||
{
|
{
|
||||||
|
@ -24,37 +31,6 @@ f_log()
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
usage()
|
|
||||||
{
|
|
||||||
cat << EOF
|
|
||||||
|
|
||||||
This mysql backup engine.
|
|
||||||
|
|
||||||
Usage: $0 <[database-name]> <[options]> or bash $0 <[database-name]> <[options]>
|
|
||||||
|
|
||||||
Options:
|
|
||||||
--exclude-tables= Exclude tables
|
|
||||||
--exclude-data-tables= Exclude data tables
|
|
||||||
-c= | --compress= Compress with gzip or bzip2
|
|
||||||
-v | --verbose Add verbose into output
|
|
||||||
-l | --lifetime= Lifetime for dump files
|
|
||||||
--config= Config file of Debian format
|
|
||||||
--dir= Backup directory
|
|
||||||
-h | --help This text
|
|
||||||
|
|
||||||
Examples:
|
|
||||||
backup.sh --verbose --compress=
|
|
||||||
backup.sh --verbose --compress=zgip
|
|
||||||
backup.sh --verbose --compress=bzip2
|
|
||||||
backup.sh --verbose --compress="
|
|
||||||
backup.sh --verbose --compress= --lifetime="3 day ago"
|
|
||||||
backup.sh --verbose --config="/etc/mysql/debian.cnf" --lifetime="1 day ago"
|
|
||||||
backup.sh --verbose --dir="/var/backups/mysql" --config="/etc/mysql/debian.cnf" --lifetime="1 day ago"
|
|
||||||
backup.sh --verbose --dir="/home/backups/mysql" --lifetime="1 day ago"
|
|
||||||
backup.sh --verbose --dir="/home/backups/mysql" --exclude-tables="tbl_template" --lifetime="1 day ago"
|
|
||||||
EOF
|
|
||||||
}
|
|
||||||
|
|
||||||
prepaire_skip_expression()
|
prepaire_skip_expression()
|
||||||
{
|
{
|
||||||
local array_skip=( "${@}" )
|
local array_skip=( "${@}" )
|
||||||
|
@ -174,6 +150,37 @@ backup()
|
||||||
f_log " END "
|
f_log " END "
|
||||||
}
|
}
|
||||||
|
|
||||||
|
usage()
|
||||||
|
{
|
||||||
|
cat << EOF
|
||||||
|
|
||||||
|
This mysql backup engine.
|
||||||
|
|
||||||
|
Usage: $0 <[database-name]> <[options]> or bash $0 <[database-name]> <[options]>
|
||||||
|
|
||||||
|
Options:
|
||||||
|
--exclude-tables= Exclude tables
|
||||||
|
--exclude-data-tables= Exclude data tables
|
||||||
|
-c= | --compress= Compress with gzip or bzip2
|
||||||
|
-v | --verbose Add verbose into output
|
||||||
|
-l | --lifetime= Lifetime for dump files
|
||||||
|
--config= Config file of Debian format
|
||||||
|
--dir= Backup directory
|
||||||
|
-h | --help This text
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
backup.sh --verbose --compress=
|
||||||
|
backup.sh --verbose --compress=zgip
|
||||||
|
backup.sh --verbose --compress=bzip2
|
||||||
|
backup.sh --verbose --compress=
|
||||||
|
backup.sh --verbose --compress= --lifetime="3 day ago"
|
||||||
|
backup.sh --verbose --config="/etc/mysql/debian.cnf" --lifetime="1 day ago"
|
||||||
|
backup.sh --verbose --dir="/var/backups/mysql" --config="/etc/mysql/debian.cnf" --lifetime="1 day ago"
|
||||||
|
backup.sh --verbose --dir="/home/backups/mysql" --lifetime="1 day ago"
|
||||||
|
backup.sh --verbose --dir="/home/backups/mysql" --exclude-tables="tbl_template" --lifetime="1 day ago"
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
if [ $# = 0 ]; then
|
if [ $# = 0 ]; then
|
||||||
usage;
|
usage;
|
||||||
exit;
|
exit;
|
||||||
|
@ -184,12 +191,7 @@ EXCLUDE_TABLES=''
|
||||||
EXCLUDE_DATA_TABLES=''
|
EXCLUDE_DATA_TABLES=''
|
||||||
BIN_DEPS="mysql mysqldump $COMPRESS"
|
BIN_DEPS="mysql mysqldump $COMPRESS"
|
||||||
|
|
||||||
# === CHECKS ===
|
DATABASE="$1"
|
||||||
if [ "$#" -ne 1 ]; then
|
|
||||||
echo "Illegal number of parameters"
|
|
||||||
else
|
|
||||||
DATABASE="$1"
|
|
||||||
fi
|
|
||||||
|
|
||||||
for BIN in $BIN_DEPS; do
|
for BIN in $BIN_DEPS; do
|
||||||
which $BIN 1>/dev/null 2>&1
|
which $BIN 1>/dev/null 2>&1
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
# Automatically generated for Debian scripts. DO NOT TOUCH!
|
# Automatically generated for Debian scripts. DO NOT TOUCH!
|
||||||
[client]
|
[client]
|
||||||
host = localhost
|
host = localhost
|
||||||
user = debian-sys-maint
|
user = root
|
||||||
password =
|
password = password
|
||||||
socket = /var/run/mysqld/mysqld.sock
|
socket = /usr/local/var/run/mysqld.sock
|
||||||
[mysql_upgrade]
|
[mysql_upgrade]
|
||||||
host = localhost
|
host = localhost
|
||||||
user = debian-sys-maint
|
user = debian-sys-maint
|
||||||
|
|
|
@ -1,22 +1,13 @@
|
||||||
#!/bin/sh
|
#!/bin/bash
|
||||||
|
|
||||||
# === CONFIG ===
|
# === CONFIG ===
|
||||||
DIR_PWD=$(pwd)
|
CONFIG_CHUNK=1000000
|
||||||
BIN_DEPS='bunzip2 bzip2 mysql'
|
|
||||||
MYCNF='/etc/mysql/debian.cnf'
|
|
||||||
MYDATA='/var/lib/mysql'
|
|
||||||
|
|
||||||
# === DO NOT EDIT BELOW THIS LINE ===
|
# === DO NOT EDIT BELOW THIS LINE ===
|
||||||
|
|
||||||
if [ ! -n "$BASH" ] ;then echo Please run this script $0 with bash; exit 1; fi
|
if [ ! -n "$BASH" ] ;then echo Please run this script $0 with bash; exit 1; fi
|
||||||
|
|
||||||
# === FUNCTIONS ===
|
# === FUNCTIONS ===
|
||||||
if [ -f '/etc/debian_version' ]; then
|
|
||||||
CONFIG_FILE='/etc/mysql/debian.cnf'
|
|
||||||
else
|
|
||||||
CONFIG_FILE='~/mysql_utils/etc/mysql/debian.cnf'
|
|
||||||
fi
|
|
||||||
|
|
||||||
database_exists()
|
database_exists()
|
||||||
{
|
{
|
||||||
RESULT=`mysqlshow --defaults-extra-file=$MYCNF $@| grep -v Wildcard | grep -o $@`
|
RESULT=`mysqlshow --defaults-extra-file=$MYCNF $@| grep -v Wildcard | grep -o $@`
|
||||||
|
@ -32,22 +23,12 @@ f_log()
|
||||||
logger "RESTORE: $@"
|
logger "RESTORE: $@"
|
||||||
}
|
}
|
||||||
|
|
||||||
usage()
|
|
||||||
{
|
|
||||||
cat << EOF
|
|
||||||
usage: $0 options
|
|
||||||
|
|
||||||
This script restore databases.
|
|
||||||
|
|
||||||
OPTIONS:
|
|
||||||
-e Exclude databases
|
|
||||||
-s Selected databases
|
|
||||||
-c Check innochecksum of table after import
|
|
||||||
EOF
|
|
||||||
}
|
|
||||||
|
|
||||||
restore()
|
restore()
|
||||||
{
|
{
|
||||||
|
DIR=$@
|
||||||
|
|
||||||
|
f_log "Check path $DIR"
|
||||||
|
|
||||||
f_log "** START **"
|
f_log "** START **"
|
||||||
|
|
||||||
f_log "Check runtime"
|
f_log "Check runtime"
|
||||||
|
@ -60,12 +41,12 @@ restore()
|
||||||
done
|
done
|
||||||
|
|
||||||
f_log "Check backups folder"
|
f_log "Check backups folder"
|
||||||
if [ "$(ls -1 $DIR_PWD/*/__create.sql 2>/dev/null | wc -l)" -le "0" ]; then
|
if [ "$(ls -1 $DIR/*/__create.sql 2>/dev/null | wc -l)" -le "0" ]; then
|
||||||
f_log "Your must run script from backup directory"
|
f_log "Your must run script from backup directory"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
for i in $(ls -1 -d $DIR_PWD/*); do
|
for i in $(ls -1 -d $DIR/*); do
|
||||||
|
|
||||||
BDD=$(basename $i)
|
BDD=$(basename $i)
|
||||||
|
|
||||||
|
@ -87,9 +68,9 @@ restore()
|
||||||
|
|
||||||
if [ $BDD ]; then
|
if [ $BDD ]; then
|
||||||
|
|
||||||
if [ -f $DIR_PWD/$BDD/__create.sql ]; then
|
if [ -f $DIR/$BDD/__create.sql ]; then
|
||||||
f_log "Create database $BDD"
|
f_log "Create database $BDD"
|
||||||
mysql --defaults-extra-file=$MYCNF < $DIR_PWD/$BDD/__create.sql 2>/dev/null
|
time mysql --defaults-extra-file=$MYCNF < $DIR/$BDD/__create.sql 2>/dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ $(database_exists $BDD) != "YES" ]; then
|
if [ $(database_exists $BDD) != "YES" ]; then
|
||||||
|
@ -101,57 +82,60 @@ restore()
|
||||||
f_log "Create tables in $BDD"
|
f_log "Create tables in $BDD"
|
||||||
for TABLE in $tables; do
|
for TABLE in $tables; do
|
||||||
f_log "Create table: $BDD/$TABLE"
|
f_log "Create table: $BDD/$TABLE"
|
||||||
mysql --defaults-extra-file=$MYCNF $BDD -e "SET foreign_key_checks = 0;
|
time mysql --defaults-extra-file=$MYCNF $BDD -e "SET foreign_key_checks = 0;
|
||||||
DROP TABLE IF EXISTS $TABLE;
|
DROP TABLE IF EXISTS $TABLE;
|
||||||
SOURCE $DIR_PWD/$BDD/$TABLE.sql;
|
SOURCE $DIR/$BDD/$TABLE.sql;
|
||||||
SET foreign_key_checks = 1;"
|
SET foreign_key_checks = 1;"
|
||||||
done
|
done
|
||||||
|
|
||||||
f_log "Import data into $BDD"
|
f_log "Import data into $BDD"
|
||||||
for TABLE in $tables; do
|
for TABLE in $tables; do
|
||||||
f_log "Import data into $BDD/$TABLE"
|
f_log "Import data into $BDD/$TABLE"
|
||||||
|
|
||||||
if [ -f "$DIR_PWD/$BDD/$TABLE.txt.bz2" ]; then
|
if [ -f "$DIR/$BDD/$TABLE.txt.bz2" ]; then
|
||||||
f_log "< $TABLE"
|
f_log "< $TABLE"
|
||||||
if [ -f "$DIR_PWD/$BDD/$TABLE.txt" ]; then
|
if [ -f "$DIR/$BDD/$TABLE.txt" ]; then
|
||||||
rm $DIR_PWD/$BDD/$TABLE.txt
|
rm $DIR/$BDD/$TABLE.txt
|
||||||
fi
|
fi
|
||||||
bunzip2 $DIR_PWD/$BDD/$TABLE.txt.bz2
|
bunzip2 $DIR/$BDD/$TABLE.txt.bz2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -f "$DIR_PWD/$BDD/$TABLE.txt" ]; then
|
if [ -f "$DIR/$BDD/$TABLE.txt" ]; then
|
||||||
f_log "+ $TABLE"
|
f_log "+ $TABLE"
|
||||||
mysql --defaults-extra-file=$MYCNF $BDD --local-infile -e "SET foreign_key_checks = 0; SET unique_checks = 0; SET sql_log_bin = 0;
|
split -l $CONFIG_CHUNK "$DIR/$TABLE.txt" ${TABLE}_part_
|
||||||
LOAD DATA LOCAL INFILE '$DIR_PWD/$BDD/$TABLE.txt'
|
for segment in ${TABLE}_part_*; do
|
||||||
INTO TABLE $TABLE;
|
time mysql --defaults-extra-file=$MYCNF $BDD --local-infile -e "SET foreign_key_checks = 0; SET unique_checks = 0; SET sql_log_bin = 0;
|
||||||
SET foreign_key_checks = 1; SET unique_checks = 1; SET sql_log_bin = 1;"
|
LOAD DATA LOCAL INFILE '$DIR/$BDD/$TABLE.txt'
|
||||||
|
INTO TABLE $TABLE;
|
||||||
if [ ! -f "$DIR_PWD/$BDD/$TABLE.txt.bz2" ]; then
|
SET foreign_key_checks = 1; SET unique_checks = 1; SET sql_log_bin = 1;"
|
||||||
f_log "> $TABLE"
|
rm $segment
|
||||||
bzip2 $DIR_PWD/$BDD/$TABLE.txt
|
done
|
||||||
fi
|
if [ ! -f "$DIR/$BDD/$TABLE.txt.bz2" ]; then
|
||||||
fi
|
f_log "> $TABLE"
|
||||||
|
bzip2 $DIR/$BDD/$TABLE.txt
|
||||||
if [ $DATABASES_TABLE_CHECK ]; then
|
fi
|
||||||
# Check INNODB table
|
fi
|
||||||
if [ -f "$MYDATA/$BDD/$TABLE.ibd" ]; then
|
|
||||||
if [ ! $(innochecksum $MYDATA/$BDD/$TABLE.ibd) ]; then
|
if [ $DATABASES_TABLE_CHECK ]; then
|
||||||
f_log "$TABLE [OK]"
|
if [ -f "$DIR/$BDD/$TABLE.ibd" ]; then
|
||||||
else
|
if [ ! $(innochecksum $DIR/$BDD/$TABLE.ibd) ]; then
|
||||||
f_log "$TABLE [ERR]"
|
f_log "$TABLE [OK]"
|
||||||
fi
|
else
|
||||||
fi
|
f_log "$TABLE [ERR]"
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ -f "$DIR_PWD/$BDD/__routines.sql" ]; then
|
if [ -f "$DIR/$BDD/__routines.sql" ]; then
|
||||||
f_log "Import routines into $BDD"
|
f_log "Import routines into $BDD"
|
||||||
mysql --defaults-extra-file=$MYCNF $BDD < $DIR_PWD/$BDD/__routines.sql 2>/dev/null
|
time mysql --defaults-extra-file=$MYCNF $BDD < $DIR/$BDD/__routines.sql 2>/dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -f "$DIR_PWD/$BDD/__views.sql" ]; then
|
if [ -f "$DIR/$BDD/__views.sql" ]; then
|
||||||
f_log "Import views into $BDD"
|
f_log "Import views into $BDD"
|
||||||
mysql --defaults-extra-file=$MYCNF $BDD < $DIR_PWD/$BDD/__views.sql 2>/dev/null
|
time mysql --defaults-extra-file=$MYCNF $BDD < $DIR/$BDD/__views.sql 2>/dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
@ -159,33 +143,80 @@ restore()
|
||||||
done
|
done
|
||||||
|
|
||||||
f_log "Flush privileges;"
|
f_log "Flush privileges;"
|
||||||
mysql --defaults-extra-file=$MYCNF -e "flush privileges;"
|
time mysql --defaults-extra-file=$MYCNF -e "flush privileges;"
|
||||||
|
|
||||||
f_log "** END **"
|
f_log "** END **"
|
||||||
}
|
}
|
||||||
|
|
||||||
# === EXECUTE ===
|
usage()
|
||||||
while getopts ":e:c:s:" opt;
|
{
|
||||||
do
|
cat << EOF
|
||||||
case ${opt} in
|
usage: $0 options
|
||||||
e)
|
|
||||||
exclude=${OPTARG}
|
This script restore databases.
|
||||||
IFS=, read -r -a DATABASES_SKIP <<< "$exclude"
|
|
||||||
;;
|
OPTIONS:
|
||||||
s)
|
-e Exclude databases
|
||||||
selected=${OPTARG}
|
-s Selected databases
|
||||||
IFS=, read -r -a DATABASES_SELECTED <<< "$selected"
|
-c Check innochecksum of table after import
|
||||||
;;
|
EOF
|
||||||
c)
|
}
|
||||||
DATABASES_TABLE_CHECK=1
|
|
||||||
;;
|
# === CHECKS ===
|
||||||
*)
|
if [ $# = 0 ]; then
|
||||||
usage
|
usage;
|
||||||
exit 1
|
exit;
|
||||||
;;
|
fi
|
||||||
esac
|
|
||||||
|
BIN_DEPS="ls grep awk sort uniq bunzip2 bzip2 mysql"
|
||||||
|
|
||||||
|
if [ -f '/etc/debian_version' ]; then
|
||||||
|
CONFIG_FILE='/etc/mysql/debian.cnf'
|
||||||
|
else
|
||||||
|
CONFIG_FILE='~/mysql_utils/etc/mysql/debian.cnf'
|
||||||
|
fi
|
||||||
|
|
||||||
|
for BIN in $BIN_DEPS; do
|
||||||
|
which $BIN 1>/dev/null 2>&1
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "Error: Required command file not be found: $BIN"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
for i in "$@"
|
||||||
|
do
|
||||||
|
case $i in
|
||||||
|
-e)
|
||||||
|
DATABASES_SKIP=( "${i#*=}" )
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-s)
|
||||||
|
DATABASES_SELECTED=( "${i#*=}" )
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-c)
|
||||||
|
DATABASES_TABLE_CHECK=1
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
--config=*)
|
||||||
|
CONFIG_FILE=( "${i#*=}" )
|
||||||
|
shift # past argument=value
|
||||||
|
;;
|
||||||
|
-v | --verbose)
|
||||||
|
VERBOSE=1
|
||||||
|
shift # past argument=value
|
||||||
|
;;
|
||||||
|
-h | --help)
|
||||||
|
usage
|
||||||
|
exit
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
# unknown option
|
||||||
|
;;
|
||||||
|
esac
|
||||||
done
|
done
|
||||||
#shift "$((OPTIND - 1))"
|
|
||||||
|
|
||||||
# === AUTORUN ===
|
# === AUTORUN ===
|
||||||
restore
|
restore $(pwd)
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,13 @@
|
||||||
#!/bin/sh
|
#!/bin/bash
|
||||||
|
|
||||||
# === CONFIG ===
|
# === CONFIG ===
|
||||||
DIR_PWD=$(pwd)
|
CONFIG_CHUNK=1000000
|
||||||
BIN_DEPS='bunzip2 bzip2 mysql'
|
|
||||||
MYCNF='/etc/mysql/debian.cnf'
|
|
||||||
DATABASES_TABLE_CHECK=1
|
|
||||||
|
|
||||||
# === DO NOT EDIT BELOW THIS LINE ===
|
# === DO NOT EDIT BELOW THIS LINE ===
|
||||||
|
|
||||||
if [ ! -n "$BASH" ] ;then echo Please run this script $0 with bash; exit 1; fi
|
if [ ! -n "$BASH" ] ;then echo Please run this script $0 with bash; exit 1; fi
|
||||||
|
|
||||||
# === FUNCTIONS ===
|
# === FUNCTIONS ===
|
||||||
if [ -f '/etc/debian_version' ]; then
|
|
||||||
CONFIG_FILE='/etc/mysql/debian.cnf'
|
|
||||||
else
|
|
||||||
CONFIG_FILE='~/mysql_utils/etc/mysql/debian.cnf'
|
|
||||||
fi
|
|
||||||
|
|
||||||
f_log()
|
f_log()
|
||||||
{
|
{
|
||||||
echo "RESTORE: $@"
|
echo "RESTORE: $@"
|
||||||
|
@ -24,80 +15,87 @@ f_log()
|
||||||
|
|
||||||
restore()
|
restore()
|
||||||
{
|
{
|
||||||
PATH=$@
|
DIR=$@
|
||||||
|
|
||||||
f_log "Check path $PATH"
|
f_log "Check path $DIR"
|
||||||
|
|
||||||
|
f_log "** START **"
|
||||||
|
|
||||||
BDD=${PATH##*/}
|
BDD=${DIR##*/}
|
||||||
|
|
||||||
if [ $BDD ]; then
|
if [ $BDD ]; then
|
||||||
|
|
||||||
f_log "Found backup files $PATH"
|
f_log "Found backup files $DIR"
|
||||||
|
|
||||||
if [ -f $PATH/__create.sql ]; then
|
if [ -f $DIR/__create.sql ]; then
|
||||||
f_log "Create database $BDD"
|
f_log "Create database $BDD"
|
||||||
/usr/bin/mysql --defaults-extra-file=$MYCNF < $PATH/__create.sql 2>/dev/null
|
time mysql --defaults-extra-file=$CONFIG_FILE < $DIR/__create.sql 2>/dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
tables=$(/bin/ls -1 $PATH | /bin/grep -v __ | /usr/bin/awk -F. '{print $1}' | /usr/bin/sort | /usr/bin/uniq)
|
tables=$(ls -1 $DIR | grep -v __ | awk -F. '{print $1}' | sort | uniq)
|
||||||
|
|
||||||
f_log "Create tables in $BDD"
|
f_log "Create tables in $BDD"
|
||||||
for TABLE in $tables; do
|
for TABLE in $tables; do
|
||||||
f_log "Create table: $BDD/$TABLE"
|
f_log "Create table: $BDD/$TABLE"
|
||||||
/usr/bin/mysql --defaults-extra-file=$MYCNF $BDD -e "SET foreign_key_checks = 0;
|
time mysql --defaults-extra-file=$CONFIG_FILE $BDD -e "SET foreign_key_checks = 0;
|
||||||
DROP TABLE IF EXISTS $TABLE;
|
DROP TABLE IF EXISTS $TABLE;
|
||||||
SOURCE $PATH/$TABLE.sql;
|
SOURCE $DIR/$TABLE.sql;
|
||||||
SET foreign_key_checks = 1;"
|
SET foreign_key_checks = 1;"
|
||||||
done
|
done
|
||||||
|
|
||||||
f_log "Import data into $BDD"
|
f_log "Import data into $BDD"
|
||||||
for TABLE in $tables; do
|
for TABLE in $tables; do
|
||||||
f_log "Import data into $BDD/$TABLE"
|
|
||||||
if [ -f "$PATH/$TABLE.txt.bz2" ]; then
|
f_log "Import data into $BDD/$TABLE"
|
||||||
f_log "< $TABLE"
|
if [ -f "$DIR/$TABLE.txt.bz2" ]; then
|
||||||
if [ -f "$PATH/$TABLE.txt" ]; then
|
f_log "< $TABLE"
|
||||||
f_log "rm $PATH/$TABLE.txt"
|
if [ -f "$DIR/$TABLE.txt" ]; then
|
||||||
/bin/rm $PATH/$TABLE.txt
|
f_log "rm $DIR/$TABLE.txt"
|
||||||
fi
|
rm $DIR/$TABLE.txt
|
||||||
/bin/bunzip2 $PATH/$TABLE.txt.bz2
|
fi
|
||||||
fi
|
bunzip2 $DIR/$TABLE.txt.bz2
|
||||||
|
fi
|
||||||
|
|
||||||
if [ -f "$PATH/$TABLE.txt" ]; then
|
if [ -f "$DIR/$TABLE.txt" ]; then
|
||||||
f_log "+ $TABLE"
|
f_log "+ $TABLE"
|
||||||
/usr/bin/mysql --defaults-extra-file=$MYCNF $BDD --local-infile -e "SET foreign_key_checks = 0; SET unique_checks = 0; SET sql_log_bin = 0;
|
split -l $CONFIG_CHUNK "$DIR/$TABLE.txt" ${TABLE}_part_
|
||||||
LOAD DATA LOCAL INFILE '$PATH/$TABLE.txt'
|
for segment in ${TABLE}_part_*; do
|
||||||
INTO TABLE $TABLE;
|
time mysql --defaults-extra-file=$CONFIG_FILE $BDD --local-infile -e "SET foreign_key_checks = 0; SET unique_checks = 0; SET sql_log_bin = 0;
|
||||||
SET foreign_key_checks = 1; SET unique_checks = 1; SET sql_log_bin = 1;"
|
LOAD DATA LOCAL INFILE '$segment'
|
||||||
|
INTO TABLE $TABLE;
|
||||||
if [ ! -f "$PATH/$TABLE.txt.bz2" ]; then
|
SET foreign_key_checks = 1; SET unique_checks = 1; SET sql_log_bin = 1;"
|
||||||
f_log "> $TABLE"
|
rm $segment
|
||||||
/bin/bzip2 $PATH/$TABLE.txt
|
done
|
||||||
fi
|
if [ ! -f "$DIR/$TABLE.txt.bz2" ]; then
|
||||||
fi
|
f_log "> $TABLE"
|
||||||
if [ $DATABASES_TABLE_CHECK ]; then
|
bzip2 $DIR/$TABLE.txt
|
||||||
# Check INNODB table
|
fi
|
||||||
if [ -f "$PATH/$TABLE.ibd" ]; then
|
fi
|
||||||
if [ ! $(innochecksum $PATH/$TABLE.ibd) ]; then
|
|
||||||
f_log "$TABLE [OK]"
|
if [ $DATABASES_TABLE_CHECK ]; then
|
||||||
else
|
if [ -f "$DIR/$BDD/$TABLE.ibd" ]; then
|
||||||
f_log "$TABLE [ERR]"
|
if [ ! $(innochecksum $DIR/$BDD/$TABLE.ibd) ]; then
|
||||||
fi
|
f_log "$TABLE [OK]"
|
||||||
fi
|
else
|
||||||
fi
|
f_log "$TABLE [ERR]"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ -f "$PATH/__routines.sql" ]; then
|
if [ -f "$DIR/__routines.sql" ]; then
|
||||||
f_log "Import routines into $BDD"
|
f_log "Import routines into $BDD"
|
||||||
/usr/bin/mysql --defaults-extra-file=$MYCNF $BDD < $PATH/__routines.sql 2>/dev/null
|
time mysql --defaults-extra-file=$CONFIG_FILE $BDD < $DIR/__routines.sql 2>/dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -f "$PATH/__views.sql" ]; then
|
if [ -f "$DIR/__views.sql" ]; then
|
||||||
f_log "Import views into $BDD"
|
f_log "Import views into $BDD"
|
||||||
/usr/bin/mysql --defaults-extra-file=$MYCNF $BDD < $PATH/__views.sql 2>/dev/null
|
time mysql --defaults-extra-file=$CONFIG_FILE $BDD < $DIR/__views.sql 2>/dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
f_log "Flush privileges;"
|
f_log "Flush privileges;"
|
||||||
/usr/bin/mysql --defaults-extra-file=$MYCNF -e "flush privileges;"
|
time mysql --defaults-extra-file=$CONFIG_FILE -e "flush privileges;"
|
||||||
|
|
||||||
f_log "** END **"
|
f_log "** END **"
|
||||||
|
|
||||||
|
@ -108,4 +106,71 @@ restore()
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
restore $DIR_PWD
|
usage()
|
||||||
|
{
|
||||||
|
cat << EOF
|
||||||
|
usage: $0 options
|
||||||
|
|
||||||
|
This script restore databases.
|
||||||
|
|
||||||
|
OPTIONS:
|
||||||
|
-e Exclude databases
|
||||||
|
-s Selected databases
|
||||||
|
-c Check innochecksum of table after import
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
# === CHECKS ===
|
||||||
|
if [ $# = 0 ]; then
|
||||||
|
usage;
|
||||||
|
exit;
|
||||||
|
fi
|
||||||
|
|
||||||
|
BIN_DEPS="ls grep awk sort uniq bunzip2 bzip2 mysql"
|
||||||
|
|
||||||
|
if [ -f '/etc/debian_version' ]; then
|
||||||
|
CONFIG_FILE='/etc/mysql/debian.cnf'
|
||||||
|
else
|
||||||
|
CONFIG_FILE='~/mysql_utils/etc/mysql/debian.cnf'
|
||||||
|
fi
|
||||||
|
|
||||||
|
for BIN in $BIN_DEPS; do
|
||||||
|
which $BIN 1>/dev/null 2>&1
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "Error: Required command file not be found: $BIN"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
for i in "$@"
|
||||||
|
do
|
||||||
|
case $i in
|
||||||
|
--config=*)
|
||||||
|
CONFIG_FILE=( "${i#*=}" )
|
||||||
|
shift # past argument=value
|
||||||
|
;;
|
||||||
|
--chunk=*)
|
||||||
|
CONFIG_CHUNK=( "${i#*=}" )
|
||||||
|
shift # past argument=value
|
||||||
|
;;
|
||||||
|
-c)
|
||||||
|
DATABASES_TABLE_CHECK=1
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-v | --verbose)
|
||||||
|
VERBOSE=1
|
||||||
|
shift # past argument=value
|
||||||
|
;;
|
||||||
|
-h | --help)
|
||||||
|
usage
|
||||||
|
exit
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
# unknown option
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
# === AUTORUN ===
|
||||||
|
restore $(pwd)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue