Hi
in the past days I have written my first script that will run through crontab.
Please have a look, there are comments to be easier understood from everyone.
After the code part some more comments from my side.
#!/bin/bash
# The scripts should always start with the external devices unmounte.
# 1. First checks that the device is not mounted (that should be always)
# 2. Mounts the device
# 3. Copy the files
# 4. Unmounts the device
# 5. Send emails at the moment a failure appears or at the end when everything was succesful
SCRIPTNAME="BACKUPOFDATA.SH"
MOUNTPOINT=/mnt/BckpMeasurements
MAILADDRESS="hide@host.de"
doEmail() { case "$1" in
1) REASON="Mounting the Hard Disk Failed." SUBJECT="SUCCESS: rsync (backup).";;
2) REASON="Device alredy Mounted. Previous umounts failed?" SUBJECT="Backup Script Failed";;
3) REASON="Device Failed to be unmounted!" SUBJECT="Backup script failed" ;;
4) REASON="Rsync failed for some reasons" SUBJECT="Backup script failed" ;;
5) REASON="Backup Finished At: Date and Time Sucessfully";; #How to concatenate Date and Time information in that string?
*) echo "Please don't do that, Dave..." >/dev/stderr;;
esac; echo "${REASON}" | mail -r $MAILADDRESS -s "${SUBJECT}" $MAILADDRESS; }
# mount | grep $MOUNTPOINT: returns 0 when found (mounted). returns 1 when not found (unmounted)
# 1.
#Check that the device is not mounted already
(mount | grep $MOUNTPOINT) && (doEmail 2; exit 1;) # That means that mount is there already and it should not. Report the error and exit
# 2. Proceed mounting for the first time.
mount $MOUNTPOINT 2> /dev/null
(mount | grep $MOUNTPOINT) || (doEmail 1; exit 1;) # Mount is not there so something wrong happened. Report the error and exit
# Do the Copy of The Files
# 3.
# rsync command goes here
# if rsync failes send an email to me too
(rsync -rav -e ssh user@host:/storage/test/ $MOUNTPOINT) || (do Email 4; exit 1;)
# How to check that rsync returned succesfully
# End of Copy of the files
# 4.
# Unmount the Device
umount $MOUNTPOINT
(mount | grep $MOUNTPOINT) && (doEmail 3; exit 1;) # That means that the mount is there already and it should not. Report the error and exit
# 5.
doEmail 5; exit 0; # Exit 0 when normally returns?????
-As you see in the code there are some questions there already that I need help to finish those
-I was executing in the past a simple script containing only one (just one) rsync command with no checks (literally one line file). This was called then through crontab (crontab entry below)
0 5,14,22 * * * /root/backup/rsyncScript.sh > /root/CronErrors/rsync.out 2> /root/CronErrors/rsync.err
As you can see I was collecting the rsync errors inside the crontab entry. Should I move those in the large script snippet I gave above?
I would like to thank you in advance for your support
Alex