我的目的是自动将MySQL数据库和整个public_html文件夹备份到一个zip文件中,并以可下载的形式通过电子邮件将其作为附件发送给网站管理员(我)。 我可以作为一个cron运行的东西。
一些研究给了我备份文件的脚本,但没有发现如何将其作为附件邮寄,甚至没有发现如何通过SHELL(.sh)文件触发PHP文件。
我正在通过共享主机在Linux服务器上工作但是允许使用crons。
关于如何做到这一点的任何想法? 一些代码会有很多帮助! 提前谢谢。
答案 0 :(得分:0)
如果您正在使用Cpanel,则可以使用简单的PHP脚本进行连接,您可以使用cron作业触发该脚本。
我将包含一个例子。
$auth = base64_encode("username:password"); //replace with your credentials. Make sure you don't publish them anywhere
$domain = "cpanel.mydomain.com"; //replace with your domain
$theme = "paper_lantern"; //replace with your theme
$secure = false; //if no valid ssl certificate
$ftp = true; //false if you don't want to backup to an FTP server
$ftpserver = "ftp.mydomain.com"; //replace with your FTP server for backing up to FTP
$ftpusername = "ftp username";
$ftppassword = "ftp password";
$ftpport = "21";
$ftpdirectory = "/";
if ($secure) {
$url = "ssl://" . $domain;
$port = 2083;
} else {
$url = $domain;
$port = 80;
}
$socket = fsockopen($url, $port);
if (!$socket) {
exit("Failed to open socket connection.");
}
if ($ftp) {
$params = "dest=ftp&server=$ftpserver&user=$ftpusername&pass=$ftppassword&port=$ftpport&rdir=$ftpdirectory&submit=Generate Backup";
} else {
$params = "submit=Generate Backup";
}
fputs($socket, "POST /frontend/" . $theme . "/backup/dofullbackup.html?" . $params . " HTTP/1.0\r\n");
fputs($socket, "Host: $domain\r\n");
fputs($socket, "Authorization: Basic $auth\r\n");
fputs($socket, "Connection: Close\r\n");
fputs($socket, "\r\n");
while (!feof($socket)) {
$response = fgets($socket, 4096);
}
fclose($socket);
sleep(120);
//laatste file
$path = "/home/myusername/backups"; //backup store location
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
$file = "backups/" . $latest_filename;
$email_to = "Somebody <me@example.com>";
$email_from = "My Webserver <webserver@example.com>";
$email_subject = "Created a backup";
$email_txt = "<html><body>\n" . "Backup created on " . date('d/m/Y') . " at " . date("h:i:sa") . " (" . date_default_timezone_get() . "). Backup saved as " . $latest_filename . " <br>Backup is created every day." . "<br><hr><br> This mail was automatically generated. Please don't answer." . "</body></html>";
$fileatt = $file;
$fileatt_type = "package/x-generic";
$fileatt_name = $latest_filename;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers="From: " . $email_from;
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_txt;
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
mail($email_to,$email_subject,$email_message,$headers);
在cronjobs中,您将使用php -f ~/backup.php