PHP code example of onlyphp / codeigniter3-housekeeping
1. Go to this page and download the library: Download onlyphp/codeigniter3-housekeeping library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
onlyphp / codeigniter3-housekeeping example snippets
$autoload['libraries'] = array('DatabaseArchiver');
$archiver = new DatabaseArchiver();
$result = $archiver
->driver('mysql')
->backupFrom('orders')
->primaryKey('order_id')
->condition('created_at < DATE_SUB(NOW(), INTERVAL 6 MONTH)')
->mode('BO') // Backup Only
->run();
print_r($result);
$archiver = new DatabaseArchiver();
$result = $archiver
->driver('mysql')
->backupFrom('orders')
->primaryKey('order_id')
->uniqueColumns(['order_number'])
->condition('status = "completed" AND created_at < DATE_SUB(NOW(), INTERVAL 1 YEAR)')
->mode('BP') // Backup and Purge
->chunk(500) // Process 500 records at a time
->run();
// Verify archive integrity
$verification = $archiver->verifyArchive();
print_r($verification);
$archiver = new DatabaseArchiver();
$result = $archiver
->driver('mysql')
->backupFrom('transactions')
->backupTo('transactions_archive_2023')
->primaryKey('transaction_id')
->condition('YEAR(transaction_date) = 2023')
->mode('BO')
->parallel(4) // Use 4 parallel processes
->chunk(1000)
->run();
// Monitor progress
$progress = $archiver->getProgress();
print_r($progress);
$archiver = new DatabaseArchiver();
$result = $archiver
->driver('oci')
->backupFrom('EMPLOYEES')
->primaryKey('EMPLOYEE_ID')
->condition('TERMINATION_DATE IS NOT NULL')
->mode('BP')
->onDebug() // Enable debug mode
->logPath('/custom/path/archive.log')
->sqlHint('/*+ PARALLEL(4) */') // Oracle-specific hint
->run();
$archiver = new DatabaseArchiver();
$result = $archiver
->driver('mysql')
->backupFrom('system_logs')
->primaryKey('log_id')
->condition('created_at < DATE_SUB(NOW(), INTERVAL 3 MONTH)')
->mode('PO') // Purge Only
->chunk(200) // Smaller chunks to manage memory
->run();
class ArchiveManager {
private $archiver;
private $maxAttempts = 3;
private $progressCheckInterval = 5; // seconds
public function __construct() {
$this->archiver = new DatabaseArchiver();
}
public function runArchiveProcess() {
try {
// Configure the archiver
$this->archiver
->driver('mysql')
->backupFrom('orders')
->primaryKey('order_id')
->condition('created_at < DATE_SUB(NOW(), INTERVAL 6 MONTH)')
->mode('BP')
->chunk(1000)
->onDebug();
// Start the archiving process
$processId = uniqid('archive_');
log_message('info', "Starting archive process: {$processId}");
// Run the archiving process
$result = $this->archiver->run();
// Monitor progress until completion
$this->monitorProgress();
// Verify the archive
$verification = $this->verifyArchiveWithRetry();
return [
'process_id' => $processId,
'archive_result' => $result,
'verification' => $verification
];
} catch (Exception $e) {
log_message('error', "Archive process failed: " . $e->getMessage());
throw $e;
}
}
private function monitorProgress() {
$completed = false;
$lastPercentage = 0;
while (!$completed) {
$progress = $this->archiver->getProgress();
if ($progress['percentage_complete'] != $lastPercentage) {
$this->displayProgress($progress);
$lastPercentage = $progress['percentage_complete'];
}
if ($progress['percentage_complete'] >= 100) {
$completed = true;
echo "\nArchiving process completed!\n";
} else {
sleep($this->progressCheckInterval);
}
}
}
private function displayProgress($progress) {
$barLength = 50;
$completed = round(($progress['percentage_complete'] * $barLength) / 100);
$remaining = $barLength - $completed;
$progressBar = "[" .
str_repeat("=", $completed) .
">" .
str_repeat(" ", $remaining) .
"]";
echo sprintf(
"\rProgress: %s %d%% (%d/%d records)",
$progressBar,
$progress['percentage_complete'],
$progress['archived_records'],
$progress['total_records']
);
}
private function verifyArchiveWithRetry() {
$attempts = 0;
$success = false;
$lastError = null;
while ($attempts < $this->maxAttempts && !$success) {
try {
$verification = $this->archiver->verifyArchive();
if ($verification['integrity_status'] === 'ok_complete') {
return $verification;
}
// Handle different verification statuses
switch ($verification['integrity_status']) {
case 'warning_duplicates_found':
throw new Exception("Duplicate records found in archive");
case 'warning_incomplete_backup':
throw new Exception("Incomplete backup detected");
case 'warning_count_mismatch':
throw new Exception("Record count mismatch");
}
$success = true;
return $verification;
} catch (Exception $e) {
$lastError = $e;
$attempts++;
if ($attempts < $this->maxAttempts) {
sleep(5); // Wait before retry
}
}
}
if (!$success) {
throw new Exception(
"Archive verification failed after {$this->maxAttempts} attempts. " .
"Last error: " . $lastError->getMessage()
);
}
}
}
// Usage example:
try {
$manager = new ArchiveManager();
$result = $manager->runArchiveProcess();
echo "\n\nArchive Process Summary:\n";
echo "Process ID: {$result['process_id']}\n";
echo "Records Processed: {$result['archive_result']['processed']}\n";
echo "Execution Time: {$result['archive_result']['execution_time']}\n";
echo "Memory Usage: {$result['archive_result']['memory']['used']}\n";
echo "Verification Status: {$result['verification']['integrity_status']}\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
// Get current progress
$progress = $archiver->getProgress();
// Verify archive integrity
$verification = $archiver->verifyArchive();