=fix load data from source file with charset cp1251

This commit is contained in:
Mirocow 2016-03-18 00:15:50 +03:00
parent e2efabe8e4
commit c635e3bc29
4 changed files with 56 additions and 33 deletions

View file

@ -188,6 +188,8 @@ Examples:
backup.sh --verbose --dir="/var/backups/mysql" --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" --lifetime="1 day ago"
backup.sh --verbose --dir="/home/backups/mysql" --exclude="mysql" --exclude-tables="tbl_template" --lifetime="1 day ago" backup.sh --verbose --dir="/home/backups/mysql" --exclude="mysql" --exclude-tables="tbl_template" --lifetime="1 day ago"
EOF EOF
} }

View file

@ -185,6 +185,8 @@ Examples:
backup.sh --verbose --dir="/var/backups/mysql" --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" --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" --exclude-tables="tbl_template" --lifetime="1 day ago"
EOF EOF
} }

View file

@ -113,13 +113,20 @@ restore()
fi fi
if [ -s "$DIR/$BDD/$TABLE.txt" ]; then if [ -s "$DIR/$BDD/$TABLE.txt" ]; then
if [ ! -z "$(cat $DIR/$BDD/$TABLE.sql | grep -i 'DEFAULT CHARSET=CP1251')" ]; then
charset='cp1251'
else
charset='utf8'
fi
f_log "+ $TABLE" f_log "+ $TABLE, Set default charset: $charset"
split -l $CONFIG_CHUNK "$DIR/$BDD/$TABLE.txt" "$DIR/$BDD/${TABLE}_part_" split -l $CONFIG_CHUNK "$DIR/$BDD/$TABLE.txt" "$DIR/$BDD/${TABLE}_part_"
for segment in "$DIR/$BDD/${TABLE}"_part_*; do for segment in "$DIR/$BDD/${TABLE}"_part_*; do
f_log "Restore from $segment" f_log "Restore from $segment"
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; 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 character_set_database = $charset;
LOAD DATA LOCAL INFILE '$segment' LOAD DATA LOCAL INFILE '$segment'
INTO TABLE $TABLE; INTO TABLE $TABLE;
SET foreign_key_checks = 1; SET unique_checks = 1; SET sql_log_bin = 1;" SET foreign_key_checks = 1; SET unique_checks = 1; SET sql_log_bin = 1;"
@ -130,13 +137,13 @@ restore()
fi fi
done done
if [ -f "$DIR/$BDD/$TABLE.txt.bz2" ]; then
f_log "Delete source: $TABLE.txt"
rm $DIR/$BDD/$TABLE.txt
fi
fi 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 [ $DATABASES_TABLE_CHECK ]; then
if [ -f "$DIR/$BDD/$TABLE.ibd" ]; then if [ -f "$DIR/$BDD/$TABLE.ibd" ]; then
if [ ! $(innochecksum $DIR/$BDD/$TABLE.ibd) ]; then if [ ! $(innochecksum $DIR/$BDD/$TABLE.ibd) ]; then
@ -180,6 +187,8 @@ OPTIONS:
-e Exclude databases -e Exclude databases
-s Selected databases -s Selected databases
-c Check innochecksum of table after import -c Check innochecksum of table after import
EOF EOF
} }

View file

@ -69,38 +69,46 @@ restore()
fi fi
if [ -s "$DIR/$TABLE.txt" ]; then if [ -s "$DIR/$TABLE.txt" ]; then
f_log "+ $TABLE"
if [ ! -z "$(cat $DIR/$BDD/$TABLE.sql | grep -i 'DEFAULT CHARSET=CP1251')" ]; then
charset='cp1251'
else
charset='utf8'
fi
f_log "+ $TABLE, Set default charset: $charset"
split -l $CONFIG_CHUNK "$DIR/$TABLE.txt" "$DIR/${TABLE}_part_" split -l $CONFIG_CHUNK "$DIR/$TABLE.txt" "$DIR/${TABLE}_part_"
for segment in "$DIR/${TABLE}"_part_*; do for segment in "$DIR/${TABLE}"_part_*; do
f_log "Restore from $segment" f_log "Restore from $segment"
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; 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' SET character_set_database = $charset;
INTO TABLE $TABLE; LOAD DATA LOCAL INFILE '$segment'
SET foreign_key_checks = 1; SET unique_checks = 1; SET sql_log_bin = 1;" INTO TABLE $TABLE;
SET foreign_key_checks = 1; SET unique_checks = 1; SET sql_log_bin = 1;"
if [ -f "$segment" ]; then if [ -f "$segment" ]; then
f_log "Delete segment $segment" f_log "Delete segment $segment"
rm "$segment" rm "$segment"
fi fi
done done
if [ ! -f "$DIR/$TABLE.txt.bz2" ]; then if [ ! -f "$DIR/$TABLE.txt.bz2" ]; then
f_log "Delete source file: $TABLE.txt" f_log "Delete source file: $TABLE.txt"
rm $DIR/$TABLE.txt rm $DIR/$TABLE.txt
fi fi
fi fi
if [ $DATABASES_TABLE_CHECK ]; then if [ $DATABASES_TABLE_CHECK ]; then
if [ -f "$DIR/$BDD/$TABLE.ibd" ]; then if [ -f "$DIR/$BDD/$TABLE.ibd" ]; then
if [ ! $(innochecksum $DIR/$TABLE.ibd) ]; then if [ ! $(innochecksum $DIR/$TABLE.ibd) ]; then
f_log "$TABLE [OK]" f_log "$TABLE [OK]"
else else
f_log "$TABLE [ERR]" f_log "$TABLE [ERR]"
fi
fi
fi fi
fi
fi
done done
@ -137,6 +145,8 @@ OPTIONS:
-e Exclude databases -e Exclude databases
-s Selected databases -s Selected databases
-c Check innochecksum of table after import -c Check innochecksum of table after import
EOF EOF
} }