Skip to content

Instantly share code, notes, and snippets.

@ashiwanikumar
Last active October 5, 2023 09:22
Show Gist options
  • Save ashiwanikumar/b220a7f82a7e8539d6ee3190dcb8f2b9 to your computer and use it in GitHub Desktop.
Save ashiwanikumar/b220a7f82a7e8539d6ee3190dcb8f2b9 to your computer and use it in GitHub Desktop.
Scheduled Server Updates & Get Report from Email

Updating a File:

1. Create a new file named "autoupdate" under /etc/cron.daily/:

Use the vim text editor to create the file:

sudo vim /etc/cron.daily/autoupdate

Note: If you're not familiar with vim, you can use nano or any other text editor instead.

Save and exit the file.

2. Paste the script into the file:

Here's a script for OS Update with Dist-upgrade:

#!/bin/sh
{
    echo "Auto update started on $(date)"
    sudo apt-get update
    sudo apt-get upgrade -y
    sudo apt-get dist-upgrade -y
    sudo apt-get autoclean
    sudo apt-get autoremove -y
    echo "Auto update completed on $(date)"
} >> /var/log/autoupdate.log

mail -s "Auto update report for Server :Website_API_10.4.0.230 $(date)" -a "From: no-reply@domain.org" your_email@domain.org < /var/log/autoupdate.log

Or use this script for Dist-upgrade only:

#!/bin/sh
{
    echo "Auto update started on $(date)"
    sudo apt-get update
    sudo apt-get upgrade -y
    echo "Dist-upgrade started on $(date)"
    sudo apt-get dist-upgrade -y
    echo "Dist-upgrade completed on $(date)"
    sudo apt-get autoclean
    sudo apt-get autoremove -y
    echo "Auto update completed on $(date)"
} >> /var/log/autoupdate.log

mail -s "Auto update report for Server :Subject Name/Server Details $(date)" -a "From: no-reply@domain.org" your_email@domain.org < /var/log/autoupdate.log

Remember to replace your_email@domain.org with your actual email address.

3. Make the "autoupdate" file executable:

sudo chmod +x /etc/cron.daily/autoupdate

Save and exit the file.

Installing Mailutils:

1. Install Mailutils:

sudo apt-get install mailutils -y && sudo apt-get install s-nail

2. Install postfix:

sudo apt-get install postfix

3. Edit the configuration file of postfix and comment line 42:

To open the configuration file:

sudo vim /etc/postfix/main.cf

Comment the line:

#relayhost =

Add the following lines to the end of the file:

relayhost = [smtp.office365.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_use_tls = yes
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

Save and exit the file.

4. Edit the /etc/postfix/sasl_passwd file and add this line:

sudo vim /etc/postfix/sasl_passwd
[smtp.office365.com]:587 your_email@domain.org:YourPassword

Replace your_email@domain.org:YourPassword with your actual email address and password.

5. Change the ownership and permissions of the file:

sudo chown root:root /etc/postfix/sasl_passwd
sudo chmod 600 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd

6. Restart postfix:

sudo systemctl restart postfix

7. Map the password file again:

sudo postmap /etc/postfix/sasl_passwd

Testing the Email:

1. Test sending an email:

echo "This is a test email body." | mail -s "Test Email Subject" -a "From: your_email@domain.org" recipient_email@domain.org

Replace your_email@domain.org and recipient_email@domain.org with your actual email addresses.

Install Antivirus and Schedule Auto Scans with Email Notifications for SCAN OUTPUT

Install ClamAV:

sudo apt-get install clamav clamav-daemon -y

Update ClamAV Definitions:

sudo freshclam

Manage ClamAV Service:

To start, enable, check the status, stop, disable, and restart the freshclam service, use these commands respectively:

sudo systemctl start clamav-freshclam
sudo systemctl enable clamav-freshclam
sudo systemctl status clamav-freshclam
sudo systemctl stop clamav-freshclam
sudo systemctl disable clamav-freshclam
sudo systemctl restart clamav-freshclam

Schedule a CronJob for Auto Scans and Email Notifications

Edit the CRONTAB configuration file:

sudo crontab -e

Schedule a cron job to run a weekly virus scan on the server's root directory. Log results to /var/log/clamscan.log, and send an email report from no-reply@domain.org to your email. Don't forget to replace your_email@domain.org with your actual email address.

0 1 * * SUN sudo clamscan -r -i / | tee -a /var/log/clamscan.log | mail -s "Clamscan_Output_Website_API_10.4.0.230" -a "From: no-reply@domain.org" your_email@domain.org

Save and exit the file.

Manual Scan

To manually scan your system for viruses and get a list of infected files:

sudo clamscan -r -i /

After you review the list of infected files and decide you want to remove them, you can run:

sudo clamscan -r --remove /

2. Insert this line in the crontab to automatically renew certbot and send an email after completion:

0 2 * * * /usr/bin/certbot renew --quiet; echo "Certbot renewal job completed" | mail -s "Certbot Renewal Output" -a "From: no-reply@domain.org" your_email@domain.org

Don't forget to replace your_email@domain.org with your actual email address.


That's it! Remember to check the logs (/var/log/autoupdate.log and /var/log/clamscan.log) from time to time to ensure everything is running smoothly. Use cat /var/log/autoupdate.log and cat /var/log/clamscan.log to display these logs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment