md5_cp/cp_sh

59 lines
1.4 KiB
Bash
Executable File

#!/bin/bash
# @autor 6543@transport.de
# @date 2019-01-09
# @version 0.2
function free_disk() {
df "$1" | tail -n1 | sed -r -e "s/[\t\ ]+/ /g" | cut -d ' ' -f 4
}
md5="2cp"
aim="/mnt/export/transport"
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_dir=$(echo $file_orig | rev | cut -d '/' -f 2- | rev | sed 's/data\/Filme/mnt\/export\/transport/g')
file_aim=$(echo $file_orig | sed 's/data\/Filme/mnt\/export\/transport/g')
[ "$file_orig" == "$file_aim" ] && { echo Script ERROR; exit 255; }
freenow=$(free_disk "$aim")
#add puffer
freenow=$((freenow-10240))
if [ -f "$file_orig" ]; then
if [ "$freenow" -ge "$file_size" ]; then
mkdir -p "$file_aim_dir"
[ -f "$file_aim" ] && rm "$file_aim"
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" >> /mnt/export/md5.txt
sed -i "/$file_md5/d" "$md5"
else
echo "Error: Hash mismatch: \"NEW:${file_md5_new} OLD:${file_md5}\" $file_aim"
sed -i "/$file_md5/d" "$md5"
fi
else
echo "To Big: $file_md5 $file_orig"
fi
else
echo "Error: Not Exist: $file_orig"
sed -i "/$file_md5/d" "$md5"
fi
done
rm "$md5_tmp"