126 lines
3.1 KiB
PHP
126 lines
3.1 KiB
PHP
<?php
|
|
/**
|
|
* reduce file size of all databases
|
|
*/
|
|
|
|
if (!COCKPIT_CLI) return;
|
|
|
|
$emptyTrash = $app->param('trash', false);
|
|
$deleteAllRevisions = $app->param('delete-all-revisions', false);
|
|
$cleanAll = $app->param('all', false);
|
|
$noVacuum = $app->param('no-vacuum', false);
|
|
|
|
if ($cleanAll) {
|
|
$emptyTrash = true;
|
|
$deleteAllRevisions = true;
|
|
}
|
|
|
|
$server = $app->retrieve('database/server');
|
|
|
|
// TODO: add support for MongoDB
|
|
if (strpos($server, 'mongolite://') !== 0) {
|
|
CLI::writeln('The cleanup script is only compatible with the Mongolite client', false);
|
|
return;
|
|
} else {
|
|
$server = str_replace('mongolite://', '', $server);
|
|
}
|
|
|
|
$client = new \Mongolite\Client($server);
|
|
$fs = $app->helper('fs');
|
|
$revisions = $app->helper('revisions');
|
|
|
|
/**
|
|
* store old db sizes for comparison
|
|
*/
|
|
$oldSizes = [];
|
|
foreach ($fs->ls('*.sqlite', '#data:') as $file) {
|
|
$name = $file->getBasename('.sqlite');
|
|
$oldSizes[$name]['size'] = $file->getSize();
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Delete old revisions
|
|
*/
|
|
if ($deleteAllRevisions) {
|
|
$app->storage->dropCollection('cockpit/revisions');
|
|
CLI::writeln('Deleted all revisions', true);
|
|
}
|
|
else {
|
|
|
|
// TODO: delete revisions, except last <n>
|
|
// TODO: delete revisions, if older than <date>
|
|
// TODO: also delete revisions of singletons
|
|
|
|
// $collections = $client->selectDB('collections')->getCollectionNames();
|
|
//
|
|
// foreach ($collections as $collection) {
|
|
//
|
|
// $options = [
|
|
// 'fields' => [
|
|
// '_id' => true,
|
|
// ],
|
|
// ];
|
|
//
|
|
// $entries = $app->storage->find("collections/{$collection}", $options)->toArray();
|
|
//
|
|
// foreach ($entries as $entry) {
|
|
//
|
|
// $count = $revisions->count($entry['_id']);
|
|
//
|
|
// if ($count) {
|
|
// $revs = $revisions->getList($entry['_id'], 0, 0);
|
|
//
|
|
// foreach ($revs as $rev) {
|
|
// foreach ($rev as $k => $v) {
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
}
|
|
|
|
/**
|
|
* empty trash
|
|
*/
|
|
if ($emptyTrash) {
|
|
$app->storage->dropCollection('collections/_trash');
|
|
CLI::writeln('Deleted all trashed collection items', true);
|
|
}
|
|
|
|
/**
|
|
* vacuum all databases
|
|
*/
|
|
if (!$noVacuum) {
|
|
foreach ($client->listDBs() as $db) {
|
|
$client->selectDB($db)->vacuum();
|
|
CLI::writeln("Vacuumed {$db}", true);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Compare sizes after cleanup
|
|
*/
|
|
$newSizes = [];
|
|
|
|
$mask = '%-18s %-9s %-9s %-9s';
|
|
CLI::writeln(sprintf($mask, 'name', 'size old', 'size new', 'diff'));
|
|
|
|
foreach ($fs->ls('*.sqlite', '#data:') as $file) {
|
|
|
|
$name = $file->getBasename('.sqlite');
|
|
$newSizes[$name]['size'] = $file->getSize();
|
|
|
|
$diff = $oldSizes[$name]['size'] - $newSizes[$name]['size'];
|
|
$diffFormat = !$diff ? 0 : $app->helper('utils')->formatSize($diff);
|
|
|
|
$oldSizeFormat = $app->helper('utils')->formatSize($oldSizes[$name]['size']);
|
|
$newSizeFormat = $app->helper('utils')->formatSize($newSizes[$name]['size']);
|
|
|
|
CLI::writeln(sprintf($mask, $name, $oldSizeFormat, $newSizeFormat, $diffFormat));
|
|
|
|
}
|
|
|
|
CLI::writeln('Database cleanup done', true);
|