Files
ubicloud/rhizome/host/bin/delete-old-serial-logs
Furkan Sahin bfbf33c0ee Add cron job to clean-up serial.log files older than 1hr
We need to clean the serial.log files. This is both a security measure
and a practice to avoid disk full in the host. We take 2 measures:

1. Delete serial.log files if they are older than 1 hr.
2. If the directory is larger than 1 GB, delete files starting from the
largest one until we get to less than 1 GB.
2024-02-13 12:16:36 +01:00

25 lines
740 B
Ruby
Executable File

#!/bin/env ruby
# frozen_string_literal: true
require "fileutils"
directory_path = "/var/log/ubicloud/serials"
# Delete files older than 1 day
Dir.glob(File.join(directory_path, "*")).each do |file|
next unless File.file?(file) # Skip directories
file_age_in_hours = (Time.now - File.mtime(file)) / 3600
FileUtils.rm(file) if file_age_in_hours > 24
end
# Reduce the directory size to less than 1GB
files_with_sizes = Dir.glob(File.join(directory_path, "*"))
.select { File.file?(_1) }
.map { {file: _1, size: File.size(_1)} }
.sort_by { _1[:size] }
while files_with_sizes.sum { _1[:size] } > 1 * 1024 * 1024 * 1024 # 1GB in bytes
break unless (largest_file = files_with_sizes.pop)
FileUtils.rm(largest_file[:file])
end