=', 'created_at', strtotime($params['datetime'][0])]; if (!empty($params['datetime'][1])) $where[] = ['<=', 'created_at', strtotime($params['datetime'][1]. ' 23:59:59')]; $params['where'] = $where; $params['order'] = 'id desc'; $list = Backups::listPage($params); return $list; } catch (\Exception $e) { throw new Exception($e->getMessage()); } } //开始备份 public function startBackups() { try { $check = Backups::getOne([ ['status' => 0], ], true); if (!empty($check)) throw new Exception('存在备份中任务,请稍后再试'); $data = [ 'name' => 'mysql-'. date('Y-m-d-H-i').'.sql.gz', 'size' => '', 'status' => 0, 'type' => 1, ]; $res = Backups::setData($data); if (!$res) throw new Exception(Backups::$static_error); Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_LONG_DURATION, RabbitMqEnum::LONG_DURATION_QUEUE, ['id' => $res->id], BackupsService::class, 'backupsData'); return true; } catch (\Exception $e) { throw new Exception($e->getMessage()); } } //删除备份文件 public function deleteBackups($params) { try { // $ids = explode(',', $params['id']); $where = [ ['!=', 'status', -1] ]; if (!empty($params['id'])) $where[] = ['id' => $params['id']]; $list = Backups::lists(['where' => $where]); if (!empty($list)) { $ids = array_column($list, 'id'); $res = Backups::updateAll(['status' => -1], ['id' => $ids]); if (!$res) throw new Exception(Backups::$static_error); foreach ($list as $key => $value) { if (!empty($value['url'])) { unlink($value['url']); } } } } catch (\Exception $e) { throw new Exception($e->getMessage()); } } //执行备份脚本 public function backupsData($params) { try { if (is_dir('/www/wwwroot/qimall')) { $path = '/www/wwwroot/qimall/backend/web/uploads/backups'; } else { $path = '/data/wwwroot/qimall/backend/web/uploads/backups'; } if (!is_dir($path)) { @mkdir($path, 0777, true); } $info = Backups::getOne([ ['id' => $params['id']], ]); if (!is_dir('/data')) @mkdir('/data', 0777, true); if (!is_dir('/data/bak')) @mkdir('/data/bak', 0777, true); if (!is_dir('/data/bak/mysql')) @mkdir('/data/bak/mysql', 0777, true); //执行数据库备份脚本 exec('sudo sh /scripts/bak_db.sh 2>&1', $result, $status); if (!empty($result)) { foreach ($result as $key => $value) { if (strpos($value, 'Error') !== false) { throw new Exception( json_encode($value) ); } } } // if( $status ) throw new Exception( json_encode($result) ); echo '数据备份--数据库备份脚本执行完成'; $url = $path .'/'.$info['name']; $source_path = ''; if (file_exists('/data/bak/mysql/qimall-'.date('Y-m-d-H').'.sql.gz')) { echo '数据备份--查找备份文件成功'; $source_path = '/data/bak/mysql/qimall-'.date('Y-m-d-H').'.sql.gz'; } else { $time = time() - 3600; $source_path = '/data/bak/mysql/qimall-'.date('Y-m-d-H', $time).'.sql.gz'; } echo '数据备份--'.$source_path; if (copy($source_path, $url)) { chmod($url, 0777); $info->url = $url; $info->status = 1; $info->size = file_exists($url) ? $this->transf_byte(filesize($url)) : 0; } else { echo '数据备份--copy备份文件失败'; throw new Exception('copy备份文件失败'); } // print_r($info); echo '数据备份--copy备份文件成功'; if (!$info->save()) throw new Exception('保存数据失败'); //获取全部备份数据 $list = Backups::lists([ 'where' => [ ['!=', 'status', -1], ], 'order' => 'id desc', ]); if (!empty($list)) { $ids = []; foreach ($list as $key => $value) { if ($key >= 7) { $ids[] = $value['id']; } } //存在数据,则清除备份数据 if (!empty($ids)) $this->deleteBackups(['id' => $ids]); } } catch (\Exception $e) { echo '数据备份--'.$e->getMessage(); // print_r($e->getMessage()); $info->status = 2; $info->desc = $e->getMessage(); $info->save(); Yii::error($e->getMessage()); throw new Exception($e->getMessage()); } } public function transf_byte($byte) { $kb = 1024; $mb = $kb * 1024; $gb = $mb * 1024; if ($byte < $kb) { return $byte . 'b'; } else if ($byte < $mb) { return round($byte / $kb, 2) . 'kb'; } else if ($byte < $gb) { return round($byte / $mb, 2) . 'mb'; } } }