public static function getLastBackupInfo() { $criteria = new CDbCriteria(); $criteria->order = 'backup_date DESC'; $criteria->limit = 1; return BackupOldDataTx::model()->find($criteria); }
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); }
/** * runs backup process: * gets last backup info to identify if need to run new backup or continue precious * runs "makeBackup()" * * @return type */ public function run() { TimezoneWork::set('UTC'); It::debug("\n\n== STARTED MOVE DATABASE TO BACKUP ==", 'backup_database'); $this->settings = Settings::model()->findByPk(1); /** * check if backup process is enabled */ if (!$this->settings->db_exp_enabled) { It::debug("disabled", 'backup_database'); return; } else { It::debug("enabled", 'backup_database'); } /** * check time */ $now = time(); // Here we check if we should make backup $last_backup = BackupOldDataTx::getLastBackupInfo(); // calculate date of the next backup $next_planned = strtotime($last_backup->backup_date) + $this->settings->db_exp_frequency * 86400; // exit if time not yet or prew backup not completed if ($next_planned >= $now and $last_backup->completed == 1) { It::debug("not time yet", 'backup_database'); return; } /** * create new transaction */ It::debug("create new transaction", 'backup_database'); $last_backup = new BackupOldDataTx(); $last_backup->backup_date = new CDbExpression('NOW()'); $last_backup->data_timestamp_limit = date('Y-m-d 00:00:00', $now - $this->settings->db_exp_period * 86400); $last_backup->save(); $this->current_backup = $last_backup; /** * add log */ $this->addBackupLog("Need to Move Data stored before " . $last_backup->data_timestamp_limit . '; Date of backup: ' . $last_backup->backup_date . '; Frequency = ' . $this->settings->db_exp_frequency . '; '); It::debug("NOW = " . $now . " = " . date('Y-m-d H:i:s', $now), 'backup_database'); It::debug("last backup (backup_frequency) = " . $this->settings->db_exp_frequency, 'backup_database'); It::debug("last backup (id) = " . $last_backup->id, 'backup_database'); It::debug("last backup (date of backup) = " . $last_backup->backup_date, 'backup_database'); It::debug("last backup (data timestamp limit) = " . $last_backup->data_timestamp_limit, 'backup_database'); It::debug("last backup (completed) = " . $last_backup->completed, 'backup_database'); /** * make backup*/ $this->makeBackup(); //TODO: off delete row }