Linux – Securely copy directories to server.

Script que copia carpetas completas desde servidor P al servidor N, utilizando FTP, comprueba la integridad de los archivos por comparación de sumas y borra cuando un simple test de integridad pasa.

Restricciones (muy específicas):

  • No se podía utilizar Rsync: no se debe modificar nada en el servidor N.
  • No se podía utilizar scp: debido a restricciones en el servidor P.
  • Servidor N tiene solo md5 (FreeBSD).
  • Servidor P tiene solo md5sum (Debian).

 

Solución:

#!/bin/bash
dir=/temp/directory/for/folder/listing
dird=/your/directory/where/folders/are/
rm -f $dir/dirs.txt
rm -f $dir/files.txt
#TODO: check if these files where removed, if not, then try until success
find $dird -mindepth 1 -maxdepth 1 -type d >> $dir/dirs.txt
find $dird -mindepth 1 -maxdepth 1 -type f >> $dir/files.txt
#l is the line number processing.
#tl stands for total lines (until the end of file)

l=1
tl=$(wc -l $dir/dirs.txt| cut -f1 -d ' ')

echo "============================================================="
echo "Directories ($tl Detected)"
echo "============================================================="

while [ $l -le $tl ]
do
line=$(head -$l $dir/dirs.txt | tail -1)

echo "============================================================="
echo "For Directory: $line $l/$tl"
echo "============================================================="
basel=$(basename "$line")

ncftpput -R -v -u "username" -p "password" 192.168.6.6 /where/to/transfer/ "$line"

#fresh variables up
int="1"
intd="2"
ext="3"
extd="4"

int=$(find "$line" -type f | sort -k1 -V | xargs -I '{}' md5sum "{}" | cut -f1 -d ' ' | xargs echo -n | md5sum | cut -f1 -d ' ' )
#intd=$(find "$line" -type f | sort -k1 -V | xargs -I '{}' md5sum "{}" | cut -f1 -d ' ' )
ext=$(ssh user@192.168.6.6 "find \"/remote/folder/where/to/put/$basel\" -type f | sort -k1 | xargs -I '{}' md5 "{}" | cut -f2 -d '=' | xargs echo -n | md5 | cut -f2 -d '=' "| xargs echo -n)
#extd=$(ssh user@192.168.6.6 "find \"/remote/folder/where/to/put/$basel\" -type f | sort -k1 | xargs -I '{}' md5 "{}" | cut -f2 -d '=' | xargs echo -n ")
echo int:$int
echo ext:$ext
#echo intd:$intd
#echo extd:$extd
echo "/////////////////////////////////////////////////////////////"
if [ "$int" == "$ext" ]
then
echo iguales, procediendo a borrar archivo local -- $line --, recursivamente.
datenow=$(date)
echo -n "$datenow - " >> $dir/deleted.txt
echo $line >> $dir/deleted.txt
echo int:$int >> $dir/deleted.txt
echo ext:$ext >> $dir/deleted.txt
#echo intd:$intd >> $dir/deleted.txt
#echo extd:$extd >> $dir/deleted.txt
rm -Rf "$line"
else
echo Not equal, so i dont do anything
#TODO: SEND alarm, mail or something
fi
l=$(($l+1))
done
closed