/** * Execute the console command. * * @return mixed */ public function fire() { // $connection = \Config::get('database.default'); $u = \Config::get('database.connections.' . $connection . '.username'); $p = \Config::get('database.connections.' . $connection . '.password'); $d = \Config::get('database.connections.' . $connection . '.database'); $dir = storage_path('backups/' . date('Y/m/Y-m-d_H-i-s')); $f = date('Y-m-d_H-i-s') . '.sql'; $sql_file = $dir . '/' . $f; if (!file_exists($dir)) { mkdir($dir, 0777, true); } $command = sprintf('mysqldump -u%s %s %s > %s', $u, $p ? '-p' . $p : '', $d, $sql_file); exec($command); $zip_file = storage_path('backups/' . date('Y/m/Y-m-d_H-i-s')) . '.zip'; $zip = new \ZipArchiveEx(); $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE); $zip->addFromString($f, file_get_contents($sql_file)); $zip->close(); unlink($sql_file); rmdir($dir); $this->question('Бэкап создан!'); $this->question($zip_file); }
/** * @dataProvider provideTestDirs */ public function testAddDir($dirname, $expected_result, $manipulate, $zipContentsOnly = false) { # Create new archive $archive = '/tmp/archive.zip'; $zip = new ZipArchiveEx(); $zip->open($archive, ZIPARCHIVE::OVERWRITE); # Try to add directory: if ($zipContentsOnly) { $result = $zip->addDirContents($dirname); } else { $result = $zip->addDir($dirname); } $this->assertEquals($expected_result, $result); # Close archive: $zip->close(); # If directory was added successfully if ($result) { # Remove extracted testdirectory from # former testruns: $extractionDir = self::$tmpDir . '/' . basename($dirname); FileSystemManager::rrmdir($extractionDir); # Extract directory $output = array(); # -u Option forces update of already existing files, # importang for testing on travis-ci.org! $extractTo = $zipContentsOnly ? $extractionDir : self::$tmpDir; exec('unzip -u ' . $archive . ' -d ' . $extractTo, $output, $result); $this->assertEquals(0, $result); # 0 = successfull # $manipulate holds the file to manipulate, # so the following assertion fails. if ($manipulate) { file_put_contents($extractionDir . '/' . $manipulate, 'Lorem ipsum dolor sit amet.'); $expected_result = 1; } else { $expected_result = 0; } # Compare extracted directory and original one exec('diff -arq ' . $dirname . ' ' . $extractionDir, $output, $result); LogMore::debug('Output of diff-command: %s', implode(PHP_EOL, $output)); LogMore::debug('Expecting %d, got: %d', $expected_result, $result); $this->assertEquals($expected_result, $result); } }
public function RestoreFromZip($sZipFile, $sEnvironment = 'production') { $this->LogInfo("Starting restore of " . basename($sZipFile)); $oZip = new ZipArchiveEx(); $res = $oZip->open($sZipFile); // Load the database // $sDataDir = tempnam(SetupUtils::GetTmpDir(), 'itop-'); unlink($sDataDir); // I need a directory, not a file... SetupUtils::builddir($sDataDir); // Here is the directory $oZip->extractTo($sDataDir, 'itop-dump.sql'); $sDataFile = $sDataDir . '/itop-dump.sql'; $this->LoadDatabase($sDataFile); unlink($sDataFile); // Update the code // $sDeltaFile = APPROOT . 'data/' . $sEnvironment . '.delta.xml'; if ($oZip->locateName('delta.xml') !== false) { // Extract and rename delta.xml => <env>.delta.xml; file_put_contents($sDeltaFile, $oZip->getFromName('delta.xml')); } else { @unlink($sDeltaFile); } if (is_dir(APPROOT . 'data/production-modules/')) { SetupUtils::rrmdir(APPROOT . 'data/production-modules/'); } if ($oZip->locateName('production-modules/') !== false) { $oZip->extractDirTo(APPROOT . 'data/', 'production-modules/'); } $sConfigFile = APPROOT . 'conf/' . $sEnvironment . '/config-itop.php'; @chmod($sConfigFile, 0770); // Allow overwriting the file $oZip->extractTo(APPROOT . 'conf/' . $sEnvironment, 'config-itop.php'); @chmod($sConfigFile, 0444); // Read-only $oEnvironment = new RunTimeEnvironment($sEnvironment); $oEnvironment->CompileFrom($sEnvironment); }
/** * Helper to create a ZIP out of a data file and the configuration file */ protected function DoZip($aFiles, $sZipArchiveFile) { foreach ($aFiles as $aFile) { $sFile = $aFile['source']; if (!is_file($sFile) && !is_dir($sFile)) { throw new BackupException("File '{$sFile}' does not exist or could not be read"); } } // Make sure the target path exists $sZipDir = dirname($sZipArchiveFile); SetupUtils::builddir($sZipDir); $oZip = new ZipArchiveEx(); $res = $oZip->open($sZipArchiveFile, ZipArchive::CREATE | ZipArchive::OVERWRITE); if ($res === TRUE) { foreach ($aFiles as $aFile) { if (is_dir($aFile['source'])) { $oZip->addDir($aFile['source'], $aFile['dest']); } else { $oZip->addFile($aFile['source'], $aFile['dest']); } } if ($oZip->close()) { $this->LogInfo("Archive: {$sZipArchiveFile} created"); } else { $this->LogError("Failed to save zip archive: {$sZipArchiveFile}"); throw new BackupException("Failed to save zip archive: {$sZipArchiveFile}"); } } else { $this->LogError("Failed to create zip archive: {$sZipArchiveFile}."); throw new BackupException("Failed to create zip archive: {$sZipArchiveFile}."); } }
protected function __construct(Menu $xMenu) { $this->menu = $xMenu; $rewp = new ReWP(WP_VAGRANTIZE_HOME . COMPOSER_DIR . '/amekusa/ReWP'); $this->actions = array(new AjaxAction('saveSettings', function () use($rewp) { if (!$_POST) { wp_send_json_error(); } if (!array_key_exists('data', $_POST)) { wp_send_json_error(); } $data = array(); parse_str($_POST['data'], $data); $rewp->setData($data); $dest = $rewp->exportData(); if (!$dest) { wp_send_json_error(); } else { $time = filemtime($dest); wp_send_json_success(array('file' => $dest, 'date' => date(get_option('time_format') . ', ' . get_option('date_format'), $time), 'datetime' => date(DATE_W3C, $time))); } }), new AjaxAction('resetSettings', function () use($rewp) { if (!$rewp->reset()) { wp_send_json_error(); } else { wp_send_json_success(); } }), new AjaxAction('renderSettingsTable', function () use($rewp) { $data = $rewp->getData(); ob_start(); include __DIR__ . '/view/SettingsTable.php'; wp_send_json_success(ob_get_clean()); }), new AjaxAction('download', function () use($rewp) { $name = $rewp->getData('hostname'); if (!$name) { $name = $rewp->getData('ip'); } if (!$name) { $name = 'vagrantme.up'; } $name = preg_replace('/^[^a-zA-Z0-9_-]+/', '', $name); $name = preg_replace('/[^a-zA-Z0-9_-]+$/', '', $name); $name = preg_replace('/[^a-zA-Z0-9_.-]/', '_', $name); $dest = WP_VAGRANTIZE_HOME . ".exports/{$name}." . date('YmdHis') . '.zip'; $zip = new \ZipArchiveEx(); $zip->open($dest, \ZipArchive::OVERWRITE); $zip->addDirContents($rewp->getPath()); $db = ''; if ($rewp->getData('import_sql')) { $db = $rewp->exportDB(WP_VAGRANTIZE_HOME . '.exports'); $zip->addFile($db, basename($db)); } $zip->close(); if ($db) { unlink($db); } $time = filemtime($dest); wp_send_json_success(array('file' => $dest, 'fileUrl' => WP_VAGRANTIZE_URL . 'download.php?file=' . basename($dest), 'date' => date(get_option('time_format') . ', ' . get_option('date_format'), $time), 'datetime' => date(DATE_W3C, $time))); })); // @formatter:on foreach ($this->actions as $iAct) { $iAct->register(); } add_action('load-' . $this->getId(), array($this, 'setup')); }
public function downloadZip($content_id, $file_id) { $file = File::find($file_id); if (!$file || !in_array($content_id, $file->content_ids)) { return redirect('/'); } $photo_dir = $file->server_path; if (!\File::isDirectory(storage_path() . '/download_content/')) { \File::makeDirectory(storage_path() . '/download_content/', 0777); } $zipfile = $file_id . ".zip"; $zip = new \ZipArchiveEx(); $zip->open(storage_path() . '/download_content/' . $zipfile, \ZIPARCHIVE::CREATE); $source = $photo_dir; $files = \File::files($source); foreach ($files as $file) { $file = str_replace($source . '/', '', $file); $imagepath = $source . "/" . $file; $zip->addFile($imagepath, $file); } $zip->close(); $ext = ".zip"; $pathToFile = storage_path() . '/download_content/' . $zipfile; $name = $this->createFileName($content_id, $zipfile); $this->saveDownloadInfo($content_id, $file_id, 'zip', $pathToFile); return response()->download($pathToFile, $name)->deleteFileAfterSend(true); }