All Downloads are FREE. Search and download functionalities are using the official Maven repository.

refcodes-batch.1.0.2.source-code.convert-latin1-db-to-utf-8.job Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
#!/bin/bash

if [[ $HELP = y ]] ; then
	echo "The switch \"-j $JOB\" uses a given configuration to                            " >&2
	echo "convert a given DB, which is in \"latin-1\" charset into one in charset         " >&2
	echo "\"UTF-8\":                                                                      " >&2
	# echo "--------------------------------------------------------------------------------" >&2
	logSeparator
fi

. $SCRIPT_DIR/validate-db-config.job

if [[ $HELP = y ]] ; then
	exit $EXIT_SUCCESS
fi

exitOnConfigOff "DB"

DUMP_DIR="$HOME"
DUMP_FILE="$DUMP_DIR/$DB_NAME.sql"
DUMP_UTF8_FILE="$DUMP_DIR/$DB_NAME-UTF8.sql"

if [[ $VERBOSE = y ]] ; then
	echo "Assuming the path \"$DUMP_DIR\" to exist ..." >&2
	# echo "--------------------------------------------------------------------------------" >&2
	logSeparator
fi

exitOnMissingVariablePath "DUMP_DIR"

if [ -e "$DUMP_FILE" ] ; then
	echo "Removing \"$DUMP_FILE\" ..." >&2
	# echo "--------------------------------------------------------------------------------" >&2
	logSeparator
	rm $DUMP_FILE
	exitOnError "Unable to remove \"$DUMP_FILE\" !!!"	
fi

if [ -e "$DUMP_UTF8_FILE" ] ; then
	echo "Removing \"$DUMP_UTF8_FILE\" ..." >&2
	# echo "--------------------------------------------------------------------------------" >&2
	logSeparator
	rm $DUMP_UTF8_FILE
	exitOnError "Unable to remove \"$DUMP_UTF8_FILE\" !!!"	
fi

if [[ $VERBOSE = y ]] ; then
	echo "Dumping DB \"$DB_NAME\" to \"$DUMP_FILE\" ..." >&2
	# echo "--------------------------------------------------------------------------------" >&2
	logSeparator
fi

mysqldump -h $DB_HOSTNAME --user=$DB_USER_NAME -p$DB_USER_PASSWORD --default-character-set=latin1 -c --insert-ignore --skip-set-charset -r $DUMP_FILE $DB_NAME

exitOnError "Unable to dump \"$DB_NAME\" to \"$DUMP_FILE\" !!!"

if [[ $VERBOSE = y ]] ; then
	DB_DUMP_INFO=$(file $DUMP_FILE)
	echo "DB encoding of $DB_DUMP_INFO" >&2
fi

if [[ $VERBOSE = y ]] ; then
	echo "Converting \"$DUMP_FILE\" to \"$DUMP_UTF8_FILE\" ..." >&2
fi

iconv -f ISO8859-1 -t UTF-8 $DUMP_FILE > $DUMP_UTF8_FILE

exitOnError "Unable to convert encoding from \"$DUMP_FILE\" to \"$DUMP_UTF8_FILE\" !!!"

perl -pi -w -e 's/CHARSET=latin1/CHARSET=utf8/g;' $DUMP_UTF8_FILE

exitOnError "Unable convert encoding in \"$DUMP_UTF8_FILE\" (run 1) !!!"

perl -pi -w -e 's/CHARACTER SET latin1 COLLATE latin1_bin/CHARACTER SET utf8 COLLATE utf8_general_ci/g;' $DUMP_UTF8_FILE

exitOnError "Unable convert encoding in \"$DUMP_UTF8_FILE\" (run 2) !!!"

# if [[ $VERBOSE = y ]] ; then
# 	echo "Dropping and creating DB \"$DB_NAME\" ..." >&2
# 	echo "--------------------------------------------------------------------------------" >&2
# fi

# mysql --user=$DB_USER_NAME -p$DB_USER_PASSWORD --execute="DROP DATABASE $DB_NAME; CREATE DATABASE $DB_NAME CHARACTER SET utf8 COLLATE utf8_general_ci;"

# exitOnError "Unable to drop and create \"$DB_NAME\" !!!"

# if [[ $VERBOSE = y ]] ; then
# 	echo "Importing DB dump \"$DUMP_UTF8_FILE\" to DB \"$DB_NAME\" ..." >&2
# 	echo "--------------------------------------------------------------------------------" >&2
# fi

# mysql --user=$DB_USER_NAME --max_allowed_packet=16M -p$DB_USER_PASSWORD --default-character-set=utf8 $DB_NAME < $DUMP_UTF8_FILE

exitOnError "Unable to import \"$DB_NAME\" from DB dump \"$DUMP_UTF8_FILE\" !!!"

echo "DB \"$DB_NAME\" was converted to \"UTF-8\":" >&2
# echo "--------------------------------------------------------------------------------" >&2
logSeparator
echo "+ Drop \"$DB_NAME\" and import DB \"UTF-8\" file \"$DUMP_UTF8_FILE\" to \"$DB_NAME\" ..." >&2
# echo "--------------------------------------------------------------------------------" >&2
logSeparator

showSuccess "DB \"$DB_NAME\" converted from \"latin-1\" to \"UTF-8\" !"




© 2015 - 2024 Weber Informatics LLC | Privacy Policy