我决定写一篇教程,教你如何使用cron来定期自动备份你的数据库,然后通过电子邮件或者ftp发给你。
SQL Backup - 使用电子邮件发送给你
PHP 代码:
<?
$datestamp = date("Y-m-d"); // 按照当前时间作为备份文件名,格式为:YYYY-MM-DD
/* 配置下面的7个变量来匹配你的设置 */
$dbuser = ""; // 数据库用户名
$dbpwd = ""; // 数据库密码
$dbname = ""; // 数据库名称. Use --all-databases if you have more than one(如果你有多个数据库请使用--all-databases)
$filename= "backup-$datestamp.sql.gz"; // 备份文件名(可以设定路径)
$to = "you@remotesite.com"; // 收件人电子邮件地址
$from = "you@yourhost.com"; // 发件人电子邮件地址
$subject = "MySQL backup file"; // 电子邮件标题
$command = "mysqldump -u $dbuser --password=$dbpwd $dbname | gzip > $filename";
$result = passthru($command);
$attachmentname = array_pop(explode("/", $filename)); // If a path was included, strip it out for the attachment name
$message = "Compressed database backup file $attachmentname attached.";
$mime_boundary = "<<<:" . md5(time());
$data = chunk_split(base64_encode(implode("", file($filename))));
$headers = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: multipart/mixed;\r\n";
$headers .= " boundary=\"".$mime_boundary."\"\r\n";
$content = "This is a multi-part message in MIME format.\r\n\r\n";
$content.= "--".$mime_boundary."\r\n";
$content.= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$content.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$content.= $message."\r\n";
$content.= "--".$mime_boundary."\r\n";
$content.= "Content-Disposition: attachment;\r\n";
$content.= "Content-Type: Application/Octet-Stream; name=\"$attachmentname\"\r\n";
$content.= "Content-Transfer-Encoding: base64\r\n\r\n";
$content.= $data."\r\n";
$content.= "--" . $mime_boundary . "\r\n";
mail($to, $subject, $content, $headers);
unlink($filename); //delete the backup file from the server
?>
SQL Backup - 使用ftp发送给你
PHP 代码:
<?
$datestamp = date("Y-m-d"); // 按照当前时间作为备份文件名,格式为:YYYY-MM-DD
/* 配置下面的3个变量来匹配你的设置 */
$dbuser = ""; // 数据库用户名
$dbpwd = ""; // 数据库密码
$dbname = ""; // 数据库名称. Use --all-databases if you have more than one(如果你有多个数据库请使用--all-databases)
$filename= "backup-$datestamp.sql.gz"; // 备份文件名(可以设定路径)
$command = "mysqldump -u $dbuser --password=$dbpwd $dbname | gzip > $filename";
$result = passthru($command);
/* CONFIGURE THE FOLLOWING FOUR VARIABLES TO MATCH YOUR FTP SETUP */
$ftp_server = ""; // Shouldn't have any trailing slashes and shouldn't be prefixed with ftp://
$ftp_port = "21"; // FTP port - blank defaults to port 21
$ftp_username = "anonymous"; // FTP account username
$ftp_password = ""; // FTP account password - blank for anonymous
// set up basic connection
$ftp_conn = ftp_connect($ftp_server);
// Turn PASV mode on or off
ftp_pasv($ftp_conn, false);
// login with username and password
$login_result = ftp_login($ftp_conn, $ftp_username, $ftp_password);
// check connection
if ((!$ftp_conn) || (!$login_result))
{
echo "FTP connection has failed.";
echo "Attempted to connect to $ftp_server for user $ftp_username";
exit;
}
else
{
echo "Connected to $ftp_server, for user $ftp_username";
}
// upload the file
$upload = ftp_put($ftp_conn, $filename, $filename, FTP_BINARY);
// check upload status
if (!$upload)
{
echo "FTP upload has failed.";
}
else
{
echo "Uploaded $filename to $ftp_server.";
}
// close the FTP stream
ftp_close($ftp_conn);
unlink($filename); //delete the backup file from the server
?>
你需要填写相关变量,然后保存为一个php文件,比如name.php,然后上传到你的服务器上相应的目录下,并设置权限为755
然后在cron下添加一个cron job,使用以下代码
代码:
php -q /home/username/public_html/folder/name.php
这个路径,你可能需要修改成你自己的路径,比如username需要换成你自己的用户名,folder需要换成你文件所在的目录,还有其他的,要根据实际情况来。
这些脚本不是我写的,是Sandman写的
译者(MyBB中文站)注:这篇文章不是我写的,是我翻译的。
原文链接:http://community.mybboard.net/thread-48421.html
本文可以随意转载,但请注明出处:
MyBB中文站
http://www.mybbchina.net
通过cron备份数据库 [教程]