Truth be told, one day I will learn and use somethimg like Ansible, but today I use the following scripts to make regular maintenance tasks easy for the linux servers in my home lab:

For my Ubuntu/Debian servers:

#!/bin/bash

# Update the system
sudo apt-get update -y
sudo apt-get upgrade -y

# Clear the package cache
sudo apt-get clean

# Check for and remove orphaned packages
sudo apt-get autoremove -y

# Rotate log files
sudo logrotate /etc/logrotate.conf

# Check the file system for errors
sudo fsck -y /dev/sda1

# Check for and remove old backups
find /backups/* -mtime +30 -exec rm {} \;


# Define the current date and time
date_time=$(date +%Y-%m-%d_%H-%M-%S)

# Define the filename of the backup
filename="server_backup_$date_time.tar.gz"

# Create the backup
tar -zcvf /backup/$filename / --exclude=/backup

# Print confirmation message
echo "Backup complete. File located at backup/$filename"


# Schedule a reboot
sudo shutdown -r +1 "Rebooting for monthly maintenance"

For my CentOS servers:

#!/bin/bash

# This script performs monthly maintenance tasks on a CentOS based server

# Update the system
yum update -y

# Clear yum cache
yum clean all

# Remove old kernels
package-cleanup --oldkernels --count=1

# Run the system file checker
fsck -A

# Clear the logs
cat /dev/null > /var/log/messages
cat /dev/null > /var/log/secure
cat /dev/null > /var/log/cron
cat /dev/null > /var/log/maillog

# Clear the tmp directory
rm -rf /tmp/*


# Backup the system
tar -zcvf /backup/monthly_backup_$(date +%Y-%m-%d).tar.gz / --exclude=/backup

# Restart the system
reboot