Advertisement
Jaels

Untitled

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