General
==============================
Send file from Linux bash using scp
==============================
Description
==============================
A process is creating a big Report file(s) and additional file with the report(s) file(s) name(s).
A crontab task reads the "Parent" file
Process the file line by line
For each line scp the Reports listed in "Parent" file to another server using scp.
==============================
code
==============================
bash code
code
==============================
bash code
/usr/iu/workarea/scripts/MEGA_EVENT/move_report_to_mng_server.sh
#scp -r /starhome/iu/workarea/ora_exp/Export_Outbound_Roamers_* my_server:/starhome/data_export/ita-vod/megaevent/ && rm /starhome/iu/workarea/ora_exp/Export_Outbound_Roamers_*
FILES_DIR=/starhome/iu/workarea/ora_exp
LOG_FILE=${WORK_DIR}/SCP_VRS_REPORTS.log
TARGET_SERVER=my_server
TARGET_USER=iu
TARGET_PATH=/starhome/data_export/ita-vod/megaevent
SCP_FILE_NAME=scp_to_${TARGET_SERVER}.sh
PARENT_FILE=${FILES_DIR}/megaevent_created.txt
#main()
RUN_DATE=`date "+%Y%m%d"_"%H%M%S" `
touch ${LOG_FILE}
echo "===============================================" >> $LOG_FILE
echo "Starting at $RUN_DATE" >> $LOG_FILE
if [[ -f ${PARENT_FILE} ]]; then
REPORTS_FILES=`less ${PARENT_FILE} | wc -l`
else
echo "No Input Files to Process. ${PARENT_FILE} does not exists " >> ${LOG_FILE}
echo "Finished at $RUN_DATE" >> $LOG_FILE
echo "===============================================" >> $LOG_FILE
exit 0
fi
if [[ $REPORTS_FILES -eq 0 ]]; then
echo "No Input Files to Process. ${PARENT_FILE} is empty " >> ${LOG_FILE}
echo "Finished at $RUN_DATE" >> $LOG_FILE
echo "===============================================" >> $LOG_FILE
exit 0
else
echo "Got $REPORTS_FILES Input Files to Process. " >> ${LOG_FILE}
fi
REPORTS_FILE_NAME=`less ${PARENT_FILE}`
while IFS='' read -r line || [[ -n "$line" ]]; do
REPORTS_FILE_NAME=$line
echo sending ${REPORTS_FILE_NAME} >> $LOG_FILE
echo "scp ${FILES_DIR}/${REPORTS_FILE_NAME} ${TARGET_USER}@${TARGET_SERVER}:${TARGET_PATH}/." > ${WORK_DIR}/${SCP_FILE_NAME}
chmod +x ${WORK_DIR}/${SCP_FILE_NAME}
echo "Running ${WORK_DIR}/${SCP_FILE_NAME} " >> $LOG_FILE
${WORK_DIR}/${SCP_FILE_NAME}
rm -f ${FILES_DIR}/${REPORTS_FILE_NAME}
done < ${PARENT_FILE}
mv -f ${PARENT_FILE} ${WORK_DIR}/megaevent_sent_${RUN_DATE}
RUN_DATE=`date "+%Y%m%d"_"%H%M%S" `
echo "Finished at $RUN_DATE" >> $LOG_FILE
echo "===============================================" >> $LOG_FILE
exit 0
PL/SQL code
PROCEDURE Export_Subscribers_Report(pVlrs IN varchar2, pCountries IN varchar2) IS
-----------------------------------------------------------------------------------
-- Propose : Procedure to export data to csv file.
--
-- Parameters : pVlrIds - List of VLRs.
-- pCountryIds - List of Coutnries.
-----------------------------------------------------------------------------------
CURSOR ResultCrs (pVlrIds IN VARCHAR2, pCountryIds IN VARCHAR2) IS
SELECT s.msisdn || ','|| s.imsi || ','|| s.current_vlr ||','|| c.country_name AS TEXT
FROM SGA_W_PSMS_SUBSCRIBER s, GSM_COUNTRIES c
WHERE ( s.current_vlr IN
(SELECT regexp_substr(txt, '[^,]+', 1, level)
FROM (SELECT pVlrIds AS txt FROM DUAL)
CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(txt, '[^,]')) + 1)
OR
s.current_country IN
(SELECT regexp_substr(txt, '[^,]+', 1, level)
FROM (SELECT pCountryIds AS txt FROM DUAL)
CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(txt, '[^,]')) + 1))
AND s.is_subscriber = 1
AND s.is_roaming = 1
and s.current_country = c.country_id ;
CURSOR AllResultCrs IS
SELECT s.msisdn || ','|| s.imsi ||','|| s.current_vlr ||','|| c.country_name AS TEXT
FROM SGA_W_PSMS_SUBSCRIBER s, GSM_COUNTRIES c
WHERE s.is_subscriber = 1
AND s.is_roaming = 1
and s.current_country = c.country_id ;
NewFile UTL_FILE.FILE_TYPE;
vPath VARCHAR2(30);
vFileName VARCHAR2(200) := '';
vStartDateString VARCHAR2(19);
vSqlerrm VARCHAR2(2000);
ParentFile UTL_FILE.FILE_TYPE;
v_parent_file VARCHAR2(100);
BEGIN
vStartDateString := TO_CHAR(SYSDATE,'YYYYMMDD_HH24MISS');
vPath := 'IG_EXP_DIR';
vFileName := 'Export_Outbound_Roamers_' || vStartDateString|| '.csv';
v_parent_file := 'megaevent_created.txt';
BEGIN
NewFile := UTL_FILE.FOPEN(vPath, vFileName, 'W', 10000);
EXCEPTION
WHEN OTHERS THEN
UTL_FILE.FCLOSE(NewFile);
END;
IF pVlrs IS NULL AND pCountries IS NULL THEN
FOR ResultRec IN AllResultCrs LOOP
UTL_FILE.PUT_LINE(NewFile, ResultRec.Text);
END LOOP;
ELSE
FOR ResultRec IN ResultCrs(pVlrs,pCountries) LOOP
UTL_FILE.PUT_LINE(NewFile, ResultRec.Text);
END LOOP;
END IF;
UTL_FILE.FCLOSE(NewFile);
BEGIN
ParentFile := UTL_FILE.FOPEN(vPath, v_parent_file, 'A', 10000);
UTL_FILE.PUT_LINE(ParentFile, vFileName);
UTL_FILE.FCLOSE(ParentFile);
EXCEPTION
WHEN OTHERS THEN
sga_pkg.write_sga_w_log('PSMS_SUBSCRIBER_PKG.Export_Subscribers_Report', 'Unexpected Error. Failed to close Parent File: ' ||v_parent_file);
sga_pkg.write_sga_w_log('PSMS_SUBSCRIBER_PKG.Export_Subscribers_Report', 'Unexpected Error: ' || sqlerrm);
END;
EXCEPTION
WHEN OTHERS THEN
sga_pkg.write_sga_w_log('PSMS_SUBSCRIBER_PKG.Export_Subscribers_Report', 'Unexpected Error: ' || sqlerrm);
UTL_FILE.FCLOSE(NewFile);
END Export_Subscribers_Report;
No comments:
Post a Comment