├── .gitignore ├── LICENSE ├── Module.php ├── README.md ├── backup-restore.png ├── composer.json ├── controllers └── DefaultController.php ├── index.php ├── models ├── BackupFile.php └── UploadForm.php └── views └── default ├── _list.php ├── index.php ├── restore.php └── upload.php /.gitignore: -------------------------------------------------------------------------------- 1 | /_html 2 | /tmp 3 | .env 4 | .env-dev 5 | .env-prod 6 | 7 | # phpstorm project files 8 | .idea 9 | 10 | # netbeans project files 11 | nbproject 12 | 13 | # zend studio for eclipse project files 14 | .buildpath 15 | .project 16 | .settings 17 | 18 | # windows thumbnail cache 19 | Thumbs.db 20 | 21 | # composer vendor dir 22 | /vendor 23 | 24 | # composer itself is not needed 25 | composer.phar 26 | 27 | # Mac DS_Store Files 28 | .DS_Store 29 | 30 | # apidoc generated documentation 31 | docs-html 32 | 33 | # phpunit itself is not needed 34 | phpunit.phar 35 | 36 | # local phpunit config 37 | /phpunit.xml 38 | 39 | # IDEs 40 | nbproject 41 | .DS_Store 42 | .idea 43 | *.swp 44 | .vagrant 45 | logs 46 | 47 | tests/_output/* 48 | backend 49 | local.php 50 | version 51 | web/assets-prod 52 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /Module.php: -------------------------------------------------------------------------------- 1 | 53 | 'backuprestore' => [ 54 | 'class' => '\oe\modules\backuprestore\Module', 55 | //'layout' => '@admin-views/layouts/main', or what ever layout you use 56 | ... 57 | ... 58 | ], 59 | 60 | make sure you create a writable directory named _backup on app root directory. 61 | 62 | Pretty Url's ```/backuprestore``` 63 | 64 | No pretty Url's ```index.php?r=backuprestore``` 65 | 66 | -------------------------------------------------------------------------------- /backup-restore.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/open-ecommerce/yii2-backuprestore/59d99ac02d56cf798f831810384fa601b5525ca1/backup-restore.png -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "open-ecommerce/yii2-backuprestore", 3 | "description": "Database Backup and Restore functionality", 4 | "type": "yii2-extension", 5 | "keywords": ["yii2","Database", "Backup","Restore"], 6 | "license": "Apache-2.0", 7 | "authors": [ 8 | { 9 | "name": "open-ecommerce.org", 10 | "email": "info@open-ecommerce.org" 11 | } 12 | ], 13 | "require": { 14 | "yiisoft/yii2": "*" 15 | }, 16 | "autoload": { 17 | "psr-4": { 18 | "oe\\modules\\backuprestore\\": "" 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /controllers/DefaultController.php: -------------------------------------------------------------------------------- 1 | module->path)) 27 | $this->_path = $this->module->path; 28 | else 29 | $this->_path = Yii::$app->basePath . '/_backup/'; 30 | 31 | if (!file_exists($this->_path)) { 32 | mkdir($this->_path); 33 | chmod($this->_path, '777'); 34 | } 35 | return $this->_path; 36 | } 37 | 38 | public function execSqlFile($sqlFile) { 39 | $message = "ok"; 40 | 41 | if (file_exists($sqlFile)) { 42 | $sqlArray = file_get_contents($sqlFile); 43 | 44 | $cmd = Yii::$app->db->createCommand($sqlArray); 45 | try { 46 | $cmd->execute(); 47 | } catch (CDbException $e) { 48 | $message = $e->getMessage(); 49 | } 50 | } 51 | return $message; 52 | } 53 | 54 | public function getColumns($tableName) { 55 | $sql = 'SHOW CREATE TABLE ' . $tableName; 56 | $cmd = Yii::$app->db->createCommand($sql); 57 | $table = $cmd->queryOne(); 58 | 59 | $create_query = $table['Create Table'] . ';'; 60 | 61 | $create_query = preg_replace('/^CREATE TABLE/', 'CREATE TABLE IF NOT EXISTS', $create_query); 62 | $create_query = preg_replace('/AUTO_INCREMENT\s*=\s*([0-9])+/', '', $create_query); 63 | if ($this->fp) { 64 | $this->writeComment('TABLE `' . addslashes($tableName) . '`'); 65 | $final = 'DROP TABLE IF EXISTS `' . addslashes($tableName) . '`;' . PHP_EOL . $create_query . PHP_EOL . PHP_EOL; 66 | fwrite($this->fp, $final); 67 | } else { 68 | $this->tables[$tableName]['create'] = $create_query; 69 | return $create_query; 70 | } 71 | } 72 | 73 | public function getData($tableName) { 74 | $sql = 'SELECT * FROM ' . $tableName; 75 | $cmd = Yii::$app->db->createCommand($sql); 76 | $dataReader = $cmd->query(); 77 | 78 | $data_string = ''; 79 | 80 | foreach ($dataReader as $data) { 81 | $itemNames = array_keys($data); 82 | $itemNames = array_map("addslashes", $itemNames); 83 | $items = join('`,`', $itemNames); 84 | $itemValues = array_values($data); 85 | $itemValues = array_map("addslashes", $itemValues); 86 | $valueString = join("','", $itemValues); 87 | $valueString = "('" . $valueString . "'),"; 88 | $values = "\n" . $valueString; 89 | if ($values != "") { 90 | $data_string .= "INSERT INTO `$tableName` (`$items`) VALUES" . rtrim($values, ",") . ";" . PHP_EOL; 91 | } 92 | } 93 | 94 | if ($data_string == '') 95 | return null; 96 | 97 | if ($this->fp) { 98 | $this->writeComment('TABLE DATA ' . $tableName); 99 | $final = $data_string . PHP_EOL . PHP_EOL . PHP_EOL; 100 | fwrite($this->fp, $final); 101 | } else { 102 | $this->tables[$tableName]['data'] = $data_string; 103 | return $data_string; 104 | } 105 | } 106 | 107 | public function getTables($dbName = null) { 108 | $sql = 'SHOW TABLES'; 109 | $cmd = Yii::$app->db->createCommand($sql); 110 | $tables = $cmd->queryColumn(); 111 | return $tables; 112 | } 113 | 114 | public function StartBackup($addcheck = true) { 115 | $this->file_name = $this->path . $this->back_temp_file . date('Y.m.d_H.i.s') . '.sql'; 116 | 117 | $this->fp = fopen($this->file_name, 'w+'); 118 | 119 | if ($this->fp == null) 120 | return false; 121 | fwrite($this->fp, '-- -------------------------------------------' . PHP_EOL); 122 | if ($addcheck) { 123 | fwrite($this->fp, 'SET AUTOCOMMIT=0;' . PHP_EOL); 124 | fwrite($this->fp, 'START TRANSACTION;' . PHP_EOL); 125 | fwrite($this->fp, 'SET SQL_QUOTE_SHOW_CREATE = 1;' . PHP_EOL); 126 | } 127 | fwrite($this->fp, 'SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;' . PHP_EOL); 128 | fwrite($this->fp, 'SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;' . PHP_EOL); 129 | fwrite($this->fp, '-- -------------------------------------------' . PHP_EOL); 130 | $this->writeComment('START BACKUP'); 131 | return true; 132 | } 133 | 134 | public function EndBackup($addcheck = true) { 135 | fwrite($this->fp, '-- -------------------------------------------' . PHP_EOL); 136 | fwrite($this->fp, 'SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;' . PHP_EOL); 137 | fwrite($this->fp, 'SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;' . PHP_EOL); 138 | 139 | if ($addcheck) { 140 | fwrite($this->fp, 'COMMIT;' . PHP_EOL); 141 | } 142 | fwrite($this->fp, '-- -------------------------------------------' . PHP_EOL); 143 | $this->writeComment('END BACKUP'); 144 | fclose($this->fp); 145 | $this->fp = null; 146 | } 147 | 148 | public function writeComment($string) { 149 | fwrite($this->fp, '-- -------------------------------------------' . PHP_EOL); 150 | fwrite($this->fp, '-- ' . $string . PHP_EOL); 151 | fwrite($this->fp, '-- -------------------------------------------' . PHP_EOL); 152 | } 153 | 154 | public function actionCreate() { 155 | $flashError = ''; 156 | $flashMsg = ''; 157 | 158 | $tables = $this->getTables(); 159 | 160 | if (!$this->StartBackup()) { 161 | //render error 162 | $flashError = 'error'; 163 | $flashMsg = 'Some errors creating the file'; 164 | return $this->render('index'); 165 | } 166 | 167 | foreach ($tables as $tableName) { 168 | $this->getColumns($tableName); 169 | } 170 | foreach ($tables as $tableName) { 171 | $this->getData($tableName); 172 | } 173 | $this->EndBackup(); 174 | 175 | $flashError = 'success'; 176 | $flashMsg = 'The file was created !!!'; 177 | 178 | \Yii::$app->getSession()->setFlash($flashError, $flashMsg); 179 | 180 | $this->redirect(array('index')); 181 | } 182 | 183 | public function actionClean($redirect = true) { 184 | $ignore = array('tbl_user', 'tbl_user_role', 'tbl_event'); 185 | $tables = $this->getTables(); 186 | 187 | if (!$this->StartBackup()) { 188 | //render error 189 | Yii::$app->user->setFlash('success', "Error"); 190 | return $this->render('index'); 191 | } 192 | 193 | $message = ''; 194 | 195 | foreach ($tables as $tableName) { 196 | if (in_array($tableName, $ignore)) 197 | continue; 198 | fwrite($this->fp, '-- -------------------------------------------' . PHP_EOL); 199 | fwrite($this->fp, 'DROP TABLE IF EXISTS ' . addslashes($tableName) . ';' . PHP_EOL); 200 | fwrite($this->fp, '-- -------------------------------------------' . PHP_EOL); 201 | 202 | $message .= $tableName . ','; 203 | } 204 | $this->EndBackup(); 205 | 206 | // logout so there is no problme later . 207 | Yii::$app->user->logout(); 208 | 209 | $this->execSqlFile($this->file_name); 210 | unlink($this->file_name); 211 | $message .= ' are deleted.'; 212 | Yii::$app->session->setFlash('success', $message); 213 | return $this->redirect(array('index')); 214 | } 215 | 216 | public function actionDelete($file = null) { 217 | $flashError = ''; 218 | $flashMsg = ''; 219 | 220 | $file = $_GET[0]['filename']; 221 | 222 | $this->updateMenuItems(); 223 | if (isset($file)) { 224 | $sqlFile = $this->path . basename($file); 225 | if (file_exists($sqlFile)) { 226 | unlink($sqlFile); 227 | $flashError = 'success'; 228 | $flashMsg = 'The file ' . $sqlFile . ' was successfully deleted.'; 229 | } else { 230 | $flashError = 'error'; 231 | $flashMsg = 'The file ' . $sqlFile . ' was not found.'; 232 | } 233 | } else { 234 | $flashError = 'error'; 235 | $flashMsg = 'The file ' . $sqlFile . ' was not found.'; 236 | } 237 | \Yii::$app->getSession()->setFlash($flashError, $flashMsg); 238 | $this->redirect(array('index')); 239 | } 240 | 241 | public function actionDownload($file = null) { 242 | $this->updateMenuItems(); 243 | if (isset($file)) { 244 | $sqlFile = $this->path . basename($file); 245 | if (file_exists($sqlFile)) { 246 | $request = Yii::$app->getRequest(); 247 | $request->sendFile(basename($sqlFile), file_get_contents($sqlFile)); 248 | } 249 | } 250 | throw new CHttpException(404, Yii::t('app', 'File not found')); 251 | } 252 | 253 | public function actionIndex() { 254 | //$this->layout = 'column1'; 255 | $this->updateMenuItems(); 256 | $path = $this->path; 257 | $dataArray = array(); 258 | 259 | $list_files = glob($path . '*.sql'); 260 | if ($list_files) { 261 | $list = array_map('basename', $list_files); 262 | sort($list); 263 | foreach ($list as $id => $filename) { 264 | $columns = array(); 265 | $columns['id'] = $id; 266 | $columns['name'] = basename($filename); 267 | $columns['size'] = filesize($path . $filename); 268 | 269 | $columns['create_time'] = date('Y-m-d H:i:s', filectime($path . $filename)); 270 | $columns['modified_time'] = date('Y-m-d H:i:s', filemtime($path . $filename)); 271 | 272 | $dataArray[] = $columns; 273 | } 274 | } 275 | $dataProvider = new ArrayDataProvider(['allModels' => $dataArray]); 276 | return $this->render('index', array( 277 | 'dataProvider' => $dataProvider, 278 | )); 279 | } 280 | 281 | public function actionSyncdown() { 282 | $tables = $this->getTables(); 283 | 284 | if (!$this->StartBackup()) { 285 | //render error 286 | return $this->render('index'); 287 | } 288 | 289 | foreach ($tables as $tableName) { 290 | $this->getColumns($tableName); 291 | } 292 | foreach ($tables as $tableName) { 293 | $this->getData($tableName); 294 | } 295 | $this->EndBackup(); 296 | return $this->actionDownload(basename($this->file_name)); 297 | } 298 | 299 | public function actionRestore($file = null) { 300 | $flashError = ''; 301 | $flashMsg = ''; 302 | 303 | $file = $_GET['filename']; 304 | 305 | $this->updateMenuItems(); 306 | $sqlFile = $this->path . basename($file); 307 | if (isset($file)) { 308 | $sqlFile = $this->path . basename($file); 309 | $flashError = 'success'; 310 | $flashMsg = 'Looks like working :)'; 311 | } else { 312 | $flashError = 'error'; 313 | $flashMsg = 'Problems with the file name'; 314 | } 315 | $this->execSqlFile($sqlFile); 316 | 317 | \Yii::$app->getSession()->setFlash($flashError, $flashMsg); 318 | $this->redirect(array('index')); 319 | } 320 | 321 | public function actionUpload() { 322 | $model = new UploadForm(); 323 | if (isset($_POST['UploadForm'])) { 324 | $model->attributes = $_POST['UploadForm']; 325 | //oe change cUploaded for this 326 | $model->upload_file = UploadedFile::getInstance($model, 'upload_file'); 327 | if ($model->upload_file->saveAs($this->path . $model->upload_file)) { 328 | // redirect to success page 329 | return $this->redirect(array('index')); 330 | } 331 | } 332 | 333 | return $this->render('upload', array('model' => $model)); 334 | } 335 | 336 | protected function updateMenuItems($model = null) { 337 | // create static model if model is null 338 | if ($model == null) 339 | $model = new UploadForm(); 340 | 341 | switch ($this->action->id) { 342 | case 'restore': { 343 | $this->menu[] = array('label' => Yii::t('app', 'View Site'), 'url' => Yii::$app->HomeUrl); 344 | } 345 | case 'create': { 346 | $this->menu[] = array('label' => Yii::t('app', 'List Backup'), 'url' => array('index')); 347 | } 348 | break; 349 | case 'upload': { 350 | $this->menu[] = array('label' => Yii::t('app', 'Create Backup'), 'url' => array('create')); 351 | } 352 | break; 353 | default: { 354 | $this->menu[] = array('label' => Yii::t('app', 'List Backup'), 'url' => array('index')); 355 | $this->menu[] = array('label' => Yii::t('app', 'Create Backup'), 'url' => array('create')); 356 | $this->menu[] = array('label' => Yii::t('app', 'Upload Backup'), 'url' => array('upload')); 357 | $this->menu[] = array('label' => Yii::t('app', 'Restore Backup'), 'url' => array('restore')); 358 | $this->menu[] = array('label' => Yii::t('app', 'Clean Database'), 'url' => array('clean')); 359 | $this->menu[] = array('label' => Yii::t('app', 'View Site'), 'url' => Yii::$app->HomeUrl); 360 | } 361 | break; 362 | } 363 | } 364 | 365 | } 366 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | hola peron -------------------------------------------------------------------------------- /models/BackupFile.php: -------------------------------------------------------------------------------- 1 | 14 | */ 15 | /** 16 | * UploadForm class. 17 | * UploadForm is the data structure for keeping 18 | */ 19 | class BackupFile extends Model 20 | { 21 | public $id ; 22 | public $name ; 23 | public $size ; 24 | public $create_time ; 25 | public $modified_time ; 26 | /** 27 | * Declares the validation rules. 28 | * The rules state that username and password are required, 29 | * and password needs to be authenticated. 30 | */ 31 | public function rules() 32 | { 33 | return array( 34 | array(['id','name','size','create_time','modified_time'], 'required'), 35 | ); 36 | } 37 | 38 | /** 39 | * Declares attribute labels. 40 | */ 41 | public function attributeLabels() 42 | { 43 | return array( 44 | 'name'=>'File Name', 45 | 'size'=>'File Size', 46 | 'create_time'=>'Create Time', 47 | 'modified_time'=>'Modified Time', 48 | ); 49 | } 50 | public static function label($n = 1) { 51 | return Yii::t('app', 'Backup File|Backup Files', $n); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /models/UploadForm.php: -------------------------------------------------------------------------------- 1 | 14 | */ 15 | /** 16 | * UploadForm class. 17 | * UploadForm is the data structure for keeping 18 | */ 19 | class UploadForm extends Model 20 | { 21 | public $upload_file ; 22 | 23 | /** 24 | * Declares the validation rules. 25 | * The rules state that username and password are required, 26 | * and password needs to be authenticated. 27 | */ 28 | public function rules() 29 | { 30 | if(!isset($this->scenario)) 31 | $this->scenario = 'upload'; 32 | 33 | return array( 34 | array('upload_file', 'required'), 35 | ); 36 | } 37 | 38 | /** 39 | * Declares attribute labels. 40 | */ 41 | public function attributeLabels() 42 | { 43 | return array( 44 | 'upload_file'=>'Upload File', 45 | ); 46 | } 47 | public static function label($n = 1) { 48 | return Yii::t('app', 'File|Files', $n); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /views/default/_list.php: -------------------------------------------------------------------------------- 1 | 'install-grid', 12 | 'export' => false, 13 | 'dataProvider' => $dataProvider, 14 | 'resizableColumns' => false, 15 | 'showPageSummary' => false, 16 | 'headerRowOptions' => ['class' => 'kartik-sheet-style'], 17 | 'filterRowOptions' => ['class' => 'kartik-sheet-style'], 18 | 'responsive' => true, 19 | 'hover' => true, 20 | 'panel' => [ 21 | 'heading' => '

Database backup files

', 22 | 'type' => 'primary', 23 | 'showFooter' => false 24 | ], 25 | // set your toolbar 26 | 'toolbar' => [ 27 | ['content' => 28 | Html::a(' Create Backup', ['create'], ['class' => 'btn btn-success create-backup']). ' '. 29 | Html::a(' Upload Backup File', ['upload'], ['class' => 'btn btn-success']), 30 | ], 31 | ], 32 | 'columns' => array( 33 | 'name', 34 | 'size:size', 35 | 'create_time', 36 | 'modified_time:relativeTime', 37 | [ 38 | 'class' => 'kartik\grid\ActionColumn', 39 | 'template' => '{restore_action}', 40 | 'header' => 'Restore DB', 41 | 'buttons' => [ 42 | 'restore_action' => function ($url, $model) { 43 | return Html::a('', $url, [ 44 | 'title' => Yii::t('app', 'Restore this backup'), 45 | ]); 46 | } 47 | ], 48 | 'urlCreator' => function ($action, $model, $key, $index) { 49 | if ($action === 'restore_action') { 50 | $url = Yii::$app->urlManager->createUrl(['backuprestore/default/restore', 'filename' => $model['name']]); 51 | return $url; 52 | } 53 | } 54 | ], 55 | [ 56 | 'class' => 'kartik\grid\ActionColumn', 57 | 'template' => '{delete_action}', 58 | 'header' => 'Delete file', 59 | 'buttons' => [ 60 | 'delete_action' => function ($url, $model) { 61 | return Html::a('', $url, [ 62 | 'title' => Yii::t('app', 'Delete this backup'), 63 | ]); 64 | } 65 | ], 66 | 'urlCreator' => function ($action, $model, $key, $index) { 67 | if ($action === 'delete_action') { 68 | $url = Yii::$app->urlManager->createUrl(array('backuprestore/default/delete', ['filename' => $model['name']])); 69 | return $url; 70 | } 71 | } 72 | ], 73 | ), 74 | ]); 75 | ?> 76 | -------------------------------------------------------------------------------- /views/default/index.php: -------------------------------------------------------------------------------- 1 |
2 | 3 | params ['breadcrumbs'] [] = [ 5 | 'label' => 'Manage', 6 | 'url' => array( 7 | 'index' 8 | ) 9 | ]; 10 | ?> 11 | 12 | session->hasFlash('success')): ?> 13 |
14 | session->getFlash('success'); ?> 15 |
16 | 17 | 18 | 19 |
20 |
21 | render('_list', array( 23 | 'dataProvider' => $dataProvider 24 | )); 25 | ?> 26 |
27 |
28 | 29 |
-------------------------------------------------------------------------------- /views/default/restore.php: -------------------------------------------------------------------------------- 1 | params ['breadcrumbs'] [] = [ 6 | 'label' => 'Manage', 7 | 'url' => array ( 8 | 'index' 9 | ) 10 | ]; 11 | $this->params['breadcrumbs'][]= [ 12 | 'label' => 'Restore', 13 | 'url' => array('restore'), 14 | ];?> 15 | 16 | 17 | widget('bootstrap.widgets.TbButtonGroup', array( 19 | //'buttons'=>$this->actions, 20 | //'type'=>'success', 21 | //'size'=>'mini', 22 | //'htmlOptions'=>array('class'=>'pull-right') 23 | //)); 24 | ?> 25 |

26 | action->id; ?> 27 |

28 | 29 |

30 | 31 |

32 |

33 | 34 | 'btn btn-warning']) ?> 35 | 36 | 37 | HomeUrl)?> 38 |

39 | -------------------------------------------------------------------------------- /views/default/upload.php: -------------------------------------------------------------------------------- 1 | params['breadcrumbs'][]= [ 14 | 'label' => 'Upload', 15 | 'url' => array('upload'), 16 | ];?> 17 | 18 |

Upload

19 | 20 | 21 | ['enctype' => 'multipart/form-data']]); ?> 22 | 23 | 24 | field($model, 'upload_file')->widget(FileInput::classname(), [ 25 | 'options' => ['accept' => 'mysql/*.sql'], 26 | ]); ?> 27 | 28 | 29 |
30 | 'btn btn-success'] 33 | ) ?> 34 |
35 | 36 | 37 | 38 | 39 | --------------------------------------------------------------------------------