public function prepareList($page_size = 5)
 {
     if ($page_size) {
         $sql = "SELECT COUNT(*)\n\t\t\t\t\tFROM `" . BackupOldDataTx::model()->tableName() . "` \n\t\t\t\t\tORDER BY `backup_date` DESC";
         $total = Yii::app()->db->createCommand($sql)->queryScalar();
         $pages = new CPagination($total);
         $pages->pageSize = $page_size;
         //$pages->applyLimit($criteria);
     }
     $sql = "SELECT *\n\t\t\t\tFROM `" . BackupOldDataTx::model()->tableName() . "` \n\t\t\t\tORDER BY `backup_date` DESC";
     if ($page_size) {
         $sql .= " LIMIT " . $pages->currentPage * $pages->pageSize . ", " . $pages->pageSize;
     }
     $res = Yii::app()->db->createCommand($sql)->queryAll();
     if ($res) {
         $ids = array();
         foreach ($res as $key => $value) {
             $ids[] = $value['id'];
         }
         $sql = "SELECT * \n\t\t\t\t\tFROM `" . BackupOldDataTxLog::model()->tableName() . "` \n\t\t\t\t\tWHERE `backup_id` IN (" . implode(',', $ids) . ") \n\t\t\t\t\tORDER BY `created` ASC";
         $res2 = Yii::app()->db->createCommand($sql)->queryAll();
         if ($res2) {
             foreach ($res2 as $key => $value) {
                 $res2prepared[$value['backup_id']][] = $value;
             }
             foreach ($res as $key => $value) {
                 $res[$key]['logs'] = $res2prepared[$value['id']] ? $res2prepared[$value['id']] : array();
             }
         }
     }
     return array('list' => $res, 'pages' => $pages);
 }
Example #2
0
 public function actionDbsetup()
 {
     // #############################################################################
     $backups_path = Yii::app()->params['backups_path'];
     if (isset($_REQUEST['apply'])) {
         $apply = trim($_REQUEST['apply']);
         if (file_exists($backups_path . DIRECTORY_SEPARATOR . $apply)) {
             ini_set('memory_limit', '-1');
             set_time_limit(0);
             $sql = file_get_contents($backups_path . DIRECTORY_SEPARATOR . $apply);
             //use long or short db
             if (stripos($apply, 'long')) {
                 $res = Yii::app()->db_long->createCommand($sql)->query();
             } else {
                 $res = Yii::app()->db->createCommand($sql)->query();
             }
             It::memStatus('admin_backup_applied');
             $this->redirect($this->createUrl('admin/dbsetup'));
         }
     }
     if (isset($_REQUEST['delete'])) {
         $delete = trim($_REQUEST['delete']);
         if (file_exists($backups_path . DIRECTORY_SEPARATOR . $delete)) {
             unlink($backups_path . DIRECTORY_SEPARATOR . $delete);
             It::memStatus('admin_backup_deleted');
             $this->redirect($this->createUrl('admin/dbsetup'));
         }
     }
     if (isset($_REQUEST['create'])) {
         $backup_path = dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'backups';
         if (It::isLinux()) {
             $backup_path .= DIRECTORY_SEPARATOR . '`date +%Y_%m_%d`.sql';
         } else {
             if (It::isWindows()) {
                 $backup_path .= DIRECTORY_SEPARATOR . '%DATE:~7,4%_%DATE:~3,2%_%DATE:~0,2%.sql';
             }
         }
         $command = Yii::app()->params['applications']['mysqldump_exe_path'] . ' --user="******"' . ' --password="******"' . ' --result-file="' . $backup_path . '" ' . Yii::app()->params['db_params']['dbname'];
         if (DOCKER) {
             $backup_name = '`date +%Y_%m_%d`.sql';
             $command = 'echo ' . $backup_name . ' >' . dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'backups' . DIRECTORY_SEPARATOR . 'new';
         }
         set_time_limit(1800);
         // 30min
         $output = null;
         $return = null;
         exec($command, $output, $return);
         if ($return == 0) {
             It::memStatus('admin_backup_refreshed');
             $this->redirect($this->createUrl('admin/dbsetup'));
         }
     }
     if (isset($_REQUEST['create_long'])) {
         $backup_path = dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'backups';
         if (It::isLinux()) {
             $backup_path .= DIRECTORY_SEPARATOR . '`date +%Y_%m_%d`_long.sql';
         } else {
             if (It::isWindows()) {
                 $backup_path .= DIRECTORY_SEPARATOR . '%DATE:~7,4%_%DATE:~3,2%_%DATE:~0,2%_long.sql';
             }
         }
         $command = Yii::app()->params['applications']['mysqldump_exe_path'] . ' --user="******"' . ' --password="******"' . ' --result-file="' . $backup_path . '" ' . Yii::app()->params['db_long_params']['dbname'];
         if (DOCKER) {
             $backup_name = '`date +%Y_%m_%d`_long.sql';
             $command = 'echo ' . $backup_name . ' >' . dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'backups' . DIRECTORY_SEPARATOR . 'new_long';
         }
         set_time_limit(1800);
         // 30min
         $output = null;
         $return = null;
         exec($command, $output, $return);
         if ($return == 0) {
             It::memStatus('admin_backup_refreshed');
             $this->redirect($this->createUrl('admin/dbsetup'));
         }
     }
     $outputs = null;
     if (It::isLinux()) {
         $cmd = 'ls -1At ' . $backups_path . ' | egrep -i *.sql';
         exec($cmd, $outputs);
     } else {
         if (It::isWindows()) {
             exec('dir ' . $backups_path . DIRECTORY_SEPARATOR . '*.sql /B /4 /T:C /O:D', $outputs);
             array_slice($outputs, 0, -1);
         }
     }
     $backups = array();
     foreach ($outputs as $output) {
         $backups[] = array('filename' => $output, 'created' => filemtime($backups_path . DIRECTORY_SEPARATOR . $output));
     }
     // #############################################################################
     $settings = Settings::model()->find();
     if (is_null($settings)) {
         $settings = new Settings();
     }
     $settings->scenario = 'dbexport';
     if (Yii::app()->request->isPostRequest && isset($_POST['Settings'])) {
         $settings->attributes = $_POST['Settings'];
         if (!$settings->attributes['db_exp_enabled']) {
             Settings::model()->updateByPk(1, array('db_exp_enabled' => 0));
             It::memStatus('admin_dbexport_settings_saved');
             $this->redirect($this->createUrl('admin/dbsetup'));
         } else {
             if ($settings->validate()) {
                 if ($settings->save(false)) {
                     It::memStatus('admin_dbexport_settings_saved');
                     $this->redirect($this->createUrl('admin/dbsetup'));
                 }
             }
         }
     }
     // #############################################################################
     $res = BackupOldDataTxLog::model()->prepareList();
     // ###########################################################################
     $this->render('dbsetup', array('backups' => $backups, 'settings' => $settings, 'list' => $res['list'], 'pages' => $res['pages']));
 }
Example #3
0
 private function addBackupLog($msg)
 {
     $log = new BackupOldDataTxLog();
     $log->backup_id = $this->current_backup->id;
     $log->comment = $msg;
     $log->save();
 }