Write Unix shell script to automate the backup and rotation of application log files stored in a specific directory
2 min readJul 14, 2024
Create daily backups of log files.
Compress older backups.
Maintain a defined number of backup copies.
Manage disk space efficiently.
#!/bin/bash
# Configuration
LOG_DIR="/path/to/log_directory"
BACKUP_DIR="/path/to/backup_directory"
RETENTION_DAYS=7
DATE=$(date +'%Y-%m-%d')
# Ensure backup directory exists
mkdir -p "$BACKUP_DIR"
# Create a daily backup
backup_logs() {
local backup_file="$BACKUP_DIR/logs_$DATE.tar.gz"
tar -czf "$backup_file" -C "$LOG_DIR" .
if [ $? -eq 0 ]; then
echo "$(date +'%Y-%m-%d %H:%M:%S'): Successfully created backup $backup_file"
else
echo "$(date +'%Y-%m-%d %H:%M:%S'): Failed to create backup $backup_file" >&2
exit 1
fi
}
# Remove backups older than RETENTION_DAYS
cleanup_old_backups() {
find "$BACKUP_DIR" -type f -name "*.tar.gz" -mtime +$RETENTION_DAYS -exec rm {} \;
if [ $? -eq 0 ]; then
echo "$(date +'%Y-%m-%d %H:%M:%S'): Successfully cleaned up old backups"
else
echo "$(date +'%Y-%m-%d %H:%M:%S'): Failed to clean up old backups" >&2
fi
}
# Main script execution
backup_logs
cleanup_old_backups
Option-2
#!/bin/bash
# Define variables
LOG_DIR="/var/log/myapp" # Directory containing application logs
BACKUP_DIR="/var/log/myapp_backups" # Directory for storing backups
MAX_BACKUPS=7 # Maximum number of backup files to keep
LOG_FILE="application.log" # Specific log file to backup (optional)
# Ensure backup directory exists
if [[ ! -d "$BACKUP_DIR" ]]; then
mkdir -p "$BACKUP_DIR"
fi
# Get current date in YYYY-MM-DD format
TODAY=$(date +%Y-%m-%d)
# Create a compressed archive of the log file(s)
if [[ -n "$LOG_FILE" ]]; then
# Backup specific log file
tar -czf "$BACKUP_DIR/$TODAY-$LOG_FILE.tar.gz" "$LOG_DIR/$LOG_FILE"
else
# Backup all log files in the directory
tar -czf "$BACKUP_DIR/$TODAY.tar.gz" "$LOG_DIR/*"
fi
# Clean up old backups
count=$(ls -tr "$BACKUP_DIR" | wc -l)
if [[ $count -gt $MAX_BACKUPS ]]; then
# Delete oldest backups until reaching the desired number
for file in $(ls -t "$BACKUP_DIR" | tail -n +$(($count - $MAX_BACKUPS))); do
rm -f "$BACKUP_DIR/$file"
done
fi
echo "Log backup completed on $(date)"