src.dualinventive.com/mtinfo/dist/webroot/rc-4.05/scripts/other/tasks/archiver.php

92 lines
3.1 KiB
PHP

<?php
/** \file scripts/other/tasks/archiver.php
* \brief DI webinterface task manager script, database archiver
* \author Rob Schalken, Core|Vision
* \version $Revision: 26247 $
* \date $Date: 2016-02-29 10:40:22 +0100 (Mon, 29 Feb 2016) $
*
* Database archiver
*/
/************************************/
/* Database archiver */
/************************************/
// Acquire mutex
if (db_mutex_acquire(RemoveExtension(basename(__FILE__)) . "_" . $params['task_id'], 0)) {
// Constants
define('ARCHIVER_MAX_RUNTIME', (10 * 60));
// Initial values
$start = microtime(TRUE);
if( isset($params['param']['age']) ) {
// when 'age' is set, then the other parameters are assumed to be present
$source_db = $params['param']['source'];
$target_db = $params['param']['target'];
$age = $params['param']['age'];
$entries = $params['param']['max_entries'];
$delete_from_source = !$params['param']['no_delete'];
if( !$delete_from_source ) DBG("archiver " . $params['task_id'] . ": source data is preserved");
}
else {
// parameters not set; assume old-style archiver
$source_db = "default";
$target_db = "archive";
$age = "40 days ago";
$entries = 10000;
$delete_from_source = TRUE;
}
// Remove entries before this date:
$delete_older = strtotime($age);
// Define tables
$tables = array("log_realtime", "log_zkl", "log_gebruiker", "log_tcp", "log_secure");
for($i=0; $i<sizeof($tables); $i++) {
// Clear total amount
$total_archived_items[$i] = 0;
}
$last_date = array();
DBG("archiver " . $params['task_id'] . ": from " . $source_db . " to " . $target_db . ", date before " . iso8601($delete_older) . ", " . $age);
do {
$total_this_round = 0;
for($i=0; $i<sizeof($tables); $i++) {
// "Move" entries in batches
$archived_items[$i] = db_log_archive($tables[$i], $source_db, $target_db, $delete_older, $entries, $last_date[$i], $delete_from_source);
// Total amount of deleted items
$total_archived_items[$i] += $archived_items[$i];
$total_this_round += $archived_items[$i];
}
} while(((microtime(TRUE) - $start) < ARCHIVER_MAX_RUNTIME) && ($total_this_round != 0));
// End time
$end = microtime(TRUE) - $start;
// Show results
// Clear overall total counter
$total = 0;
for($i=0; $i<sizeof($tables); $i++) {
DBG("archiver " . $params['task_id'] . ": " . sprintf("%-15s", $tables[$i] . ":") . " " . sprintf("%7u", $total_archived_items[$i]) . " entries moved" . ($last_date[$i] ? (", last entry: " . iso8601($last_date[$i])) : ""));
// Add subtotal to overall total
$total += $total_archived_items[$i];
}
DBG("archiver " . $params['task_id'] . ": " . sprintf("%-15s", "total:") . " " . sprintf("%7u", $total) . " entries moved, " . ($total ? (sprintf("%.0F", $total/$end) . " entries/s, ") : "") . sprintf("%.3F", $end) . " s");
// Release mutex
db_mutex_release(RemoveExtension(basename(__FILE__)) . "_" . $params['task_id']);
// Set handled flag
$handled = 1;
}
else {
// Debug info
DBG("Mutex not released: " . $params['task_id']);
}
?>