protected function execute(array $arguments = array(), array $options = array()) { $time = strftime('%Y%m%d-%H%M%S', time()); $this->log("[{$time}] - Starting backup procedure...\n", nbLogger::COMMENT); $hudsonHome = $arguments['hudsonHome']; if (!is_dir($hudsonHome)) { throw new Exception('Cannot find hudson home directory: ' . $hudsonHome); } $finder = nbFileFinder::create(); // hudson config $files = $finder->setType('file')->relative()->maxdepth(0)->add('*.xml')->in($hudsonHome); $files = array_merge($files, $this->findInSubFolder($hudsonHome, 'users', '*')); $excludeDirs = array(); if (isset($options['builds'])) { $this->log(" + including job builds\n", nbLogger::COMMENT); } else { $excludeDirs[] = 'builds'; } if (isset($options['workspace'])) { $this->log(" + including job workspace\n", nbLogger::COMMENT); } else { $excludeDirs[] = 'workspace'; } $files = array_merge($files, $this->findInSubFolder($hudsonHome, 'jobs', '*', $excludeDirs)); if (isset($options['fingerprints'])) { $this->log(" + including fingerprints\n", nbLogger::COMMENT); $files = array_merge($files, $this->findInSubFolder($hudsonHome, 'fingerprints', '*')); } if (isset($options['usercontent'])) { $this->log(" + including userContent\n", nbLogger::COMMENT); $files = array_merge($files, $this->findInSubFolder($hudsonHome, 'userContent', '*')); } if (isset($options['hudson'])) { $this->log(" + including hudson server\n", nbLogger::COMMENT); $files = array_merge($files, $this->findInSubFolder($hudsonHome, 'war', '*')); $files = array_merge($files, $this->findInSubFolder($hudsonHome, 'plugins', '*')); } $numFiles = count($files); // perform file copy if (isset($options['copy'])) { $backupHome = $options['copy']; if (isset($options['autoname'])) { $backupHome .= '-' . $time; } nbFileSystem::mkdir($backupHome, true); $progress = new nbProgress($numFiles, 8); $this->log("Copying from {$hudsonHome} to {$backupHome} {$numFiles} files... ", nbLogger::COMMENT); foreach ($files as $key => $file) { if (($p = $progress->getProgress($key)) !== null) { $this->log("{$p}% - "); } nbFileSystem::copy($hudsonHome . '/' . $file, $backupHome . '/' . $file); } $this->log("100%\n"); } // create zip file if (isset($options['zip'])) { if (class_exists('ZipArchive')) { $filename = $options['zip']; if (isset($options['autoname'])) { $filename .= '-' . $time; } $filename .= '.zip'; $this->log("Creating ZIP file {$filename} ... ", nbLogger::COMMENT); $zip = new ZipArchive(); if ($zip->open($filename, ZIPARCHIVE::CREATE) !== true) { throw new Exception('[nbBackupCommand:execute] cannot create zip file: ' . $filename); } $progress = new nbProgress($numFiles, 8); foreach ($files as $key => $file) { if (($p = $progress->getProgress($key)) !== null) { $this->log("{$p}% - "); } $zip->addFile($hudsonHome . '/' . $file, $file); } $zip->close(); $this->log("100%\n"); } else { $this->log("Zip support not found\n"); } } $time = strftime('%Y%m%d-%H%M%S', time()); $this->log("[{$time}] - Backup succesful.\n", nbLogger::COMMENT); return true; }
<?php require_once dirname(__FILE__) . '/../../../bootstrap/unit.php'; $t = new lime_test(); $t->comment('nbProgressTest - Test 1'); $progress = new nbProgress(1, 1); $t->is($progress->getProgress(0), 0, '->getProgress() with 0 is always 0'); $t->is($progress->getProgress(1), 100, '->getProgress() with maxValue is always 100'); $progress = new nbProgress(2, 2); $t->is($progress->getProgress(1), 50, '->getProgress() returns 50'); $progress = new nbProgress(50, 4); $t->is($progress->getProgress(0), 0, '->getProgress() with 0 is always 0'); $t->is($progress->getProgress(50), 100, '->getProgress() with maxValue is always 100'); $t->is($progress->getProgress(2), null, '->getProgress() returns null'); $t->is($progress->getProgress(25), 50, '->getProgress() returns 50'); $t->is($progress->getProgress(13), 25, '->getProgress() returns 25'); $t->is($progress->getProgress(38), 75, '->getProgress() returns 25');