Last active
May 3, 2021 18:03
-
-
Save wicadmin/22857e2ef4ef65e8420175cecbfd09fd to your computer and use it in GitHub Desktop.
LFTP Progress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a script I use which runs on a scheduler (e.g. cron) every 5 min and sends the progress of a LFTP transfer to MQTT. | |
In my case I then have NodeRed read it and send it to Telegram. Also, I run LFTP in a docker container, | |
so adjust if you do not. Those parts are not covered here. | |
I also use three threads, so adjust that accordingly also. | |
#!/bin/bash | |
exec 100>/var/tmp/lftpprogresslock.lock || exit 1 | |
flock -n 100 || exit 1 # Exit if process is already running | |
trap 'rm -f /var/tmp/lftpprogresslock.lock' EXIT | |
CONTNM="lftp-CONTAINER" | |
DEBUG=false | |
if [ ${DEBUG} = true ]; then | |
TEST_STR="Transferring file \`Files/ISO/Ubuntu/Ubuntu_16.04.iso'" | |
fi | |
# Check if lftp-CONTAINER is running | |
PROC=$(docker ps | grep -c ${CONTNM}) | |
if [ $PROC -gt 0 ] || [ $DEBUG = true ]; then | |
FILEPATH_PART=$(docker logs --tail 1 ${CONTNM} | grep "Transferring file" | sed 's/^Transferring file *//') | |
if [ ${DEBUG} = true ]; then | |
FILEPATH_PART=$(echo "${TEST_STR}" | grep "Transferring file" | sed 's/^Transferring file *//') | |
fi | |
if [ ! -z "${FILEPATH_PART}" ]; then | |
FILEPATH="/storage/${FILEPATH_PART:1:-1}" | |
DLFILE=$(echo "${FILEPATH_PART}" | cut -d'/' -f4-) | |
STATUS_FILE="${FILEPATH}.lftp-pget-status" | |
STATUS_FILE_CONTENTS=$(cat "${STATUS_FILE}") | |
if [ ${DEBUG} = true ]; then | |
STATUS_FILE_CONTENTS=$(cat /tmp/teststatusfile.txt) # create this temp file to test when DEBUG is enabled | |
fi | |
FILE_SIZE=$(echo "${STATUS_FILE_CONTENTS}" | grep "size=" | sed 's/^size=//') | |
POS0=$(echo "${STATUS_FILE_CONTENTS}" | grep "0.pos=" | sed 's/^0.pos=//') | |
POS1=$(echo "${STATUS_FILE_CONTENTS}" | grep "1.pos=" | sed 's/^1.pos=//') | |
POS2=$(echo "${STATUS_FILE_CONTENTS}" | grep "2.pos=" | sed 's/^2.pos=//') | |
LMT0=$(echo "${STATUS_FILE_CONTENTS}" | grep "0.limit=" | sed 's/^0.limit=//') | |
LMT1=$(echo "${STATUS_FILE_CONTENTS}" | grep "1.limit=" | sed 's/^1.limit=//') | |
LMT2=$(echo "${STATUS_FILE_CONTENTS}" | grep "2.limit=" | sed 's/^2.limit=//') | |
PCT_COMPLETE=$(bc <<< "scale=2; ((($POS2-$LMT1)+($POS1-$LMT0)+($POS0))/$FILE_SIZE)*100") | |
mosquitto_pub -t <mqtt topic> -h <mqtt hostname> -m "${DLFILE} is ${PCT_COMPLETE}% complete" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment