Day 2 : Mastering Log File Management ~ A Bash Script for Seamless Backup and Cleanup
Hi Amigos, Today we will explore a simple script example demonstrating how we can effectively manage log files.
This script will create a copy and generate a backup file in a specific directory. Afterwards, if the log file remains inaccessible for 7 days, it will be removed as it is no longer needed
#!/bin/bash
LOG_FILE="/var/logs/AF_service.log"
BACKUP_DIR="/var/logs/backups"
BACKUP_FILE="$BACKUP_DIR/AF_service_$(date +"%Y-%m-%d").log"
if [ ! -d "$BACKUP_DIR" ]; then
mkdir "$BACKUP_DIR"
else
echo "$BACKUP_DIR already exists"
fi
cp "$LOG_FILE" "$BACKUP_FILE"
if [ $? -eq 0 ]; then
echo "Backup successful : $BACKUP_FILE"
else
echo "Log file backup failed!"
exit 1
fi
find "$BACKUP_DIR" -type f -name "*.log" -mtime +0.00034722 -delete
Output :
/var/logs/backups already exists
Backup successful : /var/logs/backups/AF_service_2024-04-06.log
Explanation :
LOG_FILE="/var/logs/AF_service.log"
This line defines a variable LOG_FILE
and assigns the path /var/logs/AF_service.log
to it. This variable stores the path of the log file that needs to be backed up.
BACKUP_DIR="/var/logs/backups"
Here, a variable BACKUP_DIR
is defined and assigned the path /var/logs/backups
. This variable represents the directory where backup files will be stored.
BACKUP_FILE="$BACKUP_DIR/AF_service_$(date +"%Y-%m-%d").log"
This line constructs the name of the backup file using the current date and stores it in the variable BACKUP_FILE
. The format for the date is YYYY-MM-DD
.
if [ ! -d "$BACKUP_DIR" ]; then
mkdir "$BACKUP_DIR"
else
echo "$BACKUP_DIR already exists"
fi
This block of code checks if the backup directory exists. If it doesn't ([ ! -d "$BACKUP_DIR" ]
), it creates the directory using the mkdir
command. If the directory already exists, it prints a message indicating that
cp "$LOG_FILE" "$BACKUP_FILE"
This command copies the log file specified by LOG_FILE
to the backup file specified by BACKUP_FILE
using the cp
command.
if [ $? -eq 0 ]; then
echo "Backup successful : $BACKUP_FILE"
else
echo "Log file backup failed!"
exit 1
fi
This block checks the exit status of the previous command (cp "$LOG_FILE" "$BACKUP_FILE"
). If the exit status is 0
, it means the command was successful, so it prints a success message along with the path of the backup file. If the exit status is not 0
, it prints an error message indicating that the log file backup failed and exits the script with an error code of 1
.
find "$BACKUP_DIR" -type f -name "*.log" -mtime +7 -delete
This command uses the find
utility to locate files (-type f
) in the backup directory ($BACKUP_DIR
) with names matching *.log
pattern that are older than 7 days (-mtime +7
). It then deletes them using the -delete
action. This line ensures that old log files are periodically removed from the backup directory to save disk space.
Then, you can enable the cron expression to run this script every day at the same time
Open the cron table for editing by running
crontab -e
Add a new line at the end of the file to specify the schedule for executing your script. For example, to run the script every day at midnight, you can add
0 0 * * * /path/to/your/script.sh
This cron expression 0 0 * * *
represents :
- Minute:
0
(0th minute) - Hour:
0
(0th hour, which is midnight) - Day of month:
*
(every day) - Month:
*
(every month) - Day of week:
*
(every day of the week)
Replace /path/to/your/script.sh
with the actual path to your script.
Task : If you want to enable email notifications, such as receiving a message or file whenever a backup is completed, check out this blog https://medium.com/@CJwrites154/sending-mail-from-a-linux-environment-9a9efea0c29f
And that’s a wrap! Thanks for tuning in. Loads of love to you all! ❤️