Skip to content

Instantly share code, notes, and snippets.

@NeutralKaon
Last active December 4, 2018 10:40
Show Gist options
  • Save NeutralKaon/5c0e2bd9ce7730db0a330f56fbb559c7 to your computer and use it in GitHub Desktop.
Save NeutralKaon/5c0e2bd9ce7730db0a330f56fbb559c7 to your computer and use it in GitHub Desktop.
#!/bin/bash
# A silly wrapper script to halt University of Oxford file
# transfers to the central Tivoli Storage Manager / HFS Backup / IBM Spectrum Protect
# (take your pick, they're all the same) backup robot place.
#
# Usage: sudo /path/to/script /path/to/backup/location
#
# Requires python3 accessible as python3, and the regex / os modules.
# Tested on MacOS and Linux
#
#
BYTES_SENT=0;
#MAX_SIZE_TO_SEND=150 #Bytes, for testing
MAX_SIZE_TO_SEND=$[185*(2**30)] #185 GiB <base 2> is approximately 198.6 GB <base 10> -- don't know which one they use for accounting for size.
args=("$@")
sudo rm -f /tmp/dsmc-script.PID
function outputParser() {
python3 <<'EOF'
import os, re
rex=re.compile(r"Normal File\-\-\>\s*?([,0-9]*,?)\s*?\/")
valueToParse=os.environ.get('line');
match=rex.match(valueToParse);
try:
stringToReturn = str(match.group(1));
stringToReturn =stringToReturn.replace(',','');
except AttributeError:
stringToReturn = "";
#Check for failed transfers
failedResults = re.findall(r"\*\* Unsuccessful \*\*", valueToParse);
nFailedResults = len(failedResults);
if (nFailedResults >0):
stringToReturn = "";
print(stringToReturn);
EOF
} #I am sure that the above is a one-liner in sed or awk. I just don't know what the one line is.
function trapCaught() {
printf "\nCaught signal -- sending SIGINT to "
pid=$(cat /tmp/dsmc-script.PID);
sudo kill -SIGTERM $pid;
echo -n "pid $pid"
sudo rm -f /tmp/dsmc-script.PID;
sleep 5;
sudo kill -9 $pid;
sleep 0.1;
sudo kill -9 0;
echo ", quitting."
}
trap trapCaught sigint
killCount=0
startTime=$SECONDS
while read -r line; do
echo "$line"
export line;
X=$(export line=$line; outputParser)
if [[ ! -z "$X" ]]; then
BYTES_SENT=$[$BYTES_SENT + $X]
echo "Sent $X bytes, $BYTES_SENT in total"
fi
if (( BYTES_SENT > MAX_SIZE_TO_SEND )); then
if (( killCount < 1)); then
echo "STOPPED BACKUP BECAUSE $BYTES_SENT is GREATER THAN THE PERMITTED MAXIMUM OF $MAX_SIZE_TO_SEND";
killStartTime=$(( SECONDS - startTime ))
pid=$(cat /tmp/dsmc-script.PID)
echo "PID is $pid"
echo $pid | sudo xargs kill
fi
killCount=$[$killCount + 1];
timeKillNow=$(( SECONDS - killStartTime ))
rm -f /tmp/dsmc-script.PID
if (( killCount > 100 || timeKillNow > 30 )); then
echo "Taking too long to die; retrying"
echo $pid | sudo xargs kill -9;
sleep 0.1;
sudo kill -9 0;
fi
fi
done < <( sudo dsmc incr ${args[0]} & echo $! > /tmp/dsmc-script.PID )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment