SoFunction
Updated on 2025-04-18

Example of Python implementation of log backup daemon

Training background

Suppose you are an operation and maintenance engineer and need to develop a simple log backup daemon for the company's monitoring system. This process needs to meet the following requirements:

  • Background operation: Leave the terminal and monitor log files in specified directories (such as /var/log/app/) for a long time.
  • Automatic backup: Compress and backup the newly added log files to the /backup/logs/ directory every 5 minutes.
  • Logging: Record the daemon's own operation log to /var/log/backup_daemon.log.
  • System service: manages the start, stop and status view of the process through systemd.

Environmental preparation

Operating system: mainstream Linux distributions such as Ubuntu/CentOS

Python version: Python

Depend on installation:

# Make sure Python3 and pip are installedsudo apt install python3 python3-pip   # Ubuntu
sudo yum install python3 python3-pip   # CentOS

Training steps

Task 1: Write Python daemon code

Goal: Use Python to implement log monitoring and backup logic without manual processing.fork()

Create scriptbackup_daemon.py

import os
import time
import logging
from datetime import datetime
import subprocess

#Configuration log(
    filename='/var/log/backup_daemon.log',
    level=,
    format='%(asctime)s - %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S'
)

def backup_logs():
    log_dir = '/var/log/app'
    backup_dir = '/backup/logs'

    # traverse the log directory    for root, _, files in (log_dir):
        for file in files:
            src_path = (root, file)
            dest_path = (backup_dir, f"{file}.")

            # Check if it has been backed up            if not (dest_path):
                (f"Backing up {file}...")
                # Use tar to compress                try:
                    (
                        ['tar', '-czf', dest_path, src_path],
                        check=True,
                        stdout=,
                        stderr=
                    )
                except  as e:
                    (f"Failed to backup {file}: {e}")

if __name__ == "__main__":
    ("Daemon started.")
    while True:
        backup_logs()
        (300)  # Execute once in 5 minutes

Task 2: Configure the systemd service

Goal: Let Python scripts run in the background as services, without the need for in-code daemon logic.

Create a service file

sudo vim /etc/systemd/system/backup_daemon.service

Write service configuration

[Unit]
Description=Python Log Backup Daemon
After=

[Service]
Type=simple
ExecStart=/usr/bin/python3 /path/to/backup_daemon.py  # Modify to the actual script pathRestart=on-failure
RestartSec=5s
User=root
WorkingDirectory=/  # Optional: Set the working directory
[Install]
WantedBy=

Deploy and start the service

sudo systemctl daemon-reload
sudo systemctl start backup_daemon
sudo systemctl enable backup_daemon

Task 3: Testing and Verification

Generate test log files

sudo touch /var/log/app/

View backup results

ls /backup/logs  # It should be generated in 5 minutes

View daemon log

tail -f /var/log/backup_daemon.log

Task 4: Management Services

View status

systemctl status backup_daemon

Stop service

sudo systemctl stop backup_daemon

View systemd log

journalctl -u backup_daemon -f  # Real-time tracking logs

Training summary

Through this case, you will learn:

  • Implement daemon logic using Python (no manualfork())。
  • passsystemdManage the background operation of Python scripts.
  • Using PythonloggingThe module records the operation log.

Key points of knowledge

Advantages of Python:

  • No need to deal with the underlying layerfork()andsetsid(), the code is more concise.
  • usesubprocessModules can easily call system commands (such astar)。

systemd management:

  • passType=simpleRun the foreground program directly,systemdAutomatic protection.
  • Logs can be passedjournalctlUnified viewing.

Logging:

  • Python built-inloggingThe module provides flexible log management.

Extended optimization suggestions

  • Incremental backup: Records the backed-up file name or timestamp to avoid repeated compression.
  • Exception handling: Addtry/exceptCapture file operation exception.
  • Configuration file: UseconfigparserModule management path, interval time and other parameters.

This is the end of this article about Python's example implementation of log backup daemon. For more related Python log backup daemon content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!