/**
* Power by PHP备份 (http://www.phpbak.com)
* 开发者:PHP老码农
*/
class back
{
//私有的构造方法
private $link;
private $host;
private $port;
private $user;
private $pass;
private $db;
private $charset;
public function __construct($config=array())
{
$this->host = $config['host'] ? $config['host'] : 'localhost';
$this->port = $config['port'] ? $config['port'] : '3306';
$this->user = $config['user'] ? $config['user'] : 'root';
$this->pass = $config['pass'] ? $config['pass'] : 'root';
$this->db = $config['db'] ? $config['db'] : 'small2';
$this->charset=isset($arr['charset']) ? $arr['charset'] : 'utf8';
//连接数据库
$this->db_connect();
//选择数据库
$this->db_usedb();
//设置字符集
$this->db_charset();
}
//连接数据库
private function db_connect()
{
$this->link=mysqli_connect($this->host.':'.$this->port,$this->user,$this->pass);
if(!$this->link){
echo "数据库连接失败
";
echo "错误编码".mysqli_errno($this->link)."
";
echo "错误信息".mysqli_error($this->link)."
";
exit;
}
}
//设置字符集
private function db_charset()
{
mysqli_query($this->link,"set names {$this->charset}");
}
//选择数据库
private function db_usedb()
{
mysqli_query($this->link,"use {$this->db}");
}
//数据库中有哪些表
public function get_list_tables()
{
$rs = mysqli_query($this->link,"SHOW TABLES FROM {$this->db}");
$tables = array();
while($row = mysqli_fetch_row($rs))
{
$tables[] = $row[0];
}
mysqli_free_result($rs);
return $tables;
}
//将每个表的表结构导出到文件
public function struct_to_file($tabList)
{
$str = '';
foreach($tabList as $val)
{
$sql = "show create table ".$val;
$res = mysqli_query($this->link,$sql);
$row = mysqli_fetch_array($res);
$info = "-- ----------------------------\r\n";
$info .= "-- Table structure for `".$val."`\r\n";
$info .= "-- ----------------------------\r\n";
$info .= "DROP TABLE IF EXISTS `".$val."`;\r\n";
$sqlStr = $info.$row[1].";\r\n\r\n";
mysqli_free_result($res);//释放资源
$str = $str . $sqlStr;
}
return $str;
}
//将每个表的数据导出到文件
public function tables_to_file($tabList)
{
$str = '';
foreach($tabList as $val)
{
$sql = "select * from ".$val;
$res = mysqli_query($this->link,$sql);
//如果表中没有数据,则继续下一张表
if(mysqli_num_rows($res)<1) continue;
$info = "-- ----------------------------\r\n";
$info .= "-- Records for `".$val."`\r\n";
$info .= "-- ----------------------------\r\n";
$str = $str .$info;
//读取数据
while($row = mysqli_fetch_row($res))
{
$sqlStr = "INSERT INTO `".$val."` VALUES (";
foreach($row as $zd)
{
$sqlStr .= "'".$zd."', ";
}
//去掉最后一个逗号和空格
$sqlStr = substr($sqlStr,0,strlen($sqlStr)-2);
$sqlStr .= ");\r\n";
$str = $str . $sqlStr;
}
//释放资源
mysqli_free_result($res);
$str = $str . $sqlStr;
}
return $str;
}
public function info()
{
$info = "-- ----------------------------\r\n";
$info .= "-- 日期:".date("Y-m-d H:i:s",time())."\r\n";
$info .= "-- Power by PHP备份 (http://www.phpbak.com)\r\n";
$info .= "-- 仅用于测试和学习,本程序不适合处理超大量数据\r\n";
$info .= "-- ----------------------------\r\n\r\n";
return $info;
}
public function sendMail($mail,$receiver,$files,$senderArr='')
{
try {
// 服务器设置
$mail->SMTPDebug = 0; // 开启Debug
$mail->isSMTP(); // 使用SMTP
$mail->Host = 'smtp.126.com'; // 服务器地址
$mail->SMTPAuth = true; // 开启SMTP验证
$mail->Username = 'XXX@126.com'; // SMTP 用户名(你要使用的邮件发送账号)
$mail->Password = 'XXX'; // SMTP 密码
// $mail->SMTPSecure = 'tls'; // 开启TLS 可选
$mail->Port = 25; // 端口
// 收件人
$mail->setFrom('XXX@126.com', 'phpbak.com'); // 来自
//$mail->addAddress('23275429@qq.com', 'George.Haung'); // 添加一个收件人
$mail->addAddress($receiver); // 可以只传邮箱地址
$mail->addReplyTo('XXX@126.com', 'phpbak.com'); // 回复地址
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');
// 附件
// $mail->addAttachment('/var/tmp/file.tar.gz'); // 添加附件
foreach($files as $key=>$value)
{
$mail->addAttachment($value, $key); // 可以设定名字
}
// 内容
// $mail->isHTML(true); // 设置邮件格式为HTML
$mail->CharSet = 'utf-8';
$mail->Subject = date("Y_m_d").' phpbak.com数据库备份文件';
$mail->Body = '欢迎收取phpbak.com数据库备份文件';
$mail->AltBody = '备份文件';
$mail->send();
$str = '邮件发送成功。
';
}
catch (Exception $e)
{
$str = '邮件发送失败。
';
$str = $str . 'Mailer Error: ' . $mail->ErrorInfo;
}
return $str;
}
}
?>
打赏
微信扫一扫,打赏作者吧~