Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull-Request to Issue #3 #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .msmtprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Default settings for msmtp
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile ~/.msmtp.log

# Account settings
account monitor-netstack
host maildb01.netstack.de
port 587
from [email protected]
user [email protected]
password xxx

# Set a default account
account default : monitor-netstack
298 changes: 133 additions & 165 deletions backupfiles_to_nfs.sh
Original file line number Diff line number Diff line change
@@ -1,170 +1,138 @@
#!/bin/bash
#
# BackupNFS Tool 0.2
#
# Programm von Netstack GmbH written by Andreas Pfeiffer
#
# Dieses Programm führt Datei Backups zu einem NFS Share durch
#
################# CONFIG #####################
# BackupNFS Tool 0.3 - NFS-Sicherung
# Programm von Netstack GmbH

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
datenbank=(dbname dbnam2) # Datenbanken welche gesichert werden sollen
date1=$(date +%a) # Datumsformat welches an die Dateien angehangen wird
hostname=$(hostname -f) # Hostname
dbuser=DBUSER # Datenbank User
dbpass=DBPASS # Datenbank Passwort
mailsubject="Backup - $hostname" # Mail Betreff
mailtext=/tmp/mailtext.txt # Mailtext zum Versand
[email protected] # Empfaenger der Status Mails
date2=$(date +%A" "%d.%m.%Y) # Datum für E-Mail
nfsmount="192.168.0.1:/backupshare" # NFS server + mount mount
nfsmountdir=/path/to/backup/ # Ordner fuer NFS-Share

################# CONFIG END #################

#reset Error Variable
error="0";

#Clear mailtext

echo " " > $mailtext;

#Aktuelles Datum
echo "#############################################################" >> $mailtext
echo "Backup Script from $hostname - "$date2 >> $mailtext
echo "#############################################################" >> $mailtext
echo "" >> $mailtext

# Check Backupdir
if test -d $nfsmountdir
then
echo "";
else
echo -n "Create Backupdirectory ... " >> $mailtext
mkdir -p $nfsmountdir
if test -d $nfsmountdir
then
echo -n "done" >> $mailtext
echo "\n" >> $mailtext
################# CONFIG #####################
nfsmount="192.168.0.1:/backupshare"
nfsmountdir="/data/nfs_backup"
hostname="$(hostname -f)"
backupdir="$nfsmountdir/$hostname"
datadir=(/path/to/files /path/to/files/2)
datenbank=(dbname dbname2)
date1="$(date +%a)"
date2="$(date +%A %d.%m.%Y)"
dbuser="DBUSER"
dbpass="DBPASS"
email="[email protected]"
mailsubject="Backup - $hostname"
mailtext="/tmp/mailtext.txt"

################# FUNCTIONS #####################

log_message() {
echo -e "$1" | tee -a "$mailtext"
}

send_mail() {
(
echo "To: $email"
echo "Subject: $mailsubject"
echo "Content-Type: text/plain"
echo
cat $mailtext
) | msmtp $email
}

check_and_install_package() {
local pkg="$1"
if ! dpkg -l | grep -q "$pkg"; then
log_message "Installing $pkg..."
sudo apt-get update && sudo apt-get install -y "$pkg"
if [ $? -ne 0 ]; then
log_message "Failed to install $pkg. Exiting..."
send_mail
exit 1
fi
fi
}

create_directory() {
local dir="$1"
if [ ! -d "$dir" ]; then
mkdir -p "$dir"
if [ $? -ne 0 ]; then
log_message "Error creating directory: $dir"
send_mail
exit 1
fi
fi
}

mount_nfs() {
mount | grep -q "$nfsmountdir"
if [ $? -ne 0 ]; then
log_message "Mounting NFS share $nfsmount to $nfsmountdir..."
mount "$nfsmount" "$nfsmountdir"
if [ $? -ne 0 ]; then
log_message "Error mounting NFS share. Exiting..."
send_mail
exit 1
fi
fi
}

backup_files() {
for dir in "${datadir[@]}"; do
if [ -d "$dir" ]; then
local sanitized_dir="${dir%/}" # Entfernt abschließenden Slash
local name="$(basename "$sanitized_dir")" # Extrahiert den Ordnernamen
if [ -z "$name" ]; then
log_message "Error: Unable to extract directory name for path $dir."
send_mail
exit 1
fi
log_message "Backing up directory $dir to $backupdir/files/${name}_$date1.tar.gz..."
tar -Pczf "$backupdir/files/${name}_$date1.tar.gz" "$dir" --ignore-failed-read --warning=no-file-changed
if [ $? -ne 0 ]; then
log_message "Error backing up $dir. Exiting..."
send_mail
exit 1
fi
else
echo -n "Error - Cant create nfs directory" >> $mailtext
# send mail on Error
cat $mailtext | mail -s "$mailsubject" $email
exit 1;
log_message "Error: Directory $dir does not exist. Skipping..."
fi
done
}


backup_mysql() {
for db in "${datenbank[@]}"; do
local dump_file="$backupdir/mysql/${db}_$date1.sql"
log_message "Creating MySQL dump for database $db..."
mysqldump -u"$dbuser" -p"$dbpass" --opt "$db" > "$dump_file"
if [ $? -ne 0 ]; then
log_message "Error creating MySQL dump for $db. Exiting..."
send_mail
exit 1
fi
log_message "Compressing dump file $dump_file..."
gzip -f -9 "$dump_file"
if [ $? -ne 0 ]; then
log_message "Error compressing $dump_file. Exiting..."
send_mail
exit 1
fi
done
}

################# MAIN SCRIPT #####################

>"$mailtext" # Clear mail text
log_message "Starting backup script for $hostname on $date2"

check_and_install_package "nfs-common"
check_and_install_package "msmtp"

create_directory "$nfsmountdir"
mount_nfs
create_directory "$backupdir"
create_directory "$backupdir/mysql"
create_directory "$backupdir/files"

backup_files
backup_mysql

fi

#mount NFS to backupdir
echo -n "mount nfs share $nfsmount ... " >> $mailtext;
mount $nfsmount $nfsmountdir
if [ $? == "0" ]
then
echo -n " done" >> $mailtext
echo "\n" >> $mailtext
else
echo -n " error" >> $mailtext
echo "\n" >> $mailtext
# send mail on Error
cat $mailtext | mail -s "$mailsubject" $email
exit 1;
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" >> $mailtext
else
echo -n "Error - Cant create backupdirectory" >> $mailtext
# send mail on Error
cat $mailtext | mail -s "$mailsubject" $email
exit 1;
fi

fi

# Check Backupdir for mysql
if test -d $backupdir/mysql
then
echo "";
else
echo -n "Create Backupdirectory for mysql ... " >> $mailtext
mkdir $backupdir/mysql
if test -d $backupdir/mysql
then
echo -n "done" >> $mailtext
echo "\n" >> $mailtext
else
echo -n "Error - Cant create backupdirectory for mysql" >> $mailtext
# send mail on Error
cat $mailtext | mail -s "$mailsubject" $email
exit 1;
fi

fi

# Check Backupdir for files
if test -d $backupdir/files
then
echo "";
else
echo -n "Create Backupdirectory for files ... " >> $mailtext
mkdir -p $backupdir/files
if test -d $backupdir/files
then
echo -n "done" >> $mailtext
echo "\n" >> $mailtext
else
echo -n "Error - Cant create backupdirectory for files" >> $mailtext
# send mail on Error
cat $mailtext | mail -s "$mailsubject" $email
exit 1;
fi

fi

######################
# Start Files backup #
######################

#count datadir's from config
countdir=${#datadir[*]};

for (( j=0; j<$countdir; j++ ))
do
filesdir=${datadir[$j]}
#echo "";
#make backup from files dir
name=$(basename $filesdir)
echo -n "make backup from $filesdir ..." >> $mailtext
tar -Pczf $backupdir/files/$name'_'$date1.tar.gz $filesdir
if [ $? == "0" ]
then
echo -n " done" >> $mailtext
echo "\n" >> $mailtext
else
echo -n " error" >> $mailtext
echo "\n" >> $mailtext
# send mail on Error
cat $mailtext | mail -s "$mailsubject" $email
exit 1;
fi
done

#umount NFS share
umount $nfsmountdir

# Mailversand

# cat $mailtext | mail -s "$mailsubject" $email

exit 0;
umount "$nfsmountdir"
log_message "Backup completed successfully."
send_mail
exit 0
Loading