Cakephp 备份时间-缺少连接管理器
我正在使用下面的shell脚本。我在app/console/command/BackupShell.php中有它 运行脚本时,出现以下错误: 致命错误:在app/Console/Command/BackupShell.php中找不到类“ConnectionManager” 我做错了什么Cakephp 备份时间-缺少连接管理器,cakephp,cakephp-2.0,Cakephp,Cakephp 2.0,我正在使用下面的shell脚本。我在app/console/command/BackupShell.php中有它 运行脚本时,出现以下错误: 致命错误:在app/Console/Command/BackupShell.php中找不到类“ConnectionManager” 我做错了什么 class BackupShell extends Shell { var $tasks = array('ProgressBar'); public function main() { //datab
class BackupShell extends Shell {
var $tasks = array('ProgressBar');
public function main() {
//database configuration, default is "default"
if(!isset($this->args[0])){
$this->args[0] = 'default';
}
//rows per query (less rows = less ram usage but more running time), default is 0 which means all rows
if(!isset($this->args[1])){
$this->args[1] = 0;
}
//directory to save your backup, it will be created automatically if not found., default is webroot/db-backups/yyyy-mm-dd
if(!isset($this->args[2])){
$this->args[2] = 'db-backups/'.date('Y-m-d',time());
}
App::import('Core', 'ConnectionManager');
$db = ConnectionManager::getDataSource($this->args[0]);
$backupdir = $this->args[2];
$seleced_tables = '*';
//$tables = array('orders', 'users', 'profiles');
if ($seleced_tables == '*') {
$sources = $db->query("show full tables where Table_Type = 'BASE TABLE'", false);
foreach($sources as $table){
$table = array_shift($table);
$tables[] = array_shift($table);
}
} else {
$tables = is_array($tables) ? $tables : explode(',', $tables);
}
$filename = 'db-backup-' . date('Y-m-d-H-i-s',time()) .'_' . (md5(implode(',', $tables))) . '.sql';
$return = '';
$limit = $this->args[1];
$start = 0;
if(!is_dir($backupdir)) {
$this->out(' ', 1);
$this->out('Will create "'.$backupdir.'" directory!', 2);
if(mkdir($backupdir,0755,true)){
$this->out('Directory created!', 2);
}else{
$this->out('Failed to create destination directory! Can not proceed with the backup!', 2);
die;
}
}
if ($this->__isDbConnected($this->args[0])) {
$this->out('---------------------------------------------------------------');
$this->out(' Starting Backup..');
$this->out('---------------------------------------------------------------');
foreach ($tables as $table) {
$this->out(" ",2);
$this->out($table);
$handle = fopen($backupdir.'/'.$filename, 'a+');
$return= 'DROP TABLE IF EXISTS `' . $table . '`;';
$row2 = $db->query('SHOW CREATE TABLE ' . $table.';');
//$this->out($row2);
$return.= "\n\n" . $row2[0][0]['Create Table'] . ";\n\n";
fwrite($handle, $return);
for(;;){
if($limit == 0){
$limitation = '';
}else{
$limitation = ' Limit '.$start.', '.$limit;
}
$result = $db->query('SELECT * FROM ' . $table.$limitation.';', false);
$num_fields = count($result);
$this->ProgressBar->start($num_fields);
if($num_fields == 0){
$start = 0;
break;
}
foreach ($result as $row) {
$this->ProgressBar->next();
$return2 = 'INSERT INTO ' . $table . ' VALUES(';
$j = 0;
foreach ($row[$table] as $key => $inner) {
$j++;
if(isset($inner)){
if ($inner == NULL){
$return2 .= 'NULL';
}else{
$inner = addslashes($inner);
$inner = ereg_replace("\n", "\\n", $inner);
$return2.= '"' . $inner . '"';
}
}else {
$return2.= '""';
}
if ($j < (count($row[$table]))) {
$return2.= ',';
}
}
$return2.= ");\n";
fwrite($handle, $return2);
}
$start+=$limit;
if($limit == 0){
break;
}
}
$return.="\n\n\n";
fclose($handle);
}
$this->out(" ",2);
$this->out('---------------------------------------------------------------');
$this->out(' Yay! Backup Completed!');
$this->out('---------------------------------------------------------------');
}else{
$this->out(' ', 2);
$this->out('Error! Can\'t connect to "'.$this->args[0].'" database!', 2);
}
}
function __isDbConnected($db = NULL) {
$datasource = ConnectionManager::getDataSource($db);
return $datasource->isConnected();
}
类BackupShell扩展Shell{
var$tasks=array('ProgressBar');
公共功能main(){
//数据库配置,默认为“默认”
如果(!isset($this->args[0])){
$this->args[0]=“默认值”;
}
//每个查询的行数(行数越少=ram使用量越少,但运行时间越长),默认值为0,表示所有行
如果(!isset($this->args[1])){
$this->args[1]=0;
}
//用于保存备份的目录,如果未找到,将自动创建该目录。默认值为webroot/db backups/yyyy-mm-dd
如果(!isset($this->args[2])){
$this->args[2]=“db backups/”.date('Y-m-d',time());
}
应用程序::导入('Core','ConnectionManager');
$db=ConnectionManager::getDataSource($this->args[0]);
$backupdir=$this->args[2];
$seleced_tables='*';
//$tables=数组('orders'、'users'、'profiles');
如果($seleced_tables=='*')){
$sources=$db->query(“在Table_Type='BASE Table'”中显示完整的表,false);
foreach($sources作为$table){
$table=数组移位($table);
$tables[]=数组移位($table);
}
}否则{
$tables=is_数组($tables)?$tables:explode(',',$tables);
}
$filename='db backup-'.date('Y-m-d-H-i-s',time()).'.'(md5(内爆(',',$tables))).sql';
$return='';
$limit=$this->args[1];
$start=0;
如果(!is_dir($backupdir)){
$this->out(“”,1);
$this->out('将创建“.$backupdir.”目录!',2);
if(mkdir($backupdir,0755,true)){
$this->out('Directory created!',2);
}否则{
$this->out('创建目标目录失败!无法继续备份!',2);
死亡
}
}
如果($this->\uu isDbConnected($this->args[0])){
$this->out('---------------------------------------------------------------');
$this->out('Starting Backup..');
$this->out('---------------------------------------------------------------');
foreach($tables作为$table){
$this->out(“,2);
$this->out($table);
$handle=fopen($backupdir.'/'.$filename,'a+');
$return='DROP TABLE IF EXISTS`.$TABLE.'`;';
$row2=$db->query('SHOW CREATE TABLE'.$TABLE'.;');
//$this->out($row2);
$return.=“\n\n”。$row2[0][0][‘创建表’]”;\n\n;
fwrite($handle,$return);
对于(;;){
如果($limit==0){
$limition='';
}否则{
$Limit='Limit'.$start'.','.$Limit;
}
$result=$db->query('SELECT*FROM.$table.$limition.';',false);
$num_fields=计数($result);
$this->ProgressBar->start($num\u字段);
如果($num_字段==0){
$start=0;
打破
}
foreach($结果为$行){
$this->ProgressBar->next();
$return2='INSERT INTO'.$table.'值(';
$j=0;
foreach($row[$table]作为$key=>$inner){
$j++;
if(isset($内部)){
如果($inner==NULL){
$return2.='NULL';
}否则{
$inner=addslashes($inner);
$inner=ereg\U replace(“\n”、“\\n”、$inner);
$return2.=''.$INERNAR'.';
}
}否则{
$return2.='''';
}
如果($j<(计数($row[$table])){
$return2.=',';
}
}
$return2.=”;\n“;
fwrite($handle,$return2);
}
$start+=$limit;
如果($limit==0){
打破
}
}
$return.=“\n\n\n”;
fclose($handle);
}
$this->out(“,2);
$this->out('---------------------------------------------------------------');
$this->out('Yay!备份完成!');
$this->out('---------------------------------------------------------------');
}否则{
$this->out(“”,2);
$this->out('错误!无法连接到“.$this->args[0]”。“数据库!”,2);
}
}
函数uu isDbConnected($db=NULL){
$datasource=ConnectionManager::getDataSource($db);
返回$datasource->isConnected();
}
}这是2.x代码吗
App::import('Core', 'ConnectionManager');
在2.x中已弃用/错误,应
App::uses('ConnectionManager', 'Model');
也可以放在文件的顶部,