TerabyteTerminator/tools/manage_md5-lists/diff_cp
2023-02-27 18:49:21 +01:00

65 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
# @autor 6543@obermui.de
# @date 2023-02-27
# @version 0.3
function free() {
df "$1" | grep '/dev/' | sed -r -e "s/[\t\ ]+/ /g" | cut -d ' ' -f 4
}
md5="2cp"
aim="/data/Filme"
md5_success="cp.done.md5"
md5_conflict="cp.conflict.md5"
md5_tmp=`mktemp`
freenow=0
cat "$md5" > "$md5_tmp"
cat "$md5_tmp" | while read line; do
file_orig="$(echo $line | cut -c 33- | sed -r -e "s/^[\ ]//g")"
file_size=$(du "$file_orig" | cut -f 1)
file_md5=$(echo $line | cut -c -32)
file_aim=$(echo $file_orig | sed 's/data\/shares\/Filme/tmp\/sdl\/oberoed/g')
file_aim_dir=$(echo $file_aim | rev | cut -d '/' -f 2- | rev)
[ "$file_orig" == "$file_aim" ] && { echo Script ERROR; exit 255; }
freenow=$(free "$aim")
#add puffer
freenow=$((freenow-10240))
if [ -f "$file_orig" ]; then
if [ "$freenow" -ge "$file_size" ]; then
mkdir -p "$file_aim_dir"
if [ -f "${file_aim}" ]; then
echo "${file_md5} ${file_aim}" >> ${md5_conflict}
sed -i "/$file_md5/d" "$md5"
echo "ERROR: ${file_aim} already exist, skipping"
else
file_md5_new=$(cat "${file_orig}" | tee "${file_aim}" | md5sum | cut -c -32)
if [ "${file_md5_new}" == "${file_md5}" ]; then
echo "${file_md5} ${file_aim}" >> $md5_success
sed -i "/$file_md5/d" "$md5"
else
echo "ERROR: Hash mismatch: \"NEW:${file_md5_new} OLD:${file_md5}\" $file_aim"
rm "${file_aim}"
fi
fi
else
echo "WARNING: To Big: ${file_orig} (${file_size}), skipping"
fi
else
echo "ERROR: Not Exist: ${file_orig}"
sed -i "/$file_md5/d" "$md5"
fi
done
rm "${md5_tmp}"