/** * Backup database. */ public function showBackup() { $this->showMainMenu(); $tables = array(); $selectedTables = array(); foreach (Curry_Propel::getModels() as $package => $classes) { $selectedTables = array_merge($selectedTables, array_values($classes)); $tables[$package] = array(); foreach ($classes as $table) { $tables[$package][$table] = $table; } } $form = new Curry_Form(array('action' => url('', array("module", "view", "page_id")), 'method' => 'post', 'elements' => array('tables' => array('multiselect', array('label' => 'Tables', 'multiOptions' => $tables, 'value' => $selectedTables, 'size' => 15)), 'name' => array('text', array('label' => 'Name', 'required' => true, 'value' => 'backup_%Y-%m-%d_%H-%M-%S.txt', 'description' => 'Name of the file, strftime() is used to format the string.')), 'type' => array('radio', array('label' => 'Where do you want to save?', 'multiOptions' => array('online' => 'Save online', 'local' => 'Save to local file'), 'value' => 'online'))))); $form->addElement('submit', 'Go'); if (isPost() && ($_POST['tables'] == '*' || $_POST['tables'] == array('*'))) { $_POST['tables'] = $selectedTables; } if (isPost() && $form->isValid($_POST)) { $values = $form->getValues(); if ($values['type'] == 'local') { // dump to temp, stream to client $fp = fopen("php://temp", 'r+'); Curry_Backend_DatabaseHelper::dumpDatabase($fp, $values['tables'], $this); rewind($fp); $name = Curry_String::getRewriteString(Curry_Core::$config->curry->name) . '-db.txt'; Curry_Application::returnData($fp, 'application/octet-stream', $name); } else { if ($values['type'] == 'online') { $filename = Curry_Backend_DatabaseHelper::createBackupName($values['name']); $status = Curry_Backend_DatabaseHelper::dumpDatabase($filename, $values['tables'], $this); $this->addMessage('Backup created ' . $filename, $status ? self::MSG_SUCCESS : self::MSG_ERROR); } } } $this->addMainContent($form); }
/** * Create an archive of the project. */ public function showBundle() { $this->addMainMenu(); $this->addMessage('You can install this bundle using <a href="' . url('', array('module', 'view' => 'InstallScript')) . '">this installation script</a>.', self::MSG_NOTICE, false); $form = new \Curry_Form(array('action' => url('', array("module", "view")), 'method' => 'post', 'elements' => array('project' => array('checkbox', array('label' => 'Project', 'value' => true)), 'www' => array('checkbox', array('label' => 'WWW folder', 'value' => true)), 'vendor' => array('checkbox', array('label' => 'Vendor', 'value' => true)), 'database' => array('checkbox', array('label' => 'Database', 'value' => true)), 'compression' => array('select', array('label' => 'Compression', 'multiOptions' => array(Archive::COMPRESSION_NONE => 'None', Archive::COMPRESSION_GZ => 'Gzip'))), 'save' => array('submit', array('label' => 'Create bundle'))))); if (isPost() && $form->isValid($_POST)) { // create archive @set_time_limit(0); $compression = $form->compression->getValue(); $tar = new Archive('', $compression); // set up file list $options = array(array('pattern' => '*.svn*', 'pattern_subject' => 'path', 'skip' => true), array('pattern' => '*.git*', 'pattern_subject' => 'path', 'skip' => true), array('pattern' => '.DS_Store', 'skip' => true), array('pattern' => 'Thumbs.db', 'skip' => true), array('pattern' => '._*', 'skip' => true)); if ($form->project->isChecked()) { $tar->add($this->app['projectPath'], 'cms/', array_merge($options, array(array('path' => 'data/', 'pattern' => 'data/*/*', 'pattern_subject' => 'path', 'skip' => true)))); } if ($form->www->isChecked()) { $tar->add($this->app['wwwPath'], 'www/', $options); } if ($form->vendor->isChecked()) { $tar->add($this->app['projectPath'] . '/../vendor', 'vendor/', $options); } if ($form->database->isChecked()) { $fiveMBs = 5 * 1024 * 1024; $fp = fopen("php://temp/maxmemory:{$fiveMBs}", 'r+'); if (!\Curry_Backend_DatabaseHelper::dumpDatabase($fp)) { throw new \Exception('Aborting: There was an error when dumping the database.'); } fseek($fp, 0); $tar->addString('db.txt', stream_get_contents($fp)); fclose($fp); } $filename = str_replace(" ", "_", $this->app['name']) . "-bundle-" . date("Ymd") . ".tar" . ($compression ? ".{$compression}" : ''); header("Content-type: " . Archive::getCompressionMimeType($compression)); header("Content-disposition: attachment; filename=" . StringHelper::escapeQuotedString($filename)); // do not use output buffering while (ob_end_clean()) { } $tar->stream(); exit; } $this->addMainContent($form); return parent::render(); }
/** * Create an automatic backup of the database. */ public function doAutoBackup() { $autoBackup = Curry_Core::$config->curry->autoBackup; if ($autoBackup) { $filename = Curry_Backend_DatabaseHelper::createBackupName("backup_%Y-%m-%d_%H-%M-%S_autobackup.txt"); $lastModified = 0; foreach (new DirectoryIterator(dirname($filename)) as $entry) { if ($entry->isFile()) { $lastModified = max($lastModified, $entry->getMTime()); } } if (time() - $lastModified >= $autoBackup && !file_exists($filename)) { $status = Curry_Backend_DatabaseHelper::dumpDatabase($filename); if ($this->backend) { if ($status) { $this->backend->addMessage('An automatic backup of the database has been created successfully.', Curry_Backend::MSG_SUCCESS); } else { $this->backend->addMessage('There was an error when trying to create the automatic backup of the database.', Curry_Backend::MSG_ERROR); } } } } }