====== Nginx Log Rotation Script ======
==== Overview ====
This Bash script is designed to manage and rotate Nginx log files. It ensures that logs are periodically backed up, cleared, and maintained efficiently by keeping only the latest backups.
==== Script Functionality ====
=== 1. Define Log File Paths: ===
* Sets up paths for access and error logs.
* Specifies a directory for storing log backups.
=== 2. Verify Log File Existence: ===
* Checks if the `access.log` and `error.log` exist.
* If either file is missing, the script exits with an error.
=== 3. Backup Logs :===
* Creates a timestamp (`YYYYMMDDHHMMSS`) for log backups.
* Copies the current logs to the backup directory with a timestamp suffix.
=== 4. Verify Backup Success: ===
* Ensures the backup process was successful.
* If the backup fails, the script exits with an error.
=== 5. Clear Log Files: ===
* Empties the access and error logs to free up space.
=== 6. Backup Retention Management: ===
* Keeps only the latest 7 backups.
* Deletes older backups beyond the set limit.
==== Script Link ====
* You can find the Script on Gitlab via this [[https://git.elosys.net/sys/bin/-/blob/feature_nginx_rotation/nginx_rotation.sh?ref_type=heads|link]]
==== Usage Instructions ====
- Clone the Scrlipt via the git link
$ git clone git@git.elosys.net:sys/bin.git
- Make it executable
sudo chmod +x nginx_rotation.sh
- Automate it with a cron job to run periodically
sudo cronjob -e
* you can add this line which executes the script every third day :
40 1 */3 * * /home/admin/bin/gitlab_log_rotation.sh
==== Conclusion ====
This script automates the log rotation process for Nginx logs by ensuring efficient backup and cleanup. By maintaining only the last 7 backups, it helps manage disk space while ensuring log history is available when needed.
----
--- //[[nadirhabib96@gmail.com|Nadir Habib]] 2025/02/28 20:57//