· 10 years ago · Jan 27, 2016, 05:06 PM
1#!/bin/sh
2
3##################
4# Path Variables #
5##################
6
7localdir=/tmp/EDINET/
8loadsqlfile=/tmp/sqlimport.csv
9
10##########################
11# Database Configuration #
12##########################
13
14dbhost=hostname
15dbuser=username
16dbpass=password
17dbname=databasename
18dbtable=SALES_CUSTOMERINFO_DETAILS
19
20######################
21# SFTP Configuration #
22######################
23
24ftphost=ftphostname
25ftpuser=username
26ftppass=password
27ftpport=222
28ftpremotedir=inbox/850
29ftpfilepattern="EDINETAS*.csv"
30ftparchive=inbox/850/Archive
31ftpoptions="set xfer:clobber on"
32
33#########################################################################
34# Interesting columns in source CSV file #
35# For Information Purposes Only #
36#########################################################################
37# PO Number Column 6 mysql: sessionid #
38# PO Date Column 7 mysql: PeriodEndingDate #
39# Ordered By Code Column 71 mysql: StoreNumber #
40# Vendor Item Number Column 145 mysql: ItemNumber #
41# Total Transaction Amount Column 129 mysql: Amt #
42# Total Ordered Quantity Column 130 mysql: Qty #
43#########################################################################
44
45
46#######################################
47# Make sure that $localdir exists #
48# Create if it does not already exist #
49#######################################
50
51if [ ! -d $localdir ]
52then
53 mkdir -p $localdir
54fi
55
56#################################################################
57# Download all available files from $ftpremotedir on ftp server #
58# Abort (nothing to do) if no files are found #
59#################################################################
60
61echo -e "\nLooking for files that are available on the FTP server..."
62lftpconnect="sftp://$ftpuser:$ftppass@$ftphost:$ftpport"
63lftp $lftpconnect -e "$ftpoptions ; lcd $localdir ; mget \"$ftpremotedir/$ftpfilepattern\" ; bye" 1>/dev/null 2>&1
64
65if [ $? = 0 ]
66then
67 echo -e "-> Successfully retrieved available files."
68else
69 echo -e "-> No files found, aborting.\n"
70 exit 1
71fi
72
73###############################################################
74# Loop through and process all files that were downloaded #
75###############################################################
76
77find $localdir -maxdepth 1 -name "$ftpfilepattern" -printf '%f\n' | while read srcfile
78do
79 echo -e "\nProcessing $srcfile"
80
81 #######################################################################
82 # Extract only interesting columns from CSV (6,7,71,145,129,130) #
83 # Use , (comma) for both input and output field delimeter #
84 # Ignore line #1 (header, output only lines greater than 1 #
85 # Convert PO Date in column #7 from MM/DD/YYYY to YYYY-MM-DD #
86 # Print only columns 6,7,71,145,129,130 (interesting columns) #
87 # Create $loadsqlfile which contains records to be imported into SQL #
88 #######################################################################
89
90 echo -e "-> Extract columns and convert date string"
91 awk 'BEGIN { FS = OFS = "," }
92 NR>1{ split ($7, podate, /\//)
93 $7 = podate[3] "-" podate[1] "-" podate[2] ;
94 print $6,$7,$71,$145,$129,$130 }' "$localdir/$srcfile" > $loadsqlfile
95
96 #######################################################################
97 # Load new .CSV contents of $loadsqlfile into MySQL database #
98 #######################################################################
99
100 echo -e "-> Loading records into database"
101 mysql -h $dbhost -u$dbuser -p$dbpass $dbname -e \
102 "load data local infile '$loadsqlfile' into table $dbtable
103 fields terminated by ',' \
104 (sessionid,PeriodEndingDate,StoreNumber,ItemNumber,Amt,Qty);"
105
106 ##############################################################################
107 # If MySQL import is successful, remove local files and archive remote files #
108 # Otherwise, leave files in place so they can be re-tried next time #
109 ##############################################################################
110
111 if [ $? = 0 ]
112 then
113 echo -e "-> Removing local files"
114 rm -f "$localdir/$srcfile"
115 rm -f "$loadsqlfile"
116
117 echo -e "-> Archiving remote file"
118 lftp $lftpconnect -e "mv \"$ftpremotedir/$srcfile\" \"$ftparchive/$srcfile\" ; bye"
119 fi
120done
121
122###########################################
123# No more files to process, end of script #
124###########################################
125
126echo -e "\nEnd of file processing\n"
127exit