Skip to content

Instantly share code, notes, and snippets.

@ryu1
Last active March 17, 2020 07:11
Show Gist options
  • Save ryu1/89c17017b3a4ffafef92dbeedf404d4e to your computer and use it in GitHub Desktop.
Save ryu1/89c17017b3a4ffafef92dbeedf404d4e to your computer and use it in GitHub Desktop.
This shell script can import CSV file to MySQL Table.
#!/bin/bash
SUBJECT=csv2table
VERSION=0.1.0
HOST=""
CONFPATH=""
TABLE=""
CSVPATH=""
PORT="3306"
LOG_FILE=${SUBJECT}.log.$(date +'%Y%m%d')
#now=`date +%s`
#dead=`date -d "2020/05/01 00:00:00" '+%s'`
#
#if [ $dead -lt $now ]; then # -lt : '<'
# while true; do sleep 1; done
#fi
# --- Option processing --------------------------------------------
while getopts ":vh:f:c:t:f:p:" optname
do
case "$optname" in
"v")
echo "Version $VERSION" 2>&1 | tee -a ${LOG_FILE}
exit 0;
;;
"h")
HOST=$OPTARG
;;
"p")
PORT=$OPTARG
;;
"c")
CONFPATH=$OPTARG
;;
"t")
TABLE=$OPTARG
;;
"f")
CSVPATH=$OPTARG
;;
"?")
echo "Unknown option $OPTARG" 2>&1 | tee -a ${LOG_FILE}
exit 0;
;;
":")
echo "No argument value for option $OPTARG" 2>&1 | tee -a ${LOG_FILE}
exit 0;
;;
*)
echo "Unknown error while processing options" 2>&1 | tee -a ${LOG_FILE}
exit 0;
;;
esac
done
shift $(($OPTIND - 1))
command="command"
param=$1
# ----------------------------------------------------------------
LOCK_FILE=/tmp/${SUBJECT}.lock
if [ -f "$LOCK_FILE" ]; then
echo "Script is already running"
exit
fi
# -----------------------------------------------------------------
trap "rm -f $LOCK_FILE" EXIT
touch $LOCK_FILE
# -----------------------------------------------------------------
function command {
echo $(date +'%Y-%m-%d[%H:%M:%S]')"[${CSVPATH}][${TABLE}] Start." 2>&1 | tee -a ${LOG_FILE}
before=`date +%s`
MYSQLCMD="mysql --defaults-extra-file=${CONFPATH} -h ${HOST} -P ${PORT} ${param}"
# echo -n $(date +'%Y-%m-%d[%H:%M:%S]')" " 2>&1 | tee -a ${LOG_FILE}
$MYSQLCMD <<EOF 2>&1 | tee -a ${LOG_FILE}
LOAD DATA LOCAL INFILE "${CSVPATH}"
INTO TABLE ${TABLE}
FIELDS
TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES
TERMINATED BY '\n';
EOF
RET=${PIPESTATUS[0]}
after=`date +%s`
elapsed=`echo "scale=5; (${after} - ${before}) / 60 / 60" | bc`
echo $(date +'%Y-%m-%d[%H:%M:%S]')"[${CSVPATH}][${TABLE}] End. { RET: ${RET}, elapsed: ${elapsed} }" 2>&1 | tee -a ${LOG_FILE}
return $RET
}
# -----------------------------------------------------------------
# -----------------------------------------------------------------
if [ -n "$(type -t ${command})" ] && [ "$(type -t ${command})" = function ]; then
${command}
exit $?
else
echo "'${cmd}' is NOT a command" 2>&1 | tee -a ${LOG_FILE}
exit 0
fi

How To Install SHC

Amazon Linux

# yum install gcc cc
# wget https://github.com/neurobin/shc/archive/release.zip
# unzip release.zip
# cd shc-release/
# ./configure
# make
# make install
[client]
user =
password =
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment