Advertisement
Jaels

Untitled

Apr 10th, 2017
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.91 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. function clear_maillogs {
  4.  
  5. cat /dev/null > $LOGS/mailfile.txt
  6. cat /dev/null > $LOGS/worklog.txt
  7. }
  8. function fill_sender_recipients {
  9.     echo "From: sender01@ppl33-35.net" 2>&1 | tee -a $LOGS/mailfile.txt
  10.         echo "To: fys@ppl33-35.net, s.kudelin@ppl33-35.com" 2>&1 | tee -a $LOGS/mailfile.txt
  11.         echo "Subject: rsync copy log" 2>&1 | tee -a $LOGS/mailfile.txt
  12.         echo "" 2>&1 | tee -a $LOGS/mailfile.txt
  13. }
  14. function fill_checker_recipients {
  15.     echo "From: checker01@ppl33-35.net" 2>&1 | tee -a $LOGS/mailfile.txt
  16.         echo "To: fys@ppl33-35.net, s.kudelin@ppl33-35.com" 2>&1 | tee -a $LOGS/mailfile.txt
  17.         echo "Subject: checker log" 2>&1 | tee -a $LOGS/mailfile.txt
  18.         echo "" 2>&1 | tee -a $LOGS/mailfile.txt
  19. }
  20.  
  21. function sender {
  22.     SOURCE=/mnt/smb/online/
  23.     TARGET=/mnt/storage/
  24.     RSYNCLIST=/root/nas/logs/rsynclist.txt
  25.     RSYNCLOGFILE=/root/nas/logs/rsynclogfile.txt
  26.     LOGS=/root/nas/logs/
  27.  
  28.     cat /dev/null > $LOGS/mailfile.txt
  29.     cat /dev/null > $LOGS/sender_worklog.txt
  30.     cat /dev/null > $RSYNCLIST
  31.     cat /dev/null > $RSYNCLOGFILE
  32.     cat /dev/null > $LOGS/old_files_removed.txt
  33.     fill_sender_recipients
  34.     echo "Backup started at `date`" 2>&1 | tee -a $LOGS/sender_worklog.txt
  35.     echo "" 2>&1 | tee -a $LOGS/sender_worklog.txt
  36.     cd $SOURCE
  37.     find . -mtime -2 -print | tee -a $RSYNCLIST
  38.     rsync -av --ignore-existing --size-only --log-file=$RSYNCLOGFILE --files-from=$RSYNCLIST $SOURCE $TARGET
  39.  
  40.     ### DEBUG STARTED HERE ###
  41.  
  42.     MOUNTSTATUS=`systemctl is-active mnt-smb.mount`
  43.     if [ "$MOUNTSTATUS" == "active" ]; then
  44.         echo "" 2>&1 | tee -a $LOGS/debug.log
  45.         echo "Debug mode enabled: smb is up" 2>&1 | tee -a $LOGS/debug.log
  46.             echo "" 2>&1 | tee -a $LOGS/debug.log
  47.     else [ "$MOUNTSTATUS" == "inactive" ];
  48.             echo "Debug mode enabled: smb is down" 2>&1 | tee -a $LOGS/debug.log
  49.             echo "" 2>&1 | tee -a $LOGS/debug.log
  50.     fi
  51.    
  52.     ### DEBUG ENDED HERE ###
  53.    
  54.     find $TARGET -maxdepth 1 -mtime +14 -print -exec rm -rf {} \; > $LOGS/old_files_removed.txt
  55.     cat $RSYNCLOGFILE >> $LOGS/sender_worklog.txt
  56.     echo "" 2>&1 | tee -a $LOGS/sender_worklog.txt
  57.     echo "Files below was removed due to expiration date (15 days keep)" 2>&1 | tee -a $LOGS/sender_worklog.txt
  58.     cat $LOGS/old_files_removed.txt 2>&1 | tee -a $LOGS/sender_worklog.txt
  59.     echo "" 2>&1 | tee -a $LOGS/sender_worklog.txt
  60.     echo "Backup ended at `date`" 2>&1 | tee -a $LOGS/sender_worklog.txt
  61.     cat $LOGS/sender_worklog.txt >> $LOGS/mailfile.txt && cat $LOGS/mailfile.txt | sendmail -t
  62.  
  63. }
  64.  
  65. LOGS=/root/nas/logs/
  66. clear_maillogs
  67. MOUNTSTATUS=`systemctl is-active mnt-smb.mount`
  68.  
  69. echo "checker started at `date` and uptime is `uptime -p`" 2>&1 | tee -a $LOGS/worklog.txt
  70. fill_checker_recipients
  71.  
  72. if [ "$MOUNTSTATUS" == "active" ]; then
  73.     echo "" 2>&1 | tee -a $LOGS/worklog.txt
  74.     echo "SMB Share seems UP: bypassing control to copy script" 2>&1 | tee -a $LOGS/worklog.txt
  75.         echo "" 2>&1 | tee -a $LOGS/worklog.txt
  76.     cat $LOGS/worklog.txt >> $LOGS/mailfile.txt && cat $LOGS/mailfile.txt | sendmail -t
  77.     #call sender function
  78.     sender
  79.     CURRENTDAY=`date +%A`
  80.     #clear logs before sending email
  81.         clear_maillogs && fill_checker_recipients
  82.     if [ "$CURRENTDAY" == "Tuesday" ]; then
  83.         echo "Backup done, bypassing control at 11:00 tomorrow" 2>&1 | tee -a $LOGS/worklog.txt
  84.         cat $LOGS/worklog.txt >> $LOGS/mailfile.txt && cat $LOGS/mailfile.txt | sendmail -t
  85.         at -f /root/nas/waker.sh 11:00 tomorrow
  86.     else
  87.         echo "Backup done, bypassing control at 22:00 today" 2>&1 | tee -a $LOGS/worklog.txt
  88.         cat $LOGS/worklog.txt >> $LOGS/mailfile.txt && cat $LOGS/mailfile.txt | sendmail -t
  89.                 at -f /root/nas/waker.sh 22:00 today
  90.     fi
  91.  
  92. else [ "$MOUNTSTATUS" == "inactive" ];
  93.     echo "SMB Share is unmounted, check it manually" 2>&1 | tee -a $LOGS/worklog.txt
  94.     echo "" 2>&1 | tee -a $LOGS/worklog.txt
  95.     clear_maillogs && fill_checker_recipients
  96.     cat $LOGS/worklog.txt >> $LOGS/mailfile.txt && cat $LOGS/mailfile.txt | sendmail -t
  97. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement