Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- function clear_maillogs {
- cat /dev/null > $LOGS/mailfile.txt
- cat /dev/null > $LOGS/worklog.txt
- }
- function fill_sender_recipients {
- echo "From: sender01@ppl33-35.net" 2>&1 | tee -a $LOGS/mailfile.txt
- echo "To: fys@ppl33-35.net, s.kudelin@ppl33-35.com" 2>&1 | tee -a $LOGS/mailfile.txt
- echo "Subject: rsync copy log" 2>&1 | tee -a $LOGS/mailfile.txt
- echo "" 2>&1 | tee -a $LOGS/mailfile.txt
- }
- function fill_checker_recipients {
- echo "From: checker01@ppl33-35.net" 2>&1 | tee -a $LOGS/mailfile.txt
- echo "To: fys@ppl33-35.net, s.kudelin@ppl33-35.com" 2>&1 | tee -a $LOGS/mailfile.txt
- echo "Subject: checker log" 2>&1 | tee -a $LOGS/mailfile.txt
- echo "" 2>&1 | tee -a $LOGS/mailfile.txt
- }
- function sender {
- SOURCE=/mnt/smb/online/
- TARGET=/mnt/storage/
- RSYNCLIST=/root/nas/logs/rsynclist.txt
- RSYNCLOGFILE=/root/nas/logs/rsynclogfile.txt
- LOGS=/root/nas/logs/
- cat /dev/null > $LOGS/mailfile.txt
- cat /dev/null > $LOGS/sender_worklog.txt
- cat /dev/null > $RSYNCLIST
- cat /dev/null > $RSYNCLOGFILE
- cat /dev/null > $LOGS/old_files_removed.txt
- fill_sender_recipients
- echo "Backup started at `date`" 2>&1 | tee -a $LOGS/sender_worklog.txt
- echo "" 2>&1 | tee -a $LOGS/sender_worklog.txt
- cd $SOURCE
- find . -mtime -2 -print | tee -a $RSYNCLIST
- rsync -av --ignore-existing --size-only --log-file=$RSYNCLOGFILE --files-from=$RSYNCLIST $SOURCE $TARGET
- ### DEBUG STARTED HERE ###
- MOUNTSTATUS=`systemctl is-active mnt-smb.mount`
- if [ "$MOUNTSTATUS" == "active" ]; then
- echo "" 2>&1 | tee -a $LOGS/debug.log
- echo "Debug mode enabled: smb is up" 2>&1 | tee -a $LOGS/debug.log
- echo "" 2>&1 | tee -a $LOGS/debug.log
- else [ "$MOUNTSTATUS" == "inactive" ];
- echo "Debug mode enabled: smb is down" 2>&1 | tee -a $LOGS/debug.log
- echo "" 2>&1 | tee -a $LOGS/debug.log
- fi
- ### DEBUG ENDED HERE ###
- find $TARGET -maxdepth 1 -mtime +14 -print -exec rm -rf {} \; > $LOGS/old_files_removed.txt
- cat $RSYNCLOGFILE >> $LOGS/sender_worklog.txt
- echo "" 2>&1 | tee -a $LOGS/sender_worklog.txt
- echo "Files below was removed due to expiration date (15 days keep)" 2>&1 | tee -a $LOGS/sender_worklog.txt
- cat $LOGS/old_files_removed.txt 2>&1 | tee -a $LOGS/sender_worklog.txt
- echo "" 2>&1 | tee -a $LOGS/sender_worklog.txt
- echo "Backup ended at `date`" 2>&1 | tee -a $LOGS/sender_worklog.txt
- cat $LOGS/sender_worklog.txt >> $LOGS/mailfile.txt && cat $LOGS/mailfile.txt | sendmail -t
- }
- LOGS=/root/nas/logs/
- clear_maillogs
- MOUNTSTATUS=`systemctl is-active mnt-smb.mount`
- echo "checker started at `date` and uptime is `uptime -p`" 2>&1 | tee -a $LOGS/worklog.txt
- fill_checker_recipients
- if [ "$MOUNTSTATUS" == "active" ]; then
- echo "" 2>&1 | tee -a $LOGS/worklog.txt
- echo "SMB Share seems UP: bypassing control to copy script" 2>&1 | tee -a $LOGS/worklog.txt
- echo "" 2>&1 | tee -a $LOGS/worklog.txt
- cat $LOGS/worklog.txt >> $LOGS/mailfile.txt && cat $LOGS/mailfile.txt | sendmail -t
- #call sender function
- sender
- CURRENTDAY=`date +%A`
- #clear logs before sending email
- clear_maillogs && fill_checker_recipients
- if [ "$CURRENTDAY" == "Tuesday" ]; then
- echo "Backup done, bypassing control at 11:00 tomorrow" 2>&1 | tee -a $LOGS/worklog.txt
- cat $LOGS/worklog.txt >> $LOGS/mailfile.txt && cat $LOGS/mailfile.txt | sendmail -t
- at -f /root/nas/waker.sh 11:00 tomorrow
- else
- echo "Backup done, bypassing control at 22:00 today" 2>&1 | tee -a $LOGS/worklog.txt
- cat $LOGS/worklog.txt >> $LOGS/mailfile.txt && cat $LOGS/mailfile.txt | sendmail -t
- at -f /root/nas/waker.sh 22:00 today
- fi
- else [ "$MOUNTSTATUS" == "inactive" ];
- echo "SMB Share is unmounted, check it manually" 2>&1 | tee -a $LOGS/worklog.txt
- echo "" 2>&1 | tee -a $LOGS/worklog.txt
- clear_maillogs && fill_checker_recipients
- cat $LOGS/worklog.txt >> $LOGS/mailfile.txt && cat $LOGS/mailfile.txt | sendmail -t
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement