Skip to content

Instantly share code, notes, and snippets.

@ostark
Last active August 21, 2020 13:39
Show Gist options
  • Save ostark/cc8411f80fd25b1522cede155b3d80ba to your computer and use it in GitHub Desktop.
Save ostark/cc8411f80fd25b1522cede155b3d80ba to your computer and use it in GitHub Desktop.
<?php
# Run this script once a day via cron
# to persist apache_access.log.1.gz log in ./storage/logs/
$src = '../logs/apache_access.log.1.gz';
$dest_dir = './storage/logs/';
$dest_prefix = 'apache_access.';
$dest_file = $dest_dir . $dest_prefix . date("Y-m-d", strtotime("yesterday")).'.log.gz';
$keep_max_files = 10;
if (!file_exists($src)) {
die("Src file $src does not exist");
}
if (file_exists($dest_file)) {
die("Dest file $dest_file already exist");
}
if (!copy($src, $dest_file)) {
die("Failed to copy");
}
# Clean up older log files
foreach (array_reverse(glob($dest_dir . $dest_prefix . '*')) as $key => $file) {
if ($key > $keep_max_files-2) {
if (unlink(realpath($file))) {
echo PHP_EOL . "Removed $file";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment