roughly calculate remaining time, more and better help, small fixes
This commit is contained in:
129
split-dl.sh
129
split-dl.sh
@@ -6,23 +6,39 @@ CHECKSUM="md5sum" # Define application to create hashsums
|
|||||||
FILE=""
|
FILE=""
|
||||||
MODE=""
|
MODE=""
|
||||||
|
|
||||||
|
function help {
|
||||||
|
echo "This program is used to split large files into several small
|
||||||
|
files to avoid data loss and the need to re-download them from
|
||||||
|
slow or unstable internet connections."
|
||||||
|
echo
|
||||||
|
echo "Written by Max Mehl <mail@mehl.mx> in 2015."
|
||||||
|
echo
|
||||||
|
echo "Necessary arguments to start this application:"
|
||||||
|
echo "-m Desired MODE. Possible values are \"server\" or \"client\""
|
||||||
|
echo " \"server\" splits the file on the server and needs a file for -f."
|
||||||
|
echo " \"client\" downloads the files and needs a download link for -f."
|
||||||
|
echo "-f File in the same directory to be splitted or link to be downloaded."
|
||||||
|
echo
|
||||||
|
echo "Optional arguments:"
|
||||||
|
echo "-s Define the desired size of splitted files. Default is $SPLITSIZE"
|
||||||
|
echo "-i Define the desired name of the info document.
|
||||||
|
Default is $INFO"
|
||||||
|
echo "-c Define the program which should be used to calculate the hashsum.
|
||||||
|
Default is $CHECKSUM"
|
||||||
|
echo
|
||||||
|
echo "Examples:"
|
||||||
|
echo "Split Hugefile.iso in parts of 50MB and use sha1sum as hashing program."
|
||||||
|
echo " split-dl -m server -f Hugefile.iso -s 50M -c sha1sum"
|
||||||
|
echo
|
||||||
|
echo "Download the splitted files of Hugefile.iso.
|
||||||
|
The hashing program has to be the same as on the server side."
|
||||||
|
echo " split-dl -m client -f http://example.com/dl-Hugefile.iso -c sha1sum"
|
||||||
|
}
|
||||||
|
|
||||||
while getopts h:s:i:c:f:m: opt; do
|
while getopts h:s:i:c:f:m: opt; do
|
||||||
case $opt in
|
case $opt in
|
||||||
h)
|
h)
|
||||||
echo "This program is used to split large files into several small
|
help
|
||||||
files to avoid data loss and the need to re-download them from
|
|
||||||
slow or unstable internet connections."
|
|
||||||
echo
|
|
||||||
echo "Necessary arguments to start this application:"
|
|
||||||
echo "-m Desired MODE. Possible values are \"server\" or \"client\""
|
|
||||||
echo " \"server\" splits the file on the server and needs a file for -f."
|
|
||||||
echo " \"client\" downloads the files and needs a download link for -f."
|
|
||||||
echo "-f File in the same directory to be splitted or link to be downloaded."
|
|
||||||
echo
|
|
||||||
echo "Optional arguments:"
|
|
||||||
echo "-s Define the desired size of splitted files. Default is $SPLITSIZE"
|
|
||||||
echo "-i Define the desired name of the info document. Default is $INFO"
|
|
||||||
echo "-c Define the program which should be used to calculate the hashsum. Default is $CHECKSUM"
|
|
||||||
exit 0
|
exit 0
|
||||||
;;
|
;;
|
||||||
s)
|
s)
|
||||||
@@ -46,15 +62,7 @@ done
|
|||||||
# DO NOT EDIT BELOW HERE
|
# DO NOT EDIT BELOW HERE
|
||||||
# ----------------------
|
# ----------------------
|
||||||
|
|
||||||
if [ "$FILE" == "" ] || [ "$MODE" == "" ]; then
|
# # # # # # # # # # # # # # # # # # # # # # # # #
|
||||||
echo "Missing arguments! Please define the mode with -m and the file/link with -f. More info with -h."
|
|
||||||
exit 1
|
|
||||||
#else
|
|
||||||
#echo "$MODE $SPLITSIZE $INFO $FILE $CHECKSUM"
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
# # # #
|
|
||||||
# FUNCTIONS
|
# FUNCTIONS
|
||||||
# # # #
|
# # # #
|
||||||
function checkwait {
|
function checkwait {
|
||||||
@@ -76,7 +84,14 @@ function comphash {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getdu {
|
function getdu {
|
||||||
du -b $1 | awk -F" " '{ print $1 }'
|
DU=$(du -b $1 | awk -F" " '{ print $1 }')
|
||||||
|
if [ -d $1 ] && [ "$BLOCK" != "" ]; then
|
||||||
|
# Reduce the size of an directory by the block size
|
||||||
|
# $BLOCK is defined in the one-time-run section of this script
|
||||||
|
echo $(($DU - $BLOCK))
|
||||||
|
else
|
||||||
|
echo $DU
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function compdu {
|
function compdu {
|
||||||
@@ -122,8 +137,27 @@ function checkfolder {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# # # # # # # # # # # # # # # # # # # # # # # # #
|
||||||
|
# ONE-TIME-ACTIONS
|
||||||
# # # #
|
# # # #
|
||||||
|
|
||||||
|
# Check if -m and -f are given
|
||||||
|
if [ "$FILE" == "" ] || [ "$MODE" == "" ]; then
|
||||||
|
echo "Missing arguments! Please define the mode with -m and the file/link with -f."
|
||||||
|
echo
|
||||||
|
help
|
||||||
|
exit 1
|
||||||
|
#else
|
||||||
|
#echo "$MODE $SPLITSIZE $INFO $FILE $CHECKSUM"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check size in bytes of an empty directory
|
||||||
|
TMP=$(mktemp -d)
|
||||||
|
BLOCK=$(getdu $TMP)
|
||||||
|
rm -r $TMP
|
||||||
|
|
||||||
|
|
||||||
|
# # # # # # # # # # # # # # # # # # # # # # # # #
|
||||||
# SERVER MODE
|
# SERVER MODE
|
||||||
# # # #
|
# # # #
|
||||||
if [ $MODE == "server" ]; then
|
if [ $MODE == "server" ]; then
|
||||||
@@ -167,7 +201,7 @@ checkfolder $FOLDER
|
|||||||
INFONAME=$INFO
|
INFONAME=$INFO
|
||||||
INFO="$FOLDER/$INFONAME"
|
INFO="$FOLDER/$INFONAME"
|
||||||
|
|
||||||
echo "[INFO] Calculating size and HASH sum..."
|
echo "[INFO] Calculating size and hashsum..."
|
||||||
BIGNAME=$(basename $FILE)
|
BIGNAME=$(basename $FILE)
|
||||||
BIGSIZE=$(getdu $FILE)
|
BIGSIZE=$(getdu $FILE)
|
||||||
BIGHASH=$(gethash $FILE)
|
BIGHASH=$(gethash $FILE)
|
||||||
@@ -176,6 +210,11 @@ echo "BIGNAME=$BIGNAME" >> $INFO
|
|||||||
echo "BIGSIZE=$BIGSIZE" >> $INFO
|
echo "BIGSIZE=$BIGSIZE" >> $INFO
|
||||||
echo "BIGHASH=$BIGHASH" >> $INFO
|
echo "BIGHASH=$BIGHASH" >> $INFO
|
||||||
echo >> $INFO
|
echo >> $INFO
|
||||||
|
# Variables info
|
||||||
|
echo "CHECKSUM=$CHECKSUM" >> $INFO
|
||||||
|
echo "SPLITSIZE=$SPLITSIZE" >> $INFO
|
||||||
|
echo "INFO=$INFO" >> $INFO
|
||||||
|
echo >> $INFO
|
||||||
|
|
||||||
echo "[INFO] Splitting big file into smaller parts..."
|
echo "[INFO] Splitting big file into smaller parts..."
|
||||||
split --verbose -a 4 -b $SPLITSIZE $FILE $FOLDER/dl-
|
split --verbose -a 4 -b $SPLITSIZE $FILE $FOLDER/dl-
|
||||||
@@ -204,7 +243,7 @@ echo "[INFO] Please move the folder \"$FOLDER\" to a web-accessible directory an
|
|||||||
fi # /SERVER MODE
|
fi # /SERVER MODE
|
||||||
|
|
||||||
|
|
||||||
# # # #
|
# # # # # # # # # # # # # # # # # # # # # # # # #
|
||||||
# CLIENT MODE
|
# CLIENT MODE
|
||||||
# # # #
|
# # # #
|
||||||
if [ $MODE == "client" ]; then
|
if [ $MODE == "client" ]; then
|
||||||
@@ -223,7 +262,7 @@ wget -q $URL/$INFO
|
|||||||
# Rename info.cfg to avoid colissions and source it
|
# Rename info.cfg to avoid colissions and source it
|
||||||
source $INFO
|
source $INFO
|
||||||
mv $INFO $BIGNAME-$INFO
|
mv $INFO $BIGNAME-$INFO
|
||||||
INFO=$BIGNAME-$INFO
|
INFO=dl-$BIGNAME-$INFO
|
||||||
|
|
||||||
FOLDER="dl-$BIGNAME"
|
FOLDER="dl-$BIGNAME"
|
||||||
|
|
||||||
@@ -239,6 +278,7 @@ echo
|
|||||||
|
|
||||||
checkfolder $FOLDER # Checks and creates folder
|
checkfolder $FOLDER # Checks and creates folder
|
||||||
|
|
||||||
|
# Downloading every single small file
|
||||||
until [ "$STATUS" == "F" ]; do
|
until [ "$STATUS" == "F" ]; do
|
||||||
for ((i = 0; i < ${#SMALLNAME[*]}; i++)); do
|
for ((i = 0; i < ${#SMALLNAME[*]}; i++)); do
|
||||||
SMALLNAME=${SMALLNAME[$i]}
|
SMALLNAME=${SMALLNAME[$i]}
|
||||||
@@ -256,6 +296,9 @@ until [ "$STATUS" == "F" ]; do
|
|||||||
echo "Already downloaded "$(getdu $FOLDER)" of $BIGSIZE bytes."
|
echo "Already downloaded "$(getdu $FOLDER)" of $BIGSIZE bytes."
|
||||||
echo
|
echo
|
||||||
|
|
||||||
|
# Define starttime
|
||||||
|
TSTART=$(date +"%s")
|
||||||
|
|
||||||
# File doesn't exist yet, so start fresh download
|
# File doesn't exist yet, so start fresh download
|
||||||
if [ ! -e $SMALLPATH ]; then
|
if [ ! -e $SMALLPATH ]; then
|
||||||
echo "[INFO] File doesn't exist yet. Starting new download."
|
echo "[INFO] File doesn't exist yet. Starting new download."
|
||||||
@@ -282,9 +325,9 @@ until [ "$STATUS" == "F" ]; do
|
|||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# File already exists, has correct size, but has wrong HASH
|
# File already exists, has correct size, but has wrong hashsum
|
||||||
elif [ -e $SMALLPATH ] && $(compdu $SMALLPATH SMALLSIZE) && ! $(comphash $SMALLPATH $SMALLHASH); then
|
elif [ -e $SMALLPATH ] && $(compdu $SMALLPATH SMALLSIZE) && ! $(comphash $SMALLPATH $SMALLHASH); then
|
||||||
echo "[ERROR] HASH is different but file has same size."
|
echo "[ERROR] Hashsum is different but file has same size."
|
||||||
echo "[INFO] Deleting file and starting new download."
|
echo "[INFO] Deleting file and starting new download."
|
||||||
checkwait
|
checkwait
|
||||||
rm $SMALLPATH
|
rm $SMALLPATH
|
||||||
@@ -298,7 +341,7 @@ until [ "$STATUS" == "F" ]; do
|
|||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# File already exists, has correct size, and has correct HASH
|
# File already exists, has correct size, and has correct hashsum
|
||||||
elif [ -e $SMALLPATH ] && $(compdu $SMALLPATH $SMALLSIZE) && $(comphash $SMALLPATH $SMALLHASH); then
|
elif [ -e $SMALLPATH ] && $(compdu $SMALLPATH $SMALLSIZE) && $(comphash $SMALLPATH $SMALLHASH); then
|
||||||
echo "[SUCCESS] File already exists and is valid."
|
echo "[SUCCESS] File already exists and is valid."
|
||||||
STATUS="C"
|
STATUS="C"
|
||||||
@@ -309,7 +352,19 @@ until [ "$STATUS" == "F" ]; do
|
|||||||
STATUS="E"
|
STATUS="E"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check if download is finished
|
# Define time when the download finished
|
||||||
|
echo
|
||||||
|
TEND=$(date +"%s")
|
||||||
|
TDIFF=$[$TEND-$TSTART]
|
||||||
|
if [ $TDIFF -ge "3" ]; then
|
||||||
|
# Calc bytes per second
|
||||||
|
BPS=$[$SMALLSIZE / $TDIFF]
|
||||||
|
DUDIFF=$[$BIGSIZE - $(getdu $FOLDER)]
|
||||||
|
TREST=$[$DUDIFF / $BPS]
|
||||||
|
echo "[INFO] The last file was downloaded at ~$[$BPS / 1024] KB/s. ETA $(($TREST / 60)):$(($TREST % 60))."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check if all small downloads are finished
|
||||||
if [ $NO -lt $SMALLNO ]; then
|
if [ $NO -lt $SMALLNO ]; then
|
||||||
echo "[INFO] Starting next download."
|
echo "[INFO] Starting next download."
|
||||||
STATUS="C"
|
STATUS="C"
|
||||||
@@ -341,9 +396,9 @@ if [ $YN == "y" ]; then
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check big file for HASH and size
|
# Check big file for hashsum and size
|
||||||
sleep 2 # In rare cases, this can prevent a wrong du size
|
sleep 2 # In rare cases, this can prevent a wrong du size
|
||||||
echo "[INFO] Checking correct size and HASH hashsum for rebuilt big file..."
|
echo "[INFO] Checking correct size and hashsum for rebuilt big file..."
|
||||||
|
|
||||||
# Compare sizes
|
# Compare sizes
|
||||||
if $(compdu $BIGNAME $BIGSIZE); then
|
if $(compdu $BIGNAME $BIGSIZE); then
|
||||||
@@ -352,11 +407,11 @@ else
|
|||||||
echo "[ERROR] The size of the completed file is incorrect."
|
echo "[ERROR] The size of the completed file is incorrect."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Compare HASH sum
|
# Compare hashsum
|
||||||
if $(comphash $BIGNAME $BIGHASH); then
|
if $(comphash $BIGNAME $BIGHASH); then
|
||||||
echo "[SUCCESS] The HASH sum of the completed file is corrent."
|
echo "[SUCCESS] The checksum of the completed file is corrent."
|
||||||
else
|
else
|
||||||
echo "[ERROR] The HASH sum of the completed file is incorrect."
|
echo "[ERROR] The sum of the completed file is incorrect."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user