/** * Cleanup your backup directory. * * @see \phpbu\App\Backup\Cleanup::cleanup() * @param \phpbu\App\Backup\Target $target * @param \phpbu\App\Backup\Collector $collector * @param \phpbu\App\Result $result * @throws \phpbu\App\Backup\Cleaner\Exception */ public function cleanup(Target $target, Collector $collector, Result $result) { $files = $collector->getBackupFiles(); $size = $target->getSize(); /** @var \phpbu\App\Backup\File $file */ foreach ($files as $file) { $size += $file->getSize(); } // backups exceed capacity? if ($size > $this->capacityBytes) { // oldest backups first ksort($files); while ($size > $this->capacityBytes && count($files) > 0) { $file = array_shift($files); $size -= $file->getSize(); if (!$file->isWritable()) { throw new Exception(sprintf('can\'t delete file: %s', $file->getPathname())); } $result->debug(sprintf('delete %s', $file->getPathname())); $file->unlink(); } // deleted all old backups but still exceeding the space limit // delete the currently created backup as well if ($this->deleteTarget && $size > $this->capacityBytes) { $target->unlink(); } } }
/** * Cleanup your backup directory. * * @see \phpbu\App\Backup\Cleanup::cleanup() * @param \phpbu\App\Backup\Target $target * @param \phpbu\App\Backup\Collector $collector * @param \phpbu\App\Result $result * @throws \phpbu\App\Backup\Cleaner\Exception */ public function cleanup(Target $target, Collector $collector, Result $result) { $minTime = time() - $this->offsetSeconds; $files = $collector->getBackupFiles(); /** @var \phpbu\App\Backup\File $file */ foreach ($files as $file) { // last mod date < min date? delete! if ($file->getMTime() < $minTime) { if (!$file->isWritable()) { throw new Exception(sprintf('can\'t delete file: %s', $file->getPathname())); } $result->debug(sprintf('delete %s', $file->getPathname())); $file->unlink(); } } }
/** * Cleanup your backup directory. * * @see \phpbu\App\Backup\Cleanup::cleanup() * @param \phpbu\App\Backup\Target $target * @param \phpbu\App\Backup\Collector $collector * @param \phpbu\App\Result $result * @throws \phpbu\App\Backup\Cleaner\Exception */ public function cleanup(Target $target, Collector $collector, Result $result) { $files = $collector->getBackupFiles(); if ($this->isCapacityExceeded($files)) { // oldest backups first ksort($files); while ($this->isCapacityExceeded($files)) { $file = array_shift($files); $result->debug(sprintf('delete %s', $file->getPathname())); if (!$file->isWritable()) { throw new Exception(sprintf('can\'t delete file: %s', $file->getPathname())); } $result->debug(sprintf('delete %s', $file->getPathname())); $file->unlink(); } } }
/** * @see \phpbu\App\Backup\Check::pass() * @param \phpbu\App\Backup\Target $target * @param string $value * @param \phpbu\App\Backup\Collector $collector * @param \phpbu\App\Result $result * @return boolean * @throws \phpbu\App\Exception */ public function pass(Target $target, $value, Collector $collector, Result $result) { // throws App\Exception if file doesn't exist $backupSize = $target->getSize(); $history = $collector->getBackupFiles(); $historyCount = count($history); $pass = true; if ($historyCount > 0) { // oldest backups first ksort($history); /* @var $prevFile \SplFileInfo */ $prevFile = array_shift($history); $prevSize = $prevFile->getSize(); $diffPercent = Math::getDiffInPercent($backupSize, $prevSize); $pass = $diffPercent < $value; } return $pass; }