Skip to content

Instantly share code, notes, and snippets.

@tmzh
Created July 31, 2020 01:10
Show Gist options
  • Save tmzh/725a84bfe969fe63d439813f902fd6df to your computer and use it in GitHub Desktop.
Save tmzh/725a84bfe969fe63d439813f902fd6df to your computer and use it in GitHub Desktop.
A quick shell script to scp only latest files based on file timestamp
#!/bin/bash
SUB_FOLDER=$1
FILE_PATRN=$2
function usage {
echo "Usage: $0 SUB_FOLDER [FILE_PATTERN]"
exit 1
}
BASE_DIR=/data
SCP_TARGET_HOST=user@a.b.c.d
SCP_TARGET_DIR=/home/scp_files
SOURCE_FOLDER=${BASE_DIR}/${SUB_FOLDER}
LAST_CHECKPOINT=$SOURCE_FOLDER/checkpoint
# Get all files matching the file pattern and newer than the checkpoint
# In case there are multiple files, just choose the latest one to use
# If file pattern is not present, use *.csv as a default
FILE=$(find $SOURCE_FOLDER -name "${FILE_PATRN:-*.csv}" -newer ${LAST_CHECKPOINT} -type f | sort | tail -n 1 )
# Compare the timestamp with checkpoint
if [[ ${FILE} ]]; then
echo "Newer file found! Copying ${FILE} to informatica..."
scp "${FILE}" "${SCP_TARGET_HOST}:${SCP_TARGET_DIR}/${FILE_PATRN}.csv"
if [[ "$?" -ne 0 ]]; then
echo "Unable to scp file"
exit 1
fi
if [[ "$?" -ne 0 ]]; then
echo "Unable to complete SCP"
exit 1
else
echo "Updating the checkpoint file.."
touch $LAST_CHECKPOINT
fi
else
echo "No update since last check, skipping..."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment