<?php
/**
* Backs up queue files for manual execution with limited memory.
*
* @param string $queue_dir The directory containing the queue files.
* @param string $backup_dir The directory to store the backups.
* @param int $max_filesize Maximum size of each backup file in bytes (to limit memory).
*/
function backupQueues(string $queue_dir, string $backup_dir, int $max_filesize): void
{
if (!is_dir($queue_dir)) {
error_log("Error: Queue directory '$queue_dir' does not exist.");
return;
}
if (!is_dir($backup_dir)) {
mkdir($backup_dir, 0777, true); // Create backup directory if it doesn't exist
if (!is_dir($backup_dir)) {
error_log("Error: Could not create backup directory '$backup_dir'.");
return;
}
}
$files = scandir($queue_dir);
if ($files === false) {
error_log("Error: Could not scan queue directory '$queue_dir'.");
return;
}
foreach ($files as $file) {
if ($file == '.' || $file == '..') {
continue;
}
$filepath = $queue_dir . '/' . $file;
if (is_file($filepath)) {
$filename = basename($file); // Get filename without path
$backup_filepath = $backup_dir . '/' . $filename . '_' . time() . '.bak';
$handle = fopen($backup_filepath, 'wb');
if ($handle === false) {
error_log("Error: Could not open backup file '$backup_filepath' for writing.");
continue; // Skip to the next file
}
$file_size = filesize($filepath);
if ($file_size > $max_filesize) {
error_log("Warning: File '$filepath' is larger than the maximum allowed size ($max_filesize bytes). Skipping.");
fclose($handle);
continue;
}
readfile($filepath, $handle); // Copy file contents to backup
fclose($handle);
echo "Backed up: $filepath to $backup_filepath\n";
}
}
}
// Example usage:
// backupQueues('/path/to/your/queues', '/path/to/your/backups', 1024 * 1024); // 1MB limit
?>
Add your comment