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