Home >Backend Development >PHP Tutorial >备份虚拟主机数据的办法_PHP

备份虚拟主机数据的办法_PHP

WBOY
WBOYOriginal
2016-06-01 12:40:101061browse

虚拟主机

一般来说虚拟主机备份数据库是一件比较麻烦的事情。为了方便,我自己写了一个简单的办法用于备份MYSQL的数据库。
PHP代码:


/**
* 虚拟主机备份程序
* 编程:Boban
* 讨论:http://www.21php.com/forums/
* 更新:2004-02-02
* 使用说明
* 1. 更改MYSQL连接的参数,$dbuser,$dbpass,$dbname
* 2. 用FTP登录,建立一个备份数据的专用目录,更改属性为777
* 3. 通过浏览器访问这个程序,如果mysqldump的路径不对,请自行更改(默认:/usr/bin/mysqldump)
* */

set_time_limit(900);
$today = date("_Y_m_d");
$dbuser = "root";
$dbpass = "";
$dbname = "www_21php_com";

$servername = $_SERVER['SERVER_NAME'];
$servername = str_replace('.','_',$servername);
//echo $servername;
$docroot = getcwd();

echo system("locate mysqldump");
echo "
";

// 删除备份文件
system("rm ./*.gz");
system("rm ./*.sql");

// 开始备份
system("/usr/bin/mysqldump --opt $dbname --user='$dbuser' --password='$dbpass' >'./$servername$today.sql'",$r1);
if($r1==0) echo "MYSQL数据备份成功
";
else echo "备份失败
";
system("gzip ./$servername$today.sql",$r2);
if($r2==0) echo "MYSQL数据压缩成功
";
else echo "备份失败
";
system("tar -czf './$servername$today.tar.gz' '$docroot/../'",$r3);
if($r3==0) echo "网站($docroot)备份成功
";
else echo "备份($docroot)失败
";
?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn