Compare commits
62 Commits
Author | SHA1 | Date |
---|---|---|
mirocow | 31628857bf | |
mirocow | 1d4dbd9889 | |
mirocow | ba4cee5adb | |
mirocow | a33059f6a8 | |
mirocow | 46405d5594 | |
mirocow | 8c5a46063d | |
mirocow | ac136915d5 | |
mirocow | 598e20a86e | |
mirocow | e06706685a | |
mirocow | ced1fbb02f | |
mirocow | 7ea3f69bfc | |
mirocow | 2b9ef28316 | |
mirocow | f6ab63abce | |
mirocow | 501b3a5e36 | |
mirocow | 40266a4d46 | |
mirocow | 6cd7eb5427 | |
mirocow | 0586125ab7 | |
mirocow | fd432fb80a | |
mirocow | 33c5791782 | |
mirocow | 438c05c1a6 | |
mirocow | 6ecc220823 | |
Mirocow | c635e3bc29 | |
mirocow | e2efabe8e4 | |
mirocow | 8f54453049 | |
mirocow | 719abd0f15 | |
mirocow | 024da4cc60 | |
mirocow | d42db146f8 | |
mirocow | 14041fb59c | |
mirocow | dbb56b45b5 | |
mirocow | 73fcf08477 | |
mirocow | 60303b706f | |
mirocow | cf43e7931d | |
mirocow | 3dc75d5713 | |
mirocow | 194b577717 | |
mirocow | c308b1b808 | |
mirocow | 971c2d0b1d | |
mirocow | 9b0bdb0c35 | |
mirocow | 3cf1be72da | |
mirocow | be23cdb838 | |
mirocow | f6678aa588 | |
mirocow | 1d573a6d47 | |
mirocow | afd2c0613d | |
mirocow | 116511a0a3 | |
mirocow | 5b668dce39 | |
mirocow | 1147cce066 | |
mirocow | 6b9f6fafd7 | |
mirocow | 2b09c15dab | |
mirocow | db9e0568da | |
mirocow | faee180325 | |
mirocow | fe692084e8 | |
mirocow | e331888171 | |
mirocow | ea01afbb3d | |
mirocow | 32d15cc0be | |
mirocow | a2215462bc | |
mirocow | 9c509c674c | |
mirocow | e85bdf8668 | |
mirocow | 15d4283418 | |
mirocow | d23f850dc8 | |
mirocow | 082ebca7fb | |
mirocow | c6431c0752 | |
mirocow | 488c209924 | |
mirocow | 81fc75d36f |
|
@ -101,7 +101,7 @@ Options:
|
|||
|
||||
Examples:
|
||||
backup.sh --verbose --compress=
|
||||
backup.sh --verbose --compress=zgip
|
||||
backup.sh --verbose --compress=gzip
|
||||
backup.sh --verbose --compress=bzip2
|
||||
backup.sh --verbose --compress= --exclude="mysql"
|
||||
backup.sh --verbose --compress= --exclude="mysql" --lifetime="3 day ago"
|
||||
|
|
48
backup.sh
48
backup.sh
|
@ -1,5 +1,6 @@
|
|||
#!/bin/bash
|
||||
|
||||
# === CONFIG ===
|
||||
VERBOSE=0
|
||||
COMPRESS='bzip2'
|
||||
USER='mysql'
|
||||
|
@ -17,6 +18,13 @@ if [ ! -n "$BASH" ] ;then echo Please run this script $0 with bash; exit 1; fi
|
|||
# === FUNCTION ===
|
||||
f_log()
|
||||
{
|
||||
local bold=$(tput bold)
|
||||
local yellow=$(tput setf 6)
|
||||
local red=$(tput setf 4)
|
||||
local green=$(tput setf 2)
|
||||
local reset=$(tput sgr0)
|
||||
local toend=$(tput hpa $(tput cols))$(tput cub 6)
|
||||
|
||||
logger "BACKUP: $@"
|
||||
|
||||
if [ $VERBOSE -eq 1 ]; then
|
||||
|
@ -54,22 +62,21 @@ backup()
|
|||
database_exclude_expression=`prepaire_skip_expression "${database_exclude[@]}"`
|
||||
f_log "Exclude databases: $database_exclude_expression"
|
||||
|
||||
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
|
||||
for BDD in $(mysql --defaults-file=$CONFIG_FILE --skip-column-names -B -e "$query" | egrep -v "$database_exclude_expression"); do
|
||||
|
||||
mkdir -p $DST/$BDD 2>/dev/null 1>&2
|
||||
chown $USER:$GROUP $DST/$BDD
|
||||
chmod $DIRECTORYATTRIBUTES $DST/$BDD
|
||||
touch $DST/$BDD/error.log
|
||||
|
||||
query="SHOW CREATE DATABASE \`$BDD\`;"
|
||||
mysql --defaults-extra-file=$CONFIG_FILE --skip-column-names -B -e "$query" | awk -F"\t" '{ print $2 }' > $DST/$BDD/__create.sql
|
||||
mysql --defaults-file=$CONFIG_FILE --skip-column-names -B -e "$query" | awk -F"\t" '{ print $2 }' > $DST/$BDD/__create.sql
|
||||
if [ -f $DST/$BDD/__create.sql ]; then
|
||||
f_log " > Export create"
|
||||
fi
|
||||
|
||||
query="SHOW FULL TABLES WHERE Table_type = 'VIEW';"
|
||||
for viewName in $(mysql --defaults-extra-file=$CONFIG_FILE $BDD -N -e "$query" | sed 's/|//' | awk '{print $1}'); do
|
||||
for viewName in $(mysql --defaults-file=$CONFIG_FILE $BDD -N -e "$query" | sed 's/|//' | awk '{print $1}'); do
|
||||
mysqldump --defaults-file=$CONFIG_FILE $BDD $viewName >> $DST/$BDD/__views.sql 2>> $DST/$BDD/error.log
|
||||
array_views+=($viewName)
|
||||
done
|
||||
|
@ -96,15 +103,22 @@ backup()
|
|||
f_log "Exclude data tables: $data_tables_exclude_expression"
|
||||
|
||||
query="SHOW TABLES;"
|
||||
for TABLE in $(mysql --defaults-extra-file=$CONFIG_FILE --skip-column-names -B $BDD -e "$query" | egrep -v "$tables_exclude_expression"); do
|
||||
for TABLE in $(mysql --defaults-file=$CONFIG_FILE --skip-column-names -B $BDD -e "$query" | egrep -v "$tables_exclude_expression"); do
|
||||
f_log " ** Dump $BDD.$TABLE"
|
||||
|
||||
if [ $(echo $data_tables_exclude_expression| grep $TABLE) ]; then
|
||||
f_log "Exclude data from table $TABLE"
|
||||
mysqldump --defaults-file=$CONFIG_FILE --no-data --add-drop-table --tab=$DST/$BDD/ $BDD $TABLE 2>> $DST/$BDD/error.log
|
||||
else
|
||||
mysqldump --defaults-file=$CONFIG_FILE --add-drop-table --quick --tab=$DST/$BDD/ $BDD $TABLE 2>> $DST/$BDD/error.log
|
||||
fi
|
||||
# If fields has geospatial types
|
||||
checkGeo="mysql --defaults-file=$CONFIG_FILE -B $BDD -e \"SHOW COLUMNS FROM $TABLE WHERE Type IN ('point', 'polygon', 'geometry', 'linestring')\""
|
||||
hasGeo=$(eval $checkGeo)
|
||||
if [ ! -z "$hasGeo" ]; then
|
||||
mysqldump --defaults-file=$CONFIG_FILE --flush-logs --default-character-set=utf8 --add-drop-table --quick --result-file=$DST/$BDD/$TABLE.sql $BDD $TABLE 2>> $DST/$BDD/error.log
|
||||
else
|
||||
mysqldump --defaults-file=$CONFIG_FILE --flush-logs --default-character-set=utf8 --add-drop-table --quick --tab=$DST/$BDD/ $BDD $TABLE 2>> $DST/$BDD/error.log
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -f "$DST/$BDD/$TABLE.sql" ]; then
|
||||
chmod $FILEATTRIBUTES $DST/$BDD/$TABLE.sql
|
||||
|
@ -122,17 +136,17 @@ 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 && 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 && chown $USER:$GROUP $DST/$BDD/$TABLE.txt.gz && chmod $FILEATTRIBUTES $DST/$BDD/$TABLE.txt.gz) &
|
||||
|
||||
|
@ -172,7 +186,7 @@ Options:
|
|||
|
||||
Examples:
|
||||
backup.sh --verbose --compress=
|
||||
backup.sh --verbose --compress=zgip
|
||||
backup.sh --verbose --compress=gzip
|
||||
backup.sh --verbose --compress=bzip2
|
||||
backup.sh --verbose --compress= --exclude="mysql"
|
||||
backup.sh --verbose --compress= --exclude="mysql" --lifetime="3 day ago"
|
||||
|
@ -180,6 +194,8 @@ Examples:
|
|||
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
|
||||
}
|
||||
|
||||
|
@ -270,7 +286,7 @@ fi
|
|||
|
||||
# === SETTINGS ===
|
||||
f_log "============================================"
|
||||
f_log "Dump into: $BACKUP_DIR"
|
||||
f_log "Dump into: $DST"
|
||||
f_log "Config file: $CONFIG_FILE"
|
||||
f_log "Verbose: $VERBOSE"
|
||||
f_log "Compress: $COMPRESS"
|
||||
|
|
65
backup_db.sh
65
backup_db.sh
|
@ -24,6 +24,13 @@ fi
|
|||
# === FUNCTION ===
|
||||
f_log()
|
||||
{
|
||||
local bold=$(tput bold)
|
||||
local yellow=$(tput setf 6)
|
||||
local red=$(tput setf 4)
|
||||
local green=$(tput setf 2)
|
||||
local reset=$(tput sgr0)
|
||||
local toend=$(tput hpa $(tput cols))$(tput cub 6)
|
||||
|
||||
logger "BACKUP: $@"
|
||||
|
||||
if [ $VERBOSE -eq 1 ]; then
|
||||
|
@ -55,22 +62,21 @@ backup()
|
|||
'performance_schema'
|
||||
)
|
||||
|
||||
local array_views=()
|
||||
|
||||
touch $DST/$DATABASE/error.log
|
||||
local array_views=()
|
||||
|
||||
mkdir -p $DST/$DATABASE 2>/dev/null 1>&2
|
||||
chown $USER:$GROUP $DST/$DATABASE
|
||||
chmod $DIRECTORYATTRIBUTES $DST/$DATABASE
|
||||
touch $DST/$DATABASE/error.log
|
||||
|
||||
query="SHOW CREATE DATABASE \`$DATABASE\`;"
|
||||
mysql --defaults-extra-file=$CONFIG_FILE --skip-column-names -B -e "$query" | awk -F"\t" '{ print $2 }' > $DST/$DATABASE/__create.sql
|
||||
mysql --defaults-file=$CONFIG_FILE --skip-column-names -B -e "$query" | awk -F"\t" '{ print $2 }' > $DST/$DATABASE/__create.sql
|
||||
if [ -f $DST/$DATABASE/__create.sql ]; then
|
||||
f_log " > Export create"
|
||||
fi
|
||||
|
||||
query="SHOW FULL TABLES WHERE Table_type = 'VIEW';"
|
||||
for viewName in $(mysql --defaults-extra-file=$CONFIG_FILE $DATABASE -N -e "$query" | sed 's/|//' | awk '{print $1}'); do
|
||||
for viewName in $(mysql --defaults-file=$CONFIG_FILE $DATABASE -N -e "$query" | sed 's/|//' | awk '{print $1}'); do
|
||||
mysqldump --defaults-file=$CONFIG_FILE $DATABASE $viewName >> $DST/$DATABASE/__views.sql 2>> $DST/$DATABASE/error.log
|
||||
array_views+=($viewName)
|
||||
done
|
||||
|
@ -89,22 +95,46 @@ backup()
|
|||
)
|
||||
|
||||
tables_exclude=( ${default_tables_exclude[@]} ${array_views[@]} ${EXCLUDE_TABLES[@]} )
|
||||
tables_exclude_expression=`prepaire_skip_expression "${tables_exclude[@]}"`
|
||||
tables_exclude_expression=$(prepaire_skip_expression "${tables_exclude[@]}")
|
||||
f_log "Exclude tables: $tables_exclude_expression"
|
||||
|
||||
data_tables_exclude=( ${EXCLUDE_DATA_TABLES[@]} )
|
||||
data_tables_exclude_expression=`prepaire_skip_expression "${data_tables_exclude[@]}"`
|
||||
data_tables_exclude_expression=$(prepaire_skip_expression "${data_tables_exclude[@]}")
|
||||
f_log "Exclude data tables: $data_tables_exclude_expression"
|
||||
|
||||
tables=( ${TABLES[@]} )
|
||||
tables_expression=$(prepaire_skip_expression "${tables[@]}")
|
||||
f_log "Only tables: $tables_expression"
|
||||
|
||||
query="SHOW TABLES;"
|
||||
for TABLE in $(mysql --defaults-extra-file=$CONFIG_FILE --skip-column-names -B $DATABASE -e "$query" | egrep -v "$tables_exclude_expression"); do
|
||||
f_log " ** Dump $DATABASE.$TABLE"
|
||||
command="mysql --defaults-file=$CONFIG_FILE --skip-column-names -B $DATABASE -e \"$query\""
|
||||
|
||||
if [ $tables_exclude_expression ]; then
|
||||
command=" $command | egrep -v \"$tables_exclude_expression\""
|
||||
fi
|
||||
|
||||
if [ $tables_expression ]; then
|
||||
command=" $command | egrep \"$tables_expression\""
|
||||
fi
|
||||
|
||||
f_log "Command: $command"
|
||||
|
||||
for TABLE in $(eval $command); do
|
||||
|
||||
f_log " ** Dump $DATABASE.$TABLE"
|
||||
|
||||
if [ $(echo $data_tables_exclude_expression| grep $TABLE) ]; then
|
||||
f_log "Exclude data from table $TABLE"
|
||||
mysqldump --defaults-file=$CONFIG_FILE --no-data --add-drop-table --tab=$DST/$DATABASE/ $DATABASE $TABLE 2>> $DST/$DATABASE/error.log
|
||||
mysqldump --defaults-file=$CONFIG_FILE --no-data --add-drop-table --tab=$DST/$DATABASE/ $DATABASE $TABLE 2>> $DST/$DATABASE/error.log
|
||||
else
|
||||
mysqldump --defaults-file=$CONFIG_FILE --add-drop-table --quick --tab=$DST/$DATABASE/ $DATABASE $TABLE 2>> $DST/$DATABASE/error.log
|
||||
# If fields has geospatial types
|
||||
checkGeo="mysql --defaults-file=$CONFIG_FILE -B $DATABASE -e \"SHOW COLUMNS FROM $TABLE WHERE Type IN ('point', 'polygon', 'geometry', 'linestring')\""
|
||||
hasGeo=$(eval $checkGeo)
|
||||
if [ ! -z "$hasGeo" ]; then
|
||||
mysqldump --defaults-file=$CONFIG_FILE --flush-logs --default-character-set=utf8 --add-drop-table --quick --result-file=$DST/$DATABASE/$TABLE.sql $DATABASE $TABLE 2>> $DST/$DATABASE/error.log
|
||||
else
|
||||
mysqldump --defaults-file=$CONFIG_FILE --flush-logs --default-character-set=utf8 --add-drop-table --quick --tab=$DST/$DATABASE/ $DATABASE $TABLE 2>> $DST/$DATABASE/error.log
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -f "$DST/$DATABASE/$TABLE.sql" ]; then
|
||||
|
@ -159,6 +189,7 @@ usage()
|
|||
Usage: $0 <[database-name]> <[options]> or bash $0 <[database-name]> <[options]>
|
||||
|
||||
Options:
|
||||
--tables= Dump only such tables
|
||||
--exclude-tables= Exclude tables
|
||||
--exclude-data-tables= Exclude data tables
|
||||
-c= | --compress= Compress with gzip or bzip2
|
||||
|
@ -170,7 +201,7 @@ Options:
|
|||
|
||||
Examples:
|
||||
backup.sh --verbose --compress=
|
||||
backup.sh --verbose --compress=zgip
|
||||
backup.sh --verbose --compress=gzip
|
||||
backup.sh --verbose --compress=bzip2
|
||||
backup.sh --verbose --compress=
|
||||
backup.sh --verbose --compress= --lifetime="3 day ago"
|
||||
|
@ -178,6 +209,8 @@ Examples:
|
|||
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"
|
||||
backup.sh --verbose --dir="/home/backups/mysql" --tables="tbl_template tbl_template1 tbl_template2"
|
||||
|
||||
EOF
|
||||
}
|
||||
|
||||
|
@ -187,6 +220,7 @@ if [ $# = 0 ]; then
|
|||
fi
|
||||
|
||||
DATABASE=''
|
||||
TABLES=''
|
||||
EXCLUDE_TABLES=''
|
||||
EXCLUDE_DATA_TABLES=''
|
||||
BIN_DEPS="mysql mysqldump $COMPRESS"
|
||||
|
@ -204,6 +238,10 @@ done
|
|||
for i in "$@"
|
||||
do
|
||||
case $i in
|
||||
-t=* | --tables=*)
|
||||
TABLES=( "${i#*=}" )
|
||||
shift # past argument=value
|
||||
;;
|
||||
--exclude-tables=*)
|
||||
EXCLUDE_TABLES=( "${i#*=}" )
|
||||
shift # past argument=value
|
||||
|
@ -259,11 +297,12 @@ fi
|
|||
|
||||
# === SETTINGS ===
|
||||
f_log "============================================"
|
||||
f_log "Dump into: $BACKUP_DIR"
|
||||
f_log "Dump into: $DST"
|
||||
f_log "Config file: $CONFIG_FILE"
|
||||
f_log "Verbose: $VERBOSE"
|
||||
f_log "Compress: $COMPRESS"
|
||||
f_log "Database: $DATABASE"
|
||||
f_log "Tables: $TABLES"
|
||||
f_log "Exclude tables: $EXCLUDE_TABLES"
|
||||
f_log "Life time: $TIME_REMOVED_DUMP_FILES"
|
||||
f_log "============================================"
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
# === CONFIG ===
|
||||
CONFIG_CHUNK=1000000
|
||||
VERBOSE=0
|
||||
|
||||
# === DO NOT EDIT BELOW THIS LINE ===
|
||||
|
||||
|
@ -10,7 +11,7 @@ if [ ! -n "$BASH" ] ;then echo Please run this script $0 with bash; exit 1; fi
|
|||
# === FUNCTIONS ===
|
||||
database_exists()
|
||||
{
|
||||
RESULT=`mysqlshow --defaults-extra-file=$MYCNF $@| grep -v Wildcard | grep -o $@`
|
||||
RESULT=`mysqlshow --defaults-file=$CONFIG_FILE $@| grep -v Wildcard | grep -o $@`
|
||||
if [ "$RESULT" == "$@" ]; then
|
||||
echo YES
|
||||
else
|
||||
|
@ -20,7 +21,18 @@ database_exists()
|
|||
|
||||
f_log()
|
||||
{
|
||||
logger "RESTORE: $@"
|
||||
local bold=$(tput bold)
|
||||
local yellow=$(tput setf 6)
|
||||
local red=$(tput setf 4)
|
||||
local green=$(tput setf 2)
|
||||
local reset=$(tput sgr0)
|
||||
local toend=$(tput hpa $(tput cols))$(tput cub 6)
|
||||
|
||||
logger "RESTORE: $@"
|
||||
|
||||
if [ $VERBOSE -eq 1 ]; then
|
||||
echo "RESTORE: $@"
|
||||
fi
|
||||
}
|
||||
|
||||
restore()
|
||||
|
@ -47,103 +59,113 @@ restore()
|
|||
fi
|
||||
|
||||
for i in $(ls -1 -d $DIR/*); do
|
||||
|
||||
BDD=$(basename $i)
|
||||
|
||||
for skip in "${DATABASES_SKIP[@]}"; do
|
||||
if [ $BDD = $skip ]; then
|
||||
f_log "Skip database $BDD"
|
||||
unset BDD
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
for select in "${DATABASES_SELECTED[@]}"; do
|
||||
if [ $BDD != $select ]; then
|
||||
f_log "Skip database $BDD"
|
||||
unset BDD
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
BDD=$(basename $i)
|
||||
|
||||
for skip in "${DATABASES_SKIP[@]}"; do
|
||||
if [ $BDD = $skip ]; then
|
||||
f_log "Skip database $BDD"
|
||||
unset BDD
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
for select in "${DATABASES_SELECTED[@]}"; do
|
||||
if [ $BDD != $select ]; then
|
||||
f_log "Skip database $BDD"
|
||||
unset BDD
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [ $BDD ]; then
|
||||
|
||||
if [ -f $DIR/$BDD/__create.sql ]; then
|
||||
f_log "Create database $BDD"
|
||||
mysql --defaults-file=$CONFIG_FILE < $DIR/$BDD/__create.sql 2>/dev/null
|
||||
fi
|
||||
|
||||
if [ $(database_exists $BDD) != "YES" ]; then
|
||||
f_log "Error: Database $BDD dose not exists";
|
||||
else
|
||||
|
||||
tables=$(ls -1 $DIR/$BDD | grep -v __ | grep .sql | awk -F. '{print $1}' | sort | uniq)
|
||||
|
||||
f_log "Create tables in $BDD"
|
||||
for TABLE in $tables; do
|
||||
f_log "Create table: $BDD/$TABLE"
|
||||
mysql --defaults-file=$CONFIG_FILE $BDD -e "SET foreign_key_checks = 0;
|
||||
DROP TABLE IF EXISTS $TABLE;
|
||||
SOURCE $DIR/$BDD/$TABLE.sql;
|
||||
SET foreign_key_checks = 1;"
|
||||
done
|
||||
|
||||
if [ $BDD ]; then
|
||||
f_log "Import data into $BDD"
|
||||
for TABLE in $tables; do
|
||||
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 -k $DIR/$BDD/$TABLE.txt.bz2
|
||||
fi
|
||||
|
||||
if [ -f $DIR/$BDD/__create.sql ]; then
|
||||
f_log "Create database $BDD"
|
||||
time mysql --defaults-extra-file=$MYCNF < $DIR/$BDD/__create.sql 2>/dev/null
|
||||
fi
|
||||
if [ -s "$DIR/$BDD/$TABLE.txt" ]; then
|
||||
|
||||
f_log "+ $TABLE"
|
||||
|
||||
if [ $(database_exists $BDD) != "YES" ]; then
|
||||
f_log "Error: Database $BDD dose not exists";
|
||||
else
|
||||
|
||||
tables=$(ls -1 $i | 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"
|
||||
time mysql --defaults-extra-file=$MYCNF $BDD -e "SET foreign_key_checks = 0;
|
||||
DROP TABLE IF EXISTS $TABLE;
|
||||
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/$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
|
||||
split -l $CONFIG_CHUNK "$DIR/$BDD/$TABLE.txt" "$DIR/$BDD/${TABLE}_part_"
|
||||
for segment in "$DIR/$BDD/${TABLE}"_part_*; do
|
||||
f_log "Restore from $segment"
|
||||
mysql --defaults-file=$CONFIG_FILE $BDD --local-infile -e "SET foreign_key_checks = 0; SET unique_checks = 0; SET sql_log_bin = 0;
|
||||
SET character_set_database=utf8;
|
||||
LOAD DATA LOCAL INFILE '$segment'
|
||||
INTO TABLE $TABLE;
|
||||
SET foreign_key_checks = 1; SET unique_checks = 1; SET sql_log_bin = 1;"
|
||||
|
||||
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/$BDD/__routines.sql" ]; then
|
||||
f_log "Import routines into $BDD"
|
||||
time mysql --defaults-extra-file=$MYCNF $BDD < $DIR/$BDD/__routines.sql 2>/dev/null
|
||||
if [ -f "$segment" ]; then
|
||||
f_log "Delete segment $segment"
|
||||
rm "$segment"
|
||||
fi
|
||||
done
|
||||
|
||||
fi
|
||||
|
||||
if [ -f "$DIR/$BDD/$TABLE.txt" ]; then
|
||||
f_log "Delete source: $TABLE.txt"
|
||||
rm $DIR/$BDD/$TABLE.txt
|
||||
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
|
||||
|
||||
if [ -f "$DIR/$BDD/__views.sql" ]; then
|
||||
f_log "Import views into $BDD"
|
||||
time mysql --defaults-extra-file=$MYCNF $BDD < $DIR/$BDD/__views.sql 2>/dev/null
|
||||
fi
|
||||
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
done
|
||||
|
||||
if [ -f "$DIR/$BDD/__routines.sql" ]; then
|
||||
f_log "Import routines into $BDD"
|
||||
mysql --defaults-file=$CONFIG_FILE $BDD < $DIR/$BDD/__routines.sql 2>/dev/null
|
||||
fi
|
||||
|
||||
if [ -f "$DIR/$BDD/__views.sql" ]; then
|
||||
f_log "Import views into $BDD"
|
||||
mysql --defaults-file=$CONFIG_FILE $BDD < $DIR/$BDD/__views.sql 2>/dev/null
|
||||
fi
|
||||
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
f_log "Flush privileges;"
|
||||
time mysql --defaults-extra-file=$MYCNF -e "flush privileges;"
|
||||
mysql --defaults-file=$CONFIG_FILE -e "flush privileges;"
|
||||
|
||||
f_log "** END **"
|
||||
}
|
||||
|
@ -157,16 +179,16 @@ This script restore databases.
|
|||
|
||||
OPTIONS:
|
||||
-e Exclude databases
|
||||
-s Selected databases
|
||||
-c Check innochecksum of table after import
|
||||
-s Selected databases
|
||||
-c Check innochecksum of table after import
|
||||
|
||||
|
||||
EOF
|
||||
}
|
||||
|
||||
# === CHECKS ===
|
||||
if [ $# = 0 ]; then
|
||||
usage;
|
||||
exit;
|
||||
fi
|
||||
|
||||
BACKUP_DIR=$(pwd)
|
||||
|
||||
BIN_DEPS="ls grep awk sort uniq bunzip2 bzip2 mysql"
|
||||
|
||||
|
@ -187,22 +209,26 @@ done
|
|||
for i in "$@"
|
||||
do
|
||||
case $i in
|
||||
-e)
|
||||
DATABASES_SKIP=( "${i#*=}" )
|
||||
shift
|
||||
;;
|
||||
-s)
|
||||
DATABASES_SELECTED=( "${i#*=}" )
|
||||
shift
|
||||
;;
|
||||
-c)
|
||||
DATABASES_TABLE_CHECK=1
|
||||
shift
|
||||
;;
|
||||
-e)
|
||||
DATABASES_SKIP=( "${i#*=}" )
|
||||
shift
|
||||
;;
|
||||
-s)
|
||||
DATABASES_SELECTED=( "${i#*=}" )
|
||||
shift
|
||||
;;
|
||||
-c)
|
||||
DATABASES_TABLE_CHECK=1
|
||||
shift
|
||||
;;
|
||||
--config=*)
|
||||
CONFIG_FILE=( "${i#*=}" )
|
||||
shift # past argument=value
|
||||
;;
|
||||
--chunk=*)
|
||||
CONFIG_CHUNK=( "${i#*=}" )
|
||||
shift # past argument=value
|
||||
;;
|
||||
-v | --verbose)
|
||||
VERBOSE=1
|
||||
shift # past argument=value
|
||||
|
@ -217,6 +243,14 @@ do
|
|||
esac
|
||||
done
|
||||
|
||||
# === SETTINGS ===
|
||||
f_log "============================================"
|
||||
f_log "Restore from: $BACKUP_DIR"
|
||||
f_log "Config file: $CONFIG_FILE"
|
||||
f_log "Verbose: $VERBOSE"
|
||||
f_log "Selected databases: $DATABASES_SELECTED"
|
||||
f_log "============================================"
|
||||
f_log ""
|
||||
|
||||
# === AUTORUN ===
|
||||
restore $(pwd)
|
||||
|
||||
|
|
|
@ -2,15 +2,27 @@
|
|||
|
||||
# === CONFIG ===
|
||||
CONFIG_CHUNK=1000000
|
||||
VERBOSE=0
|
||||
|
||||
# === DO NOT EDIT BELOW THIS LINE ===
|
||||
|
||||
if [ ! -n "$BASH" ] ;then echo Please run this script $0 with bash; exit 1; fi
|
||||
|
||||
# === FUNCTIONS ===
|
||||
f_log()
|
||||
f_log()
|
||||
{
|
||||
echo "RESTORE: $@"
|
||||
local bold=$(tput bold)
|
||||
local yellow=$(tput setf 6)
|
||||
local red=$(tput setf 4)
|
||||
local green=$(tput setf 2)
|
||||
local reset=$(tput sgr0)
|
||||
local toend=$(tput hpa $(tput cols))$(tput cub 6)
|
||||
|
||||
logger "RESTORE: $@"
|
||||
|
||||
if [ $VERBOSE -eq 1 ]; then
|
||||
echo "RESTORE: $@"
|
||||
fi
|
||||
}
|
||||
|
||||
restore()
|
||||
|
@ -29,15 +41,15 @@ restore()
|
|||
|
||||
if [ -f $DIR/__create.sql ]; then
|
||||
f_log "Create database $BDD"
|
||||
time mysql --defaults-extra-file=$CONFIG_FILE < $DIR/__create.sql 2>/dev/null
|
||||
mysql --defaults-file=$CONFIG_FILE < $DIR/__create.sql 2>/dev/null
|
||||
fi
|
||||
|
||||
tables=$(ls -1 $DIR | grep -v __ | awk -F. '{print $1}' | sort | uniq)
|
||||
tables=$(ls -1 $DIR | grep -v __ | grep .sql | awk -F. '{print $1}' | sort | uniq)
|
||||
|
||||
f_log "Create tables in $BDD"
|
||||
for TABLE in $tables; do
|
||||
f_log "Create table: $BDD/$TABLE"
|
||||
time mysql --defaults-extra-file=$CONFIG_FILE $BDD -e "SET foreign_key_checks = 0;
|
||||
mysql --defaults-file=$CONFIG_FILE $BDD -e "SET foreign_key_checks = 0;
|
||||
DROP TABLE IF EXISTS $TABLE;
|
||||
SOURCE $DIR/$TABLE.sql;
|
||||
SET foreign_key_checks = 1;"
|
||||
|
@ -53,49 +65,59 @@ restore()
|
|||
f_log "rm $DIR/$TABLE.txt"
|
||||
rm $DIR/$TABLE.txt
|
||||
fi
|
||||
bunzip2 $DIR/$TABLE.txt.bz2
|
||||
bunzip2 -k $DIR/$TABLE.txt.bz2
|
||||
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]"
|
||||
if [ -s "$DIR/$TABLE.txt" ]; then
|
||||
|
||||
f_log "+ $TABLE"
|
||||
|
||||
split -l $CONFIG_CHUNK "$DIR/$TABLE.txt" "$DIR/${TABLE}_part_"
|
||||
for segment in "$DIR/${TABLE}"_part_*; do
|
||||
f_log "Restore from $segment"
|
||||
mysql --defaults-file=$CONFIG_FILE $BDD --local-infile -e "SET foreign_key_checks = 0; SET unique_checks = 0; SET sql_log_bin = 0;
|
||||
SET character_set_database=utf8;
|
||||
LOAD DATA LOCAL INFILE '$segment'
|
||||
INTO TABLE $TABLE;
|
||||
SET foreign_key_checks = 1; SET unique_checks = 1; SET sql_log_bin = 1;"
|
||||
|
||||
if [ -f "$segment" ]; then
|
||||
f_log "Delete segment $segment"
|
||||
rm "$segment"
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -f "$DIR/$TABLE.txt" ]; then
|
||||
f_log "Delete source file: $TABLE.txt"
|
||||
rm $DIR/$TABLE.txt
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
if [ $DATABASES_TABLE_CHECK ]; then
|
||||
if [ -f "$DIR/$BDD/$TABLE.ibd" ]; then
|
||||
if [ ! $(innochecksum $DIR/$TABLE.ibd) ]; then
|
||||
f_log "$TABLE [OK]"
|
||||
else
|
||||
f_log "$TABLE [ERR]"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
done
|
||||
|
||||
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
|
||||
mysql --defaults-file=$CONFIG_FILE $BDD < $DIR/__routines.sql 2>/dev/null
|
||||
fi
|
||||
|
||||
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
|
||||
mysql --defaults-file=$CONFIG_FILE $BDD < $DIR/__views.sql 2>/dev/null
|
||||
fi
|
||||
|
||||
f_log "Flush privileges;"
|
||||
time mysql --defaults-extra-file=$CONFIG_FILE -e "flush privileges;"
|
||||
mysql --defaults-file=$CONFIG_FILE -e "flush privileges;"
|
||||
|
||||
f_log "** END **"
|
||||
|
||||
|
@ -115,16 +137,14 @@ This script restore databases.
|
|||
|
||||
OPTIONS:
|
||||
-e Exclude databases
|
||||
-s Selected databases
|
||||
-c Check innochecksum of table after import
|
||||
-s Selected databases
|
||||
-c Check innochecksum of table after import
|
||||
|
||||
|
||||
EOF
|
||||
}
|
||||
|
||||
# === CHECKS ===
|
||||
#if [ $# = 0 ]; then
|
||||
# usage;
|
||||
# exit;
|
||||
#fi
|
||||
BACKUP_DIR=$(pwd)
|
||||
|
||||
BIN_DEPS="ls grep awk sort uniq bunzip2 bzip2 mysql"
|
||||
|
||||
|
@ -171,6 +191,13 @@ do
|
|||
esac
|
||||
done
|
||||
|
||||
# === AUTORUN ===
|
||||
restore $(pwd)
|
||||
# === SETTINGS ===
|
||||
f_log "============================================"
|
||||
f_log "Restore from: $BACKUP_DIR"
|
||||
f_log "Config file: $CONFIG_FILE"
|
||||
f_log "Verbose: $VERBOSE"
|
||||
f_log "============================================"
|
||||
f_log ""
|
||||
|
||||
# === AUTORUN ===
|
||||
restore $BACKUP_DIR
|
||||
|
|
Loading…
Reference in New Issue