SFTP Skript mit Bash

#!/bin/bash
#
# Encryption and SFTP Script 
# (c) 2017 Steffen Berg
#
# Es wird eine Verschluesselungssoftware benoetigt.
# Diese muss unter /$path/bin liegen.
#
# Es wird die Software sshpass benoetigt.
# Diese muss im Path vorhanden sein, sodass sie ueberall aufgerufen werden kann.
# Hierbei sollte mit einem credentials-File gearbeitet werden.
# Owner muss root sein und Rechte muessen 600 sein.
#
# Fuer das optionale Mounten muss ein entsprechender fstab-Eintrag vorhanden sein.
# Am besten hier auch mit credentials-File arbeiten.
# Owner auch root und Rechte auch 600.
#
# Ein sftp Client muss im Pfad stehen und von ueberall aufrufbar sein.
# Funktion mit Standard-SFTP-Client eines Ubuntu System getestet.
#Some Vars
lasttry=$(date)
path="/homedir"
work="$path/work"
encrypted="$path/encrypted"
srcserver="fileserver"
srcdir="quellverzeichnis"
filelist=`ls -1 /$srcserver/$srcdir/muster*.txt`
keyfile="$path/bin/encryptionkey"
binenc="$path/bin/encryption-binary"
credfile="/etc/sshpass.cred"
sftpserver="server oder ip"
username="sftp user name"
done="done"
logfile="encryption-history.log"
remotestatefile="remotestate.stat"
# Letzten Zeitstempel des Skriptaufrufes speichern
echo "------------------------------------- Start -----------------------------------------" >> $path/$logfile
echo "$lasttry Starting sftp Skript" >> $path/$logfile
 
# Checking if already files exist on remote server
echo 'ls -1 /ziel-unterverzeichnis' | sshpass -f /etc/sshpass.cred sftp $username@$sftpserver >; $path/$remotestatefile
if grep -v "^sftp>" $path/$remotestatefile
then
 echo "$(date) Files already exist on remote fir" >> $path/$logfile
 echo "-------- End ERROR FILE ON REMOTE EXIST ----------"
 exit 1
else
 echo "$(date) No files detected in remote dir going on" >> $path/$logfile
fi
# Mounting CIFS SRC Server
if mount /$srcserver 2>&1 >> $path/$logfile
then
 echo "$(date) Mount successful" >> $path/$logfile
else
 echo "$(date) Mount failed" >> $path/$logfile
 echo "-------- End ERROR WHILE MOUNT ---------" >> $path/$logfile
 exit 1
fi
# Dateien aus der Quelle nach work kopieren
if cp -rpv $filelist $work 2>&1 >> $path/$logfile
then
 echo "$(date) Copy successful" >> $path/$logfile
else
 echo "$(date) Copy failed" >> $path/$logfile
 echo "-------- End ERROR WHILE COPY ---------" >> $path/$logfile
 umount /$srcerver 2>&1 >> $path/$logfile
 exit 1
fi
# Dateien verschluesseln und in encrypted ablegen
filelistenc=`ls -1 $work/muster*.txt | xargs -n 1 basename`
for filelistelem in ${filelistenc[@]}; do
 if $binenc -m 1 -q $work/$filelistelem -z $encrypted/$filelistelem.enc -s $keyfile 2>> $path/$logfile
 then
 echo "$(date) Encryption successful" >> $path/$logfile
 else
 echo "$(date) Encryption failed" >> $path/$logfile
 echo "-------- End ERROR WHILE ENCR ---------" >> $path/$logfile
 umount /srfrafile 2>&1 >> $path/$logfile
 exit 1
 fi
done
# SFTP Aktionen
filelistftp=`ls -1 $encrypted/fakt*.txt.enc`
for filelistelemftp in ${filelistftp[@]}; do
if sshpass -f $credfile sftp $username@$sftpserver 2>;&1 >> $path/$logfile << HEREDOCDELIM
 cd klinik_niederrhein
 put $filelistelemftp
 quit
HEREDOCDELIM
then
 echo "$(date) SFTP transfer successful" >> $path/$logfile
else
 echo "$(date) SFTP transfer failed" >> $path/$logfile
 echo "-------- End ERROR WHILE SFTP ---------" >> $path/$logfile
 umount /$srcserver 2>&1 >> $path/$logfile
 exit 1
fi
done
#Cleaning step: Moving enc files to done
filelistmv1=`ls -1 $encrypted/muster*.txt.enc | xargs -n 1 basename`
for filelistelemmv1 in ${filelistmv1[@]}; do
 if mv $encrypted/$filelistelemmv1 $encrypted/$done 2>&1 >> $path/$logfile
 then
 echo "$(date) Moving enc Files to donedir successful" >> $path/$logfile
 else
 echo "$(date) Moving enc Files to donedir failed" >> $path/$logfile
 echo "-------- End ERROR WHILE MOVING ENC ---------" >> $path/$logfile
 umount /$srcserver 2>&1 >> $path/$logfile
 exit 1
 fi
done
#Cleaning step: Moving work files to done
filelistmv2=`ls -1 $work/muster*.txt | xargs -n 1 basename`
for filelistelemmv2 in ${filelistmv2[@]}; do
 if mv $work/$filelistelemmv2 $work/$done 2>&1 >> $path/$logfile
 then
 echo "$(date) Moving work Files to donedir successful" >> $path/$logfile
 else
 echo "$(date) Moving work Files to donedir failed" >> $path/$logfile
 echo "-------- End ERROR WHILE MOVING WORK ---------" >> $path/$logfile
 umount /$srcserver 2>&1 >> $path/$logfile
 exit 1
 fi
done
#Cleaning step: Moving src files to done
filelistmv3=`ls -1 /$srcserver/$srcdir/muster*.txt | xargs -n 1 basename`
for filelistelemmv3 in ${filelistmv3[@]}; do
 if mv /$srcserver/$srcdir/$filelistelemmv3 /$srcserver/$srcdir/$done 2>&1 >> $path/$logfile
 then
 echo "$(date) Moving src Files to donedir successful" >> $path/$logfile
 else
 echo "$(date) Moving src Files to donedir failed" >> $path/$logfile
 echo "-------- End ERROR WHILE MOVING SRC ---------" >> $path/$logfile
 umount /$srcserver 2>&1 >> $path/$logfile
 exit 1
 fi
done
#Cleaning step: Moving src semaphore files to done
filelistmv4=`ls -1 /$srcserver/$srcdir/muster*.sem | xargs -n 1 basename`
for filelistelemmv4 in ${filelistmv4[@]}; do
 if mv /$srcserver/$srcdir/$filelistelemmv4 /$srcserver/$srcdir/$done 2>&1 >> $path/$logfile
 then
 echo "$(date) Moving src ebs Files to donedir successful" >> $path/$logfile
 else
 echo "$(date) Moving src ebs Files to donedir failed" >> $path/$logfile
 echo "-------- End ERROR WHILE MOVING SRC EBS ---------" >> $path/$logfile
 umount /$srcserver 2>&1 >> $path/$logfile
 exit 1
 fi
done
# Unmounting CIFS SRC Server
if umount /$srcserver2>&1 >> $path/$logfile
then
 echo "$(date) Unmount successful" >> $path/$logfile
else
 echo "$(date) Unmount failed" >> $path/$logfile
 echo "-------- End ERROR WHILE UNMOUNT ---------" >> $path/$logfile
 exit 1
fi
echo "--------------------------- End WHOLE STEPS PASSED SUCCESSFUL -------------------------" >> $path/$logfile
exit 0

Schreibe einen Kommentar