#!/bin/bash
# Settings
save_path="/mnt/2tb/media/backup/manual/"
win_share_server="192.168.50.3"
win_share_path="DocumentRoot"
win_share_user="root"
win_share_password="meinpassword"
win_share_target_dir="media/backup/manual/"

local_mountpoint="/mnt/2tb"

info_email_adr="meineemail"

# End Settings


filename="chickenpi_sdcard_backup_"`date +%Y-%m-%d_%H-%M.img`
printf "\n~~~+++~~~\nStarte Backup: "$filename"\n"

if cat /proc/mounts | grep $local_mountpoint > /dev/null; then
echo "Windows-Share bereits gemountet"
else
echo "Windows-Share nicht gemountet, mounte"
#sudo mount -t cifs -o user=$win_share_user,password=$win_share_password //$win_share_server/$win_share_path $local_mountpoint
sudo mount -t cifs -o user="$win_share_user",password="$win_share_password",vers=2.1 //$win_share_server/$win_share_path $local_mountpoint
fi

# Nochmals pruefen ob das Windows-Share gemountet ist, wenn ja, backup starten
if cat /proc/mounts | grep $local_mountpoint > /dev/null; then
echo "Windows-Share verfuegbar, starte disk dump"
if dd if=/dev/mmcblk0 of=$local_mountpoint/$win_share_target_dir$filename bs=1M; then
echo "Das Backup sollte auf dem Server unter $win_share_target_dir liegen" | mail -s"ChickenPi DD backup erfolgreich ausgefuehrt" $info_email_adr
else
echo "Der DD Befehl meldete einen Fehler!" | mail -s"ChickenPi DD backup FEHLER" $info_email_adr
fi
else
echo "Windows-Share konnte nicht gemountet werden!!!"
echo "Konnte Windows Share nicht mounten" | mail -s"ChickenPi DD backup" $info_email_adr
fi
printf "\n~~~+++~~~\nFertig"


exit 0
