Files
rotate/README.md
2025-07-11 18:58:25 +00:00

128 lines
4.8 KiB
Markdown

# Logrotate Automation Script
This simple Bash script automates the execution of `logrotate` for specific log files, ensuring that your logs are managed effectively to prevent disk space issues and improve system performance.
-----
## Features
* **Automated Log Rotation**: Automatically runs `logrotate` with a specified configuration.
* **Status Tracking**: Utilizes a status file to keep track of the last rotation times.
* **Error Handling**: Checks for the existence of the configuration file and logs errors if it's missing.
* **Logging**: Records the execution status and any errors to a dedicated log file.
-----
## Getting Started
These instructions will help you set up and run the script on your system.
### Prerequisites
* **`logrotate`**: This utility is typically pre-installed on most Linux distributions. If not, you can install it using your system's package manager (e.g., `sudo apt-get install logrotate` on Debian/Ubuntu, `sudo yum install logrotate` on CentOS/RHEL).
### Installation
1. **Save the Script**: Copy the provided script content into a file (e.g., `run_logrotate.sh`) in your desired directory.
2. **Make Executable**: Grant execute permissions to the script:
```bash
chmod +x run_logrotate.sh
```
3. **Create Configuration**: Create a `logrotate.cfg` file in the same directory as the script. This file will contain your `logrotate` rules.
4. **Create Log and Status Files**: The script will create these if they don't exist, but you can create empty ones manually if you prefer:
```bash
touch logrotate.status
touch logrotate.log
```
-----
## Usage
### Configuration (`logrotate.cfg`)
The `logrotate.cfg` file defines how your logs should be rotated. Here's a basic example:
```
/var/log/my_app/*.log {
daily
rotate 7
compress
missingok
notifempty
create 640 root adm
dateext
}
```
* Replace `/var/log/my_app/*.log` with the actual path to your log files.
* `daily`: Rotate logs daily.
* `rotate 7`: Keep 7 rotated log files.
* `compress`: Compress old log files.
* `missingok`: Don't issue an error if the log file is missing.
* `notifempty`: Don't rotate the log if it is empty.
* `create 640 root adm`: Create new log files with specified permissions and ownership.
* `dateext`: Archive rotated logs by adding a date extension.
For more details on `logrotate` configuration, refer to the `logrotate` man page (`man logrotate`).
### Running the Script
You can run the script manually:
```bash
./run_logrotate.sh
```
### Automation (Cron Job)
To automate log rotation, it's recommended to set up a cron job.
1. Open your crontab for editing:
```bash
crontab -e
```
2. Add a line to execute the script at your desired interval. For example, to run it daily at 3:00 AM:
```cron
0 3 * * * /home/oberkoetter/Dokumente/Git/rettedpv/run_logrotate.sh >> /dev/null 2>&1
```
Make sure to replace `/home/oberkoetter/Dokumente/Git/rettedpv/run_logrotate.sh` with the actual full path to your script. The `>> /dev/null 2>&1` part redirects standard output and error to `/dev/null` to prevent unnecessary emails from cron.
-----
## Script Details
The script performs the following actions:
1. **Changes Directory**: Navigates to `/home/oberkoetter/Dokumente/Git/rettedpv`. **Note:** You might want to adjust this path if you move the script.
2. **Defines Paths**: Sets variables for the `logrotate.cfg`, `logrotate.status`, and `logrotate.log` files within the current working directory.
3. **Checks Configuration**: Verifies if `logrotate.cfg` exists. If not, it logs an error and exits.
4. **Executes Logrotate**: Runs `logrotate` in forced mode (`-f`) using the specified configuration and status files.
5. **Logs Success**: Appends a success message to `logrotate.log` after a successful execution.
-----
## Project Structure
```
.
├── run_logrotate.sh
├── logrotate.cfg
├── logrotate.log
└── logrotate.status
```
* `run_logrotate.sh`: The main script to execute logrotate.
* `logrotate.cfg`: The configuration file for logrotate.
* `logrotate.log`: Logs the execution history of the script.
* `logrotate.status`: Logrotate's internal status file.
-----
## Troubleshooting
* **"Fehler: Konfigurationsdatei './logrotate.cfg' nicht gefunden."**: Ensure `logrotate.cfg` is in the same directory as the script or update the `CONFIG` variable in the script to the correct path.
* **Script not running via cron**: Double-check the cron job entry for correct paths and permissions. You can test by manually running the script. Also, check your `logrotate.log` for any errors.
* **Log files not rotating**: Verify your `logrotate.cfg` syntax. You can test your `logrotate` configuration directly using `logrotate -d your_config_file` (debug mode) or `logrotate -f your_config_file` (force mode).
-----