add nfs-check and code tidied up

This commit is contained in:
2024-06-25 09:18:14 +02:00
parent 66ffcb7054
commit a2efadf647

View File

@@ -24,147 +24,133 @@ nfsmountdir=/path/to/backup/ # Ordner fuer NFS-Share
################# CONFIG END ################# ################# CONFIG END #################
#reset Error Variable # Function to check if NFS packages are installed
error="0"; check_nfs_packages() {
if ! dpkg -l | grep -q nfs-common; then
echo "NFS packages not installed. Installing..."
sudo apt-get update
sudo apt-get install -y nfs-common
if [ $? -ne 0 ]; then
echo "Failed to install NFS packages. Exiting..."
exit 1
fi
fi
}
# Run the NFS package check function
check_nfs_packages
# Reset Error Variable
error="0"
# Clear mailtext # Clear mailtext
echo " " > $mailtext
echo " " > $mailtext;
# Aktuelles Datum # Aktuelles Datum
echo "#############################################################" >> $mailtext echo "#############################################################" >> $mailtext
echo "Backup Script from $hostname - "$date2 >> $mailtext echo "Backup Script from $hostname - $date2" >> $mailtext
echo "#############################################################" >> $mailtext echo "#############################################################" >> $mailtext
echo "" >> $mailtext echo "" >> $mailtext
# Check Backupdir # Check Backupdir
if test -d $nfsmountdir if [ ! -d $nfsmountdir ]; then
then
echo "";
else
echo -n "Create Backupdirectory ... " >> $mailtext echo -n "Create Backupdirectory ... " >> $mailtext
mkdir -p $nfsmountdir mkdir -p $nfsmountdir
if test -d $nfsmountdir if [ ! -d $nfsmountdir ]; then
then
echo -n "done" >> $mailtext
echo "\n" >> $mailtext
else
echo -n "Error - Cant create nfs directory" >> $mailtext echo -n "Error - Cant create nfs directory" >> $mailtext
# send mail on Error # send mail on Error
cat $mailtext | mail -s "$mailsubject" $email cat $mailtext | mail -s "$mailsubject" $email
exit 1; exit 1
fi fi
fi
#mount NFS to backupdir
echo -n "mount nfs share $nfsmount ... " >> $mailtext;
mount $nfsmount $nfsmountdir
if [ $? == "0" ]
then
echo -n "done" >> $mailtext echo -n "done" >> $mailtext
echo "\n" >> $mailtext echo "\n" >> $mailtext
else fi
# Mount NFS to backupdir
echo -n "mount nfs share $nfsmount ... " >> $mailtext
mount $nfsmount $nfsmountdir
if [ $? -ne 0 ]; then
echo -n " error" >> $mailtext echo -n " error" >> $mailtext
echo "\n" >> $mailtext echo "\n" >> $mailtext
# send mail on Error # send mail on Error
cat $mailtext | mail -s "$mailsubject" $email cat $mailtext | mail -s "$mailsubject" $email
exit 1; exit 1
fi fi
# Check Backupdir
if test -d $backupdir
then
echo "";
else
echo -n "Create Backupdirectory ... " >> $mailtext
mkdir -p $backupdir
if test -d $backupdir
then
echo -n " done" >> $mailtext echo -n " done" >> $mailtext
echo "\n" >> $mailtext echo "\n" >> $mailtext
else
# Check Backupdir
if [ ! -d $backupdir ]; then
echo -n "Create Backupdirectory ... " >> $mailtext
mkdir -p $backupdir
if [ ! -d $backupdir ]; then
echo -n "Error - Cant create backupdirectory" >> $mailtext echo -n "Error - Cant create backupdirectory" >> $mailtext
# send mail on Error # send mail on Error
cat $mailtext | mail -s "$mailsubject" $email cat $mailtext | mail -s "$mailsubject" $email
exit 1; exit 1
fi fi
echo -n "done" >> $mailtext
echo "\n" >> $mailtext
fi fi
# Check Backupdir for mysql # Check Backupdir for mysql
if test -d $backupdir/mysql if [ ! -d $backupdir/mysql ]; then
then
echo "";
else
echo -n "Create Backupdirectory for mysql ... " >> $mailtext echo -n "Create Backupdirectory for mysql ... " >> $mailtext
mkdir $backupdir/mysql mkdir -p $backupdir/mysql
if test -d $backupdir/mysql if [ ! -d $backupdir/mysql ]; then
then
echo -n "done" >> $mailtext
echo "\n" >> $mailtext
else
echo -n "Error - Cant create backupdirectory for mysql" >> $mailtext echo -n "Error - Cant create backupdirectory for mysql" >> $mailtext
# send mail on Error # send mail on Error
cat $mailtext | mail -s "$mailsubject" $email cat $mailtext | mail -s "$mailsubject" $email
exit 1; exit 1
fi fi
echo -n "done" >> $mailtext
echo "\n" >> $mailtext
fi fi
# Check Backupdir for files # Check Backupdir for files
if test -d $backupdir/files if [ ! -d $backupdir/files ]; then
then
echo "";
else
echo -n "Create Backupdirectory for files ... " >> $mailtext echo -n "Create Backupdirectory for files ... " >> $mailtext
mkdir -p $backupdir/files mkdir -p $backupdir/files
if test -d $backupdir/files if [ ! -d $backupdir/files ]; then
then
echo -n "done" >> $mailtext
echo "\n" >> $mailtext
else
echo -n "Error - Cant create backupdirectory for files" >> $mailtext echo -n "Error - Cant create backupdirectory for files" >> $mailtext
# send mail on Error # send mail on Error
cat $mailtext | mail -s "$mailsubject" $email cat $mailtext | mail -s "$mailsubject" $email
exit 1; exit 1
fi fi
echo -n "done" >> $mailtext
echo "\n" >> $mailtext
fi fi
###################### ######################
# Start Files backup # # Start Files backup #
###################### ######################
#count datadir's from config # Count datadir's from config
countdir=${#datadir[*]}; countdir=${#datadir[*]}
for (( j=0; j<$countdir; j++ )) for (( j=0; j<$countdir; j++ ))
do do
filesdir=${datadir[$j]} filesdir=${datadir[$j]}
# echo ""; # echo "";
#make backup from files dir # Make backup from files dir
name=$(basename $filesdir) name=$(basename $filesdir)
echo -n "make backup from $filesdir ..." >> $mailtext echo -n "make backup from $filesdir ..." >> $mailtext
tar -Pczf $backupdir/files/$name'_'$date1.tar.gz $filesdir tar -Pczf $backupdir/files/${name}_$date1.tar.gz $filesdir
if [ $? == "0" ] if [ $? -ne 0 ]; then
then
echo -n " done" >> $mailtext
echo "\n" >> $mailtext
else
echo -n " error" >> $mailtext echo -n " error" >> $mailtext
echo "\n" >> $mailtext echo "\n" >> $mailtext
# send mail on Error # send mail on Error
cat $mailtext | mail -s "$mailsubject" $email cat $mailtext | mail -s "$mailsubject" $email
exit 1; exit 1
fi fi
echo -n " done" >> $mailtext
echo "\n" >> $mailtext
done done
#umount NFS share # Umount NFS share
umount $nfsmountdir umount $nfsmountdir
# Mailversand # Mailversand
# cat $mailtext | mail -s "$mailsubject" $email # cat $mailtext | mail -s "$mailsubject" $email
exit 0; exit 0