Operations 4 min read

Linux Log Backup Script with Automated Cleanup and Cron Scheduling

This article presents a Bash script that backs up Linux system log directories, removes backups older than a configurable number of days, compresses the daily logs, and sets up a daily cron job to automate the entire process.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Linux Log Backup Script with Automated Cleanup and Cron Scheduling

In daily development you often need to back up configuration files and log data; the following Bash script automates the backup of a specified Linux directory, deletes backup data older than a configurable number of days, and compresses the daily logs for easy storage.

#!/bin/bash #bakup system logs yesterday=`date -d -1day +%Y-%m-%d` cat /backup/syslos/ip.txt|grep -v ^#|while read ip; do log_dir=`echo $ip|awk '{print $1}'` if [ ! -d "/backup/syslog/$log_dir/$yesterday" ];then mkdir -p /backup/syslog/$log_dir/$yesterday fi rsync -avz -e ssh root@$ip:/var/log/messages /backup/syslog/$log_dir/$yesterday rsync -avz -e ssh root@$ip:/var/log/secure /backup/syslog/$log_dir/$yesterday rsync -avz -e ssh root@$ip:/var/log/boot.log /backup/syslog/$log_dir/$yesterday rsync -avz -e ssh root@$ip:/var/log/cron /backup/syslog/$log_dir/$yesterday rsync -avz -e ssh root@$ip:/var/log/lastlog /backup/syslog/$log_dir/$yesterday rsync -avz -e ssh root@$ip:/var/log/audit/audit.log /backup/syslog/$log_dir/$yesterday cd /backup/syslogs/$log_dir/$yesterday && tar -zcvf syslog.tar.gz . find . -type f -not -name '*.tar.gz' -exec rm -f {} + done find /backup/syslogs/ -type f -name "*.tar.gz" -mtime +90 -exec rm -f {} \;

The script is intended to be executed daily via cron . A typical crontab entry schedules the backup at midnight each day:

0 0 * * * cd /backup/syslog && bash syslog.sh

By using rsync over SSH, the script pulls various log files (messages, secure, boot.log, cron, lastlog, audit.log) from each host listed in /backup/syslos/ip.txt , stores them in a date‑specific directory, compresses them into syslog.tar.gz , and removes archives older than 90 days to conserve space.

Users are encouraged to like, share, and comment on the article to support further content creation.

AutomationLinuxBackupCronbashsyslog
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.