Professional Documents
Culture Documents
The example guides you on how to automate the updating of the IP2Location DB24 database. We will show how to download the IP2Location DB24 csv data from the web server and then unzip it before loading the data into MySQL server on a Linux platform. The whole shell script code is available at http://ip2location.com/tutorials/automate-downloading-unzipping-loading-db24-into-linux-mysql.
Step 3: Creating the temporary folder for storing the downloaded zipped data file
If the temporary folder does not exist, it will be created. An error message will be shown if the folder cannot be created and the script will terminate itself. if [ ! -d /tmp/ip2location ]; then echo -n "Create temporary directory........... " mkdir /tmp/ip2location if [ ! -d /tmp/ip2location ]; then error "Failed to create /tmp/ip2location" exit 0 fi success "[OK]" fi
Step 4: Downloading the zipped data file from the web server
If download is not successful then an error message will be shown and the script will terminate itself. wget -O database.zip -q http://www.ip2location.com/download?login=$LOGIN\&password=$PASS\&prod uctcode=$CODE 2>&1 if [ ! -f database.zip ]; then error "Download failed." exit 0 fi if [ ! -z "$(grep 'NO PERMISSION' database.zip)" ]; then error "Permission denied." exit 0 fi if [ ! -z "$(grep '5 times' database.zip)" ]; then error "Download quota exceed." exit 0 fi if [ $(wc -c < database.zip) -lt 102400 ]; then error "Download failed." exit 0 fi
exit 0 fi
Step 7: Loading the CSV data into the MySQL temporary table
Loads the CSV data into the temporary table. If an error is encountered, an error message will be shown and the script will terminate itself. RESULT="$(mysql -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME -e 'LOAD DATA LOCAL INFILE '\'''$NAME''\'' INTO TABLE `ip2location_database_tmp` FIELDS TERMINATED BY '\'','\'' ENCLOSED BY '\''\"'\'' LINES TERMINATED BY '\''\r\n'\'';' 2>&1)" if [ ! -z "$(echo $RESULT)" ]; then error "Failed." exit 0 fi
Step 9: Rename the temporary table to become the live data table
Renames the temporary table to become the live data table. If an error is encountered, an error message will be shown and the script will terminate itself. RESULT="$(mysql -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME -e 'RENAME TABLE `ip2location_database_tmp` TO `ip2location_database`;' 2>&1)" if [ ! -z "$(echo $RESULT)" ]; then error "Failed to rename table." exit 0 fi
Step 10: Remove temporary download folder and the downloaded data file
Remove the temporary download folder and all files in that folder. rm -rf /tmp/ip2location