mirror of
https://github.com/MrUnknownDE/netstack-backuptools-bash.git
synced 2026-04-23 00:33:52 +02:00
add nfs-check and code tidied up
This commit is contained in:
@@ -8,163 +8,149 @@
|
|||||||
#
|
#
|
||||||
################# CONFIG #####################
|
################# CONFIG #####################
|
||||||
|
|
||||||
backupdir="$nfsmountdir/$hostname" # Verzeichniss wo die Backups abgelegt werden, es werden automatisch unterordner "mysql" und "files" erstellt.
|
backupdir="$nfsmountdir/$hostname" # Verzeichniss wo die Backups abgelegt werden, es werden automatisch unterordner "mysql" und "files" erstellt.
|
||||||
datadir=(/path/to/files /path/to/files/2 ) # Welche Ordner sollen gesichert werden
|
datadir=(/path/to/files /path/to/files/2 ) # Welche Ordner sollen gesichert werden
|
||||||
datenbank=(dbname dbnam2) # Datenbanken welche gesichert werden sollen
|
datenbank=(dbname dbnam2) # Datenbanken welche gesichert werden sollen
|
||||||
date1=$(date +%a) # Datumsformat welches an die Dateien angehangen wird
|
date1=$(date +%a) # Datumsformat welches an die Dateien angehangen wird
|
||||||
hostname=$(hostname -f) # Hostname
|
hostname=$(hostname -f) # Hostname
|
||||||
dbuser=DBUSER # Datenbank User
|
dbuser=DBUSER # Datenbank User
|
||||||
dbpass=DBPASS # Datenbank Passwort
|
dbpass=DBPASS # Datenbank Passwort
|
||||||
mailsubject="Backup - $hostname" # Mail Betreff
|
mailsubject="Backup - $hostname" # Mail Betreff
|
||||||
mailtext=/tmp/mailtext.txt # Mailtext zum Versand
|
mailtext=/tmp/mailtext.txt # Mailtext zum Versand
|
||||||
email=monitor@example.com # Empfaenger der Status Mails
|
email=monitor@example.com # Empfaenger der Status Mails
|
||||||
date2=$(date +%A" "%d.%m.%Y) # Datum für E-Mail
|
date2=$(date +%A" "%d.%m.%Y) # Datum für E-Mail
|
||||||
nfsmount="192.168.0.1:/backupshare" # NFS server + mount mount
|
nfsmount="192.168.0.1:/backupshare" # NFS server + mount mount
|
||||||
nfsmountdir=/path/to/backup/ # Ordner fuer NFS-Share
|
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
|
||||||
|
}
|
||||||
|
|
||||||
#Clear mailtext
|
# Run the NFS package check function
|
||||||
|
check_nfs_packages
|
||||||
|
|
||||||
echo " " > $mailtext;
|
# Reset Error Variable
|
||||||
|
error="0"
|
||||||
|
|
||||||
#Aktuelles Datum
|
# Clear mailtext
|
||||||
|
echo " " > $mailtext
|
||||||
|
|
||||||
|
# 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 -n "Create Backupdirectory ... " >> $mailtext
|
||||||
echo "";
|
mkdir -p $nfsmountdir
|
||||||
else
|
if [ ! -d $nfsmountdir ]; then
|
||||||
echo -n "Create Backupdirectory ... " >> $mailtext
|
echo -n "Error - Cant create nfs directory" >> $mailtext
|
||||||
mkdir -p $nfsmountdir
|
# send mail on Error
|
||||||
if test -d $nfsmountdir
|
cat $mailtext | mail -s "$mailsubject" $email
|
||||||
then
|
exit 1
|
||||||
echo -n "done" >> $mailtext
|
fi
|
||||||
echo "\n" >> $mailtext
|
echo -n "done" >> $mailtext
|
||||||
else
|
echo "\n" >> $mailtext
|
||||||
echo -n "Error - Cant create nfs directory" >> $mailtext
|
|
||||||
# send mail on Error
|
|
||||||
cat $mailtext | mail -s "$mailsubject" $email
|
|
||||||
exit 1;
|
|
||||||
fi
|
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#mount NFS to backupdir
|
# Mount NFS to backupdir
|
||||||
echo -n "mount nfs share $nfsmount ... " >> $mailtext;
|
echo -n "mount nfs share $nfsmount ... " >> $mailtext
|
||||||
mount $nfsmount $nfsmountdir
|
mount $nfsmount $nfsmountdir
|
||||||
if [ $? == "0" ]
|
if [ $? -ne 0 ]; then
|
||||||
then
|
echo -n " error" >> $mailtext
|
||||||
echo -n " done" >> $mailtext
|
echo "\n" >> $mailtext
|
||||||
echo "\n" >> $mailtext
|
# send mail on Error
|
||||||
else
|
cat $mailtext | mail -s "$mailsubject" $email
|
||||||
echo -n " error" >> $mailtext
|
exit 1
|
||||||
echo "\n" >> $mailtext
|
|
||||||
# send mail on Error
|
|
||||||
cat $mailtext | mail -s "$mailsubject" $email
|
|
||||||
exit 1;
|
|
||||||
fi
|
fi
|
||||||
|
echo -n " done" >> $mailtext
|
||||||
|
echo "\n" >> $mailtext
|
||||||
|
|
||||||
# Check Backupdir
|
# Check Backupdir
|
||||||
if test -d $backupdir
|
if [ ! -d $backupdir ]; then
|
||||||
then
|
echo -n "Create Backupdirectory ... " >> $mailtext
|
||||||
echo "";
|
mkdir -p $backupdir
|
||||||
else
|
if [ ! -d $backupdir ]; then
|
||||||
echo -n "Create Backupdirectory ... " >> $mailtext
|
echo -n "Error - Cant create backupdirectory" >> $mailtext
|
||||||
mkdir -p $backupdir
|
# send mail on Error
|
||||||
if test -d $backupdir
|
cat $mailtext | mail -s "$mailsubject" $email
|
||||||
then
|
exit 1
|
||||||
echo -n "done" >> $mailtext
|
fi
|
||||||
echo "\n" >> $mailtext
|
echo -n "done" >> $mailtext
|
||||||
else
|
echo "\n" >> $mailtext
|
||||||
echo -n "Error - Cant create backupdirectory" >> $mailtext
|
|
||||||
# send mail on Error
|
|
||||||
cat $mailtext | mail -s "$mailsubject" $email
|
|
||||||
exit 1;
|
|
||||||
fi
|
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check Backupdir for mysql
|
# Check Backupdir for mysql
|
||||||
if test -d $backupdir/mysql
|
if [ ! -d $backupdir/mysql ]; then
|
||||||
then
|
echo -n "Create Backupdirectory for mysql ... " >> $mailtext
|
||||||
echo "";
|
mkdir -p $backupdir/mysql
|
||||||
else
|
if [ ! -d $backupdir/mysql ]; then
|
||||||
echo -n "Create Backupdirectory for mysql ... " >> $mailtext
|
echo -n "Error - Cant create backupdirectory for mysql" >> $mailtext
|
||||||
mkdir $backupdir/mysql
|
# send mail on Error
|
||||||
if test -d $backupdir/mysql
|
cat $mailtext | mail -s "$mailsubject" $email
|
||||||
then
|
exit 1
|
||||||
echo -n "done" >> $mailtext
|
fi
|
||||||
echo "\n" >> $mailtext
|
echo -n "done" >> $mailtext
|
||||||
else
|
echo "\n" >> $mailtext
|
||||||
echo -n "Error - Cant create backupdirectory for mysql" >> $mailtext
|
|
||||||
# send mail on Error
|
|
||||||
cat $mailtext | mail -s "$mailsubject" $email
|
|
||||||
exit 1;
|
|
||||||
fi
|
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check Backupdir for files
|
# Check Backupdir for files
|
||||||
if test -d $backupdir/files
|
if [ ! -d $backupdir/files ]; then
|
||||||
then
|
echo -n "Create Backupdirectory for files ... " >> $mailtext
|
||||||
echo "";
|
mkdir -p $backupdir/files
|
||||||
else
|
if [ ! -d $backupdir/files ]; then
|
||||||
echo -n "Create Backupdirectory for files ... " >> $mailtext
|
echo -n "Error - Cant create backupdirectory for files" >> $mailtext
|
||||||
mkdir -p $backupdir/files
|
# send mail on Error
|
||||||
if test -d $backupdir/files
|
cat $mailtext | mail -s "$mailsubject" $email
|
||||||
then
|
exit 1
|
||||||
echo -n "done" >> $mailtext
|
fi
|
||||||
echo "\n" >> $mailtext
|
echo -n "done" >> $mailtext
|
||||||
else
|
echo "\n" >> $mailtext
|
||||||
echo -n "Error - Cant create backupdirectory for files" >> $mailtext
|
|
||||||
# send mail on Error
|
|
||||||
cat $mailtext | mail -s "$mailsubject" $email
|
|
||||||
exit 1;
|
|
||||||
fi
|
|
||||||
|
|
||||||
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 " error" >> $mailtext
|
||||||
echo -n " done" >> $mailtext
|
echo "\n" >> $mailtext
|
||||||
echo "\n" >> $mailtext
|
# send mail on Error
|
||||||
else
|
cat $mailtext | mail -s "$mailsubject" $email
|
||||||
echo -n " error" >> $mailtext
|
exit 1
|
||||||
echo "\n" >> $mailtext
|
fi
|
||||||
# send mail on Error
|
echo -n " done" >> $mailtext
|
||||||
cat $mailtext | mail -s "$mailsubject" $email
|
echo "\n" >> $mailtext
|
||||||
exit 1;
|
|
||||||
fi
|
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user