=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
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
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
|
||||
|
||||
touch $DST/$BDD/error.log
|
||||
touch $DST/$BDD/error.log
|
||||
|
||||
mkdir -p $DST/$BDD 2>/dev/null 1>&2
|
||||
chown $USER:$GROUP $DST/$BDD
|
||||
|
@ -154,19 +122,19 @@ backup()
|
|||
|
||||
if [ $COMPRESS == 'bzip2' ]; then
|
||||
|
||||
if [ -f "$DST/$BDD/$TABLE.txt.bz2" ]; then
|
||||
rm $DST/$BDD/$TABLE.txt.bz2
|
||||
fi
|
||||
if [ -f "$DST/$BDD/$TABLE.txt.bz2" ]; then
|
||||
rm $DST/$BDD/$TABLE.txt.bz2
|
||||
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
|
||||
|
||||
if [ -f "$DST/$BDD/$TABLE.txt.gz" ]; then
|
||||
rm $DST/$BDD/$TABLE.txt.gz
|
||||
fi
|
||||
if [ -f "$DST/$BDD/$TABLE.txt.gz" ]; then
|
||||
rm $DST/$BDD/$TABLE.txt.gz
|
||||
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
|
||||
|
||||
|
@ -183,6 +151,38 @@ backup()
|
|||
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
|
||||
usage;
|
||||
exit;
|
||||
|
@ -194,12 +194,6 @@ EXCLUDE_DATA_TABLES=''
|
|||
BIN_DEPS="mysql mysqldump $COMPRESS"
|
||||
|
||||
# === 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
|
||||
which $BIN 1>/dev/null 2>&1
|
||||
if [ $? -ne 0 ]; then
|
||||
|
@ -208,6 +202,12 @@ for BIN in $BIN_DEPS; do
|
|||
fi
|
||||
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 "$@"
|
||||
do
|
||||
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
|
||||
|
||||
# === FUNCTIONS ===
|
||||
if [ -f '/etc/debian_version' ]; then
|
||||
CONFIG_FILE='/etc/mysql/debian.cnf'
|
||||
else
|
||||
CONFIG_FILE='~/mysql_utils/etc/mysql/debian.cnf'
|
||||
fi
|
||||
|
||||
# === FUNCTION ===
|
||||
f_log()
|
||||
{
|
||||
|
@ -24,37 +31,6 @@ f_log()
|
|||
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()
|
||||
{
|
||||
local array_skip=( "${@}" )
|
||||
|
@ -174,6 +150,37 @@ backup()
|
|||
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
|
||||
usage;
|
||||
exit;
|
||||
|
@ -184,12 +191,7 @@ EXCLUDE_TABLES=''
|
|||
EXCLUDE_DATA_TABLES=''
|
||||
BIN_DEPS="mysql mysqldump $COMPRESS"
|
||||
|
||||
# === CHECKS ===
|
||||
if [ "$#" -ne 1 ]; then
|
||||
echo "Illegal number of parameters"
|
||||
else
|
||||
DATABASE="$1"
|
||||
fi
|
||||
DATABASE="$1"
|
||||
|
||||
for BIN in $BIN_DEPS; do
|
||||
which $BIN 1>/dev/null 2>&1
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
# Automatically generated for Debian scripts. DO NOT TOUCH!
|
||||
[client]
|
||||
host = localhost
|
||||
user = debian-sys-maint
|
||||
password =
|
||||
socket = /var/run/mysqld/mysqld.sock
|
||||
user = root
|
||||
password = password
|
||||
socket = /usr/local/var/run/mysqld.sock
|
||||
[mysql_upgrade]
|
||||
host = localhost
|
||||
user = debian-sys-maint
|
||||
|
|
|
@ -1,22 +1,13 @@
|
|||
#!/bin/sh
|
||||
#!/bin/bash
|
||||
|
||||
# === CONFIG ===
|
||||
DIR_PWD=$(pwd)
|
||||
BIN_DEPS='bunzip2 bzip2 mysql'
|
||||
MYCNF='/etc/mysql/debian.cnf'
|
||||
MYDATA='/var/lib/mysql'
|
||||
CONFIG_CHUNK=1000000
|
||||
|
||||
# === DO NOT EDIT BELOW THIS LINE ===
|
||||
|
||||
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
|
||||
|
||||
database_exists()
|
||||
{
|
||||
RESULT=`mysqlshow --defaults-extra-file=$MYCNF $@| grep -v Wildcard | grep -o $@`
|
||||
|
@ -32,22 +23,12 @@ f_log()
|
|||
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()
|
||||
{
|
||||
DIR=$@
|
||||
|
||||
f_log "Check path $DIR"
|
||||
|
||||
f_log "** START **"
|
||||
|
||||
f_log "Check runtime"
|
||||
|
@ -60,12 +41,12 @@ restore()
|
|||
done
|
||||
|
||||
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"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
for i in $(ls -1 -d $DIR_PWD/*); do
|
||||
for i in $(ls -1 -d $DIR/*); do
|
||||
|
||||
BDD=$(basename $i)
|
||||
|
||||
|
@ -87,9 +68,9 @@ restore()
|
|||
|
||||
if [ $BDD ]; then
|
||||
|
||||
if [ -f $DIR_PWD/$BDD/__create.sql ]; then
|
||||
if [ -f $DIR/$BDD/__create.sql ]; then
|
||||
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
|
||||
|
||||
if [ $(database_exists $BDD) != "YES" ]; then
|
||||
|
@ -101,57 +82,60 @@ restore()
|
|||
f_log "Create tables in $BDD"
|
||||
for TABLE in $tables; do
|
||||
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;
|
||||
SOURCE $DIR_PWD/$BDD/$TABLE.sql;
|
||||
SOURCE $DIR/$BDD/$TABLE.sql;
|
||||
SET foreign_key_checks = 1;"
|
||||
done
|
||||
|
||||
f_log "Import data into $BDD"
|
||||
for TABLE in $tables; do
|
||||
f_log "Import data into $BDD/$TABLE"
|
||||
|
||||
if [ -f "$DIR_PWD/$BDD/$TABLE.txt.bz2" ]; then
|
||||
f_log "< $TABLE"
|
||||
if [ -f "$DIR_PWD/$BDD/$TABLE.txt" ]; then
|
||||
rm $DIR_PWD/$BDD/$TABLE.txt
|
||||
fi
|
||||
bunzip2 $DIR_PWD/$BDD/$TABLE.txt.bz2
|
||||
fi
|
||||
|
||||
if [ -f "$DIR_PWD/$BDD/$TABLE.txt" ]; then
|
||||
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;
|
||||
LOAD DATA LOCAL INFILE '$DIR_PWD/$BDD/$TABLE.txt'
|
||||
INTO TABLE $TABLE;
|
||||
SET foreign_key_checks = 1; SET unique_checks = 1; SET sql_log_bin = 1;"
|
||||
|
||||
if [ ! -f "$DIR_PWD/$BDD/$TABLE.txt.bz2" ]; then
|
||||
f_log "> $TABLE"
|
||||
bzip2 $DIR_PWD/$BDD/$TABLE.txt
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ $DATABASES_TABLE_CHECK ]; then
|
||||
# Check INNODB table
|
||||
if [ -f "$MYDATA/$BDD/$TABLE.ibd" ]; then
|
||||
if [ ! $(innochecksum $MYDATA/$BDD/$TABLE.ibd) ]; then
|
||||
f_log "$TABLE [OK]"
|
||||
else
|
||||
f_log "$TABLE [ERR]"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
f_log "Import data into $BDD/$TABLE"
|
||||
|
||||
if [ -f "$DIR/$BDD/$TABLE.txt.bz2" ]; then
|
||||
f_log "< $TABLE"
|
||||
if [ -f "$DIR/$BDD/$TABLE.txt" ]; then
|
||||
rm $DIR/$BDD/$TABLE.txt
|
||||
fi
|
||||
bunzip2 $DIR/$BDD/$TABLE.txt.bz2
|
||||
fi
|
||||
|
||||
if [ -f "$DIR/$BDD/$TABLE.txt" ]; then
|
||||
f_log "+ $TABLE"
|
||||
split -l $CONFIG_CHUNK "$DIR/$TABLE.txt" ${TABLE}_part_
|
||||
for segment in ${TABLE}_part_*; do
|
||||
time mysql --defaults-extra-file=$MYCNF $BDD --local-infile -e "SET foreign_key_checks = 0; SET unique_checks = 0; SET sql_log_bin = 0;
|
||||
LOAD DATA LOCAL INFILE '$DIR/$BDD/$TABLE.txt'
|
||||
INTO TABLE $TABLE;
|
||||
SET foreign_key_checks = 1; SET unique_checks = 1; SET sql_log_bin = 1;"
|
||||
rm $segment
|
||||
done
|
||||
if [ ! -f "$DIR/$BDD/$TABLE.txt.bz2" ]; then
|
||||
f_log "> $TABLE"
|
||||
bzip2 $DIR/$BDD/$TABLE.txt
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ $DATABASES_TABLE_CHECK ]; then
|
||||
if [ -f "$DIR/$BDD/$TABLE.ibd" ]; then
|
||||
if [ ! $(innochecksum $DIR/$BDD/$TABLE.ibd) ]; then
|
||||
f_log "$TABLE [OK]"
|
||||
else
|
||||
f_log "$TABLE [ERR]"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
done
|
||||
|
||||
if [ -f "$DIR_PWD/$BDD/__routines.sql" ]; then
|
||||
if [ -f "$DIR/$BDD/__routines.sql" ]; then
|
||||
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
|
||||
|
||||
if [ -f "$DIR_PWD/$BDD/__views.sql" ]; then
|
||||
if [ -f "$DIR/$BDD/__views.sql" ]; then
|
||||
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
|
||||
|
@ -159,33 +143,80 @@ restore()
|
|||
done
|
||||
|
||||
f_log "Flush privileges;"
|
||||
mysql --defaults-extra-file=$MYCNF -e "flush privileges;"
|
||||
time mysql --defaults-extra-file=$MYCNF -e "flush privileges;"
|
||||
|
||||
f_log "** END **"
|
||||
}
|
||||
|
||||
# === EXECUTE ===
|
||||
while getopts ":e:c:s:" opt;
|
||||
do
|
||||
case ${opt} in
|
||||
e)
|
||||
exclude=${OPTARG}
|
||||
IFS=, read -r -a DATABASES_SKIP <<< "$exclude"
|
||||
;;
|
||||
s)
|
||||
selected=${OPTARG}
|
||||
IFS=, read -r -a DATABASES_SELECTED <<< "$selected"
|
||||
;;
|
||||
c)
|
||||
DATABASES_TABLE_CHECK=1
|
||||
;;
|
||||
*)
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
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
|
||||
-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
|
||||
#shift "$((OPTIND - 1))"
|
||||
|
||||
# === AUTORUN ===
|
||||
restore
|
||||
restore $(pwd)
|
||||
|
||||
|
|
|
@ -1,22 +1,13 @@
|
|||
#!/bin/sh
|
||||
#!/bin/bash
|
||||
|
||||
# === CONFIG ===
|
||||
DIR_PWD=$(pwd)
|
||||
BIN_DEPS='bunzip2 bzip2 mysql'
|
||||
MYCNF='/etc/mysql/debian.cnf'
|
||||
DATABASES_TABLE_CHECK=1
|
||||
CONFIG_CHUNK=1000000
|
||||
|
||||
# === DO NOT EDIT BELOW THIS LINE ===
|
||||
|
||||
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
|
||||
|
||||
f_log()
|
||||
{
|
||||
echo "RESTORE: $@"
|
||||
|
@ -24,80 +15,87 @@ f_log()
|
|||
|
||||
restore()
|
||||
{
|
||||
PATH=$@
|
||||
DIR=$@
|
||||
|
||||
f_log "Check path $PATH"
|
||||
f_log "Check path $DIR"
|
||||
|
||||
f_log "** START **"
|
||||
|
||||
BDD=${PATH##*/}
|
||||
BDD=${DIR##*/}
|
||||
|
||||
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"
|
||||
/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
|
||||
|
||||
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"
|
||||
for TABLE in $tables; do
|
||||
f_log "Create table: $BDD/$TABLE"
|
||||
/usr/bin/mysql --defaults-extra-file=$MYCNF $BDD -e "SET foreign_key_checks = 0;
|
||||
DROP TABLE IF EXISTS $TABLE;
|
||||
SOURCE $PATH/$TABLE.sql;
|
||||
SET foreign_key_checks = 1;"
|
||||
f_log "Create table: $BDD/$TABLE"
|
||||
time mysql --defaults-extra-file=$CONFIG_FILE $BDD -e "SET foreign_key_checks = 0;
|
||||
DROP TABLE IF EXISTS $TABLE;
|
||||
SOURCE $DIR/$TABLE.sql;
|
||||
SET foreign_key_checks = 1;"
|
||||
done
|
||||
|
||||
f_log "Import data into $BDD"
|
||||
for TABLE in $tables; do
|
||||
f_log "Import data into $BDD/$TABLE"
|
||||
if [ -f "$PATH/$TABLE.txt.bz2" ]; then
|
||||
f_log "< $TABLE"
|
||||
if [ -f "$PATH/$TABLE.txt" ]; then
|
||||
f_log "rm $PATH/$TABLE.txt"
|
||||
/bin/rm $PATH/$TABLE.txt
|
||||
fi
|
||||
/bin/bunzip2 $PATH/$TABLE.txt.bz2
|
||||
fi
|
||||
|
||||
f_log "Import data into $BDD/$TABLE"
|
||||
if [ -f "$DIR/$TABLE.txt.bz2" ]; then
|
||||
f_log "< $TABLE"
|
||||
if [ -f "$DIR/$TABLE.txt" ]; then
|
||||
f_log "rm $DIR/$TABLE.txt"
|
||||
rm $DIR/$TABLE.txt
|
||||
fi
|
||||
bunzip2 $DIR/$TABLE.txt.bz2
|
||||
fi
|
||||
|
||||
if [ -f "$PATH/$TABLE.txt" ]; then
|
||||
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;
|
||||
LOAD DATA LOCAL INFILE '$PATH/$TABLE.txt'
|
||||
INTO TABLE $TABLE;
|
||||
SET foreign_key_checks = 1; SET unique_checks = 1; SET sql_log_bin = 1;"
|
||||
|
||||
if [ ! -f "$PATH/$TABLE.txt.bz2" ]; then
|
||||
f_log "> $TABLE"
|
||||
/bin/bzip2 $PATH/$TABLE.txt
|
||||
fi
|
||||
fi
|
||||
if [ $DATABASES_TABLE_CHECK ]; then
|
||||
# Check INNODB table
|
||||
if [ -f "$PATH/$TABLE.ibd" ]; then
|
||||
if [ ! $(innochecksum $PATH/$TABLE.ibd) ]; then
|
||||
f_log "$TABLE [OK]"
|
||||
else
|
||||
f_log "$TABLE [ERR]"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
if [ -f "$DIR/$TABLE.txt" ]; then
|
||||
f_log "+ $TABLE"
|
||||
split -l $CONFIG_CHUNK "$DIR/$TABLE.txt" ${TABLE}_part_
|
||||
for segment in ${TABLE}_part_*; do
|
||||
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;
|
||||
LOAD DATA LOCAL INFILE '$segment'
|
||||
INTO TABLE $TABLE;
|
||||
SET foreign_key_checks = 1; SET unique_checks = 1; SET sql_log_bin = 1;"
|
||||
rm $segment
|
||||
done
|
||||
if [ ! -f "$DIR/$TABLE.txt.bz2" ]; then
|
||||
f_log "> $TABLE"
|
||||
bzip2 $DIR/$TABLE.txt
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ $DATABASES_TABLE_CHECK ]; then
|
||||
if [ -f "$DIR/$BDD/$TABLE.ibd" ]; then
|
||||
if [ ! $(innochecksum $DIR/$BDD/$TABLE.ibd) ]; then
|
||||
f_log "$TABLE [OK]"
|
||||
else
|
||||
f_log "$TABLE [ERR]"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
done
|
||||
|
||||
if [ -f "$PATH/__routines.sql" ]; then
|
||||
f_log "Import routines into $BDD"
|
||||
/usr/bin/mysql --defaults-extra-file=$MYCNF $BDD < $PATH/__routines.sql 2>/dev/null
|
||||
if [ -f "$DIR/__routines.sql" ]; then
|
||||
f_log "Import routines into $BDD"
|
||||
time mysql --defaults-extra-file=$CONFIG_FILE $BDD < $DIR/__routines.sql 2>/dev/null
|
||||
fi
|
||||
|
||||
if [ -f "$PATH/__views.sql" ]; then
|
||||
f_log "Import views into $BDD"
|
||||
/usr/bin/mysql --defaults-extra-file=$MYCNF $BDD < $PATH/__views.sql 2>/dev/null
|
||||
if [ -f "$DIR/__views.sql" ]; then
|
||||
f_log "Import views into $BDD"
|
||||
time mysql --defaults-extra-file=$CONFIG_FILE $BDD < $DIR/__views.sql 2>/dev/null
|
||||
fi
|
||||
|
||||
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 **"
|
||||
|
||||
|
@ -108,4 +106,71 @@ restore()
|
|||
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