[英]backup files to google drive using PHP
我在GoDaddy上有一个服务器和一个域名。
我想为要上传到Google 云端硬盘的文件创建备份
这样我所有的文件和数据库都可以在Google Drive上找到它们的数据。
我的数据库使用PHP
和MySQL
经过一些研究,我发现“ 使用 PHP 自动将您的 web 服务器文件备份到 GoogleDrive ”,然后按照他说的做了。
我已经从backuptogoogledrive 存储库下载了文件google-api-php-client
。
我有一个client ID , client secret和一个authCode
我编辑了 setting.inc 并放入了自己的客户端 ID 、客户端密码和authCode 。 我还输入了我的MySQL
用户名、密码和主机名。
在此页面backuptogoogledrive
,它应该创建一个.tar.gz
文件夹,并且该文件夹应该包含我的网站文件。 然后,此文件夹应将其上传到我的Google 云端硬盘并对我的数据库执行相同的操作。
<?php
set_time_limit(0);
ini_set('memory_limit', '1024M');
require_once("google-api-php-client/src/Google_Client.php");
require_once("google-api-php-client/src/contrib/Google_DriveService.php");
include("settings.inc.php");
if($authCode == "") die("You need to run getauthcode.php first!\n\n");
/* PREPARE FILES FOR UPLOAD */
// Use the current date/time as unique identifier
$uid = date("YmdHis");
// Create tar.gz file
shell_exec("cd ".$homedir." && tar cf - ".$sitedir." -C ".$homedir." | gzip -9 > ".$homedir.$fprefix.$uid.".tar.gz");
// Dump datamabase
shell_exec("mysqldump -u".$dbuser." -p".$dbpass." ".$dbname." > ".$homedir.$dprefix.$uid.".sql");
shell_exec("gzip ".$homedir.$dprefix.$uid.".sql");
/* SEND FILES TO GOOGLEDRIVE */
$client = new Google_Client();
// Get your credentials from the APIs Console
$client->setClientId($clientId);
$client->setClientSecret($clientSecret);
$client->setRedirectUri($requestURI);
$client->setScopes(array("https://www.googleapis.com/auth/drive"));
$service = new Google_DriveService($client);
// Exchange authorisation code for access token
if(!file_exists("token.json")) {
// Save token for future use
$accessToken = $client->authenticate($authCode);
file_put_contents("token.json",$accessToken);
}
else $accessToken = file_get_contents("token.json");
$client->setAccessToken($accessToken);
// Upload file to Google Drive
$file = new Google_DriveFile();
$file->setTitle($fprefix.$uid.".tar.gz");
$file->setDescription("Server backup file");
$file->setMimeType("application/gzip");
$data = file_get_contents($homedir.$fprefix.$uid.".tar.gz");
$createdFile = $service->files->insert($file, array('data' => $data, 'mimeType' => "application/gzip",));
// Process response here....
print_r($createdFile);
// Upload database to Google Drive
$file = new Google_DriveFile();
$file->setTitle($dprefix.$uid.".sql.gz");
$file->setDescription("Database backup file");
$file->setMimeType("application/gzip");
$data = file_get_contents($homedir.$dprefix.$uid.".sql.gz");
$createdFile = $service->files->insert($file, array('data' => $data, 'mimeType' => "application/gzip",));
// Process response here....
print_r($createdFile);
/* CLEANUP */
// Delete created files
unlink($homedir.$fprefix.$uid.".tar.gz");
unlink($homedir.$dprefix.$uid.".sql.gz");
?>
现在的问题是我有两个数据库文件夹并且没有问题,还有第二个文件文件夹。 但是这个文件夹里面没有任何文件。
我怎么解决这个问题?
// User home directory (absolute)
$homedir = "/home/mhmd2991/public_html/"; // If this doesn't work, you can provide the full path yourself
// Site directory (relative)
$sitedir = "public_html/";
备份脚本似乎无法找到存储站点文件的目录。
引用您遵循的教程进行备份:
// User home directory (absolute)
$homedir = trim(shell_exec("cd ~ && pwd"))."/"; // If this doesn't work, you can provide the full path yourself
// Site directory (relative)
$sitedir = "www/";
首先确保$sitedir
使用相对路径(从主目录)到站点文件目录正确设置。
它可能与www/
不同,例如public_html/
用于托管在 GoDaddy 上的网站。
如果以上正确,请尝试使用主目录的绝对路径手动设置$home
变量。
更新
$homedir
是主目录, $sitedir
是相对于$homedir
的网站根目录
所以看看你发布的代码很可能有一个错误,两个变量应该是:
// User home directory (absolute)
$homedir = "/home/mhmd2991/"; // <-- FIXED HERE
// Site directory (relative)
$sitedir = "public_html/";
这假设您的网站根目录是public_html
并且位于您的主目录中mhmd2991
再次确保您的网站根目录实际上是public_html
而不是www
或html
或其他任何内容。 使用终端检查一下。
你不会错过你的焦油中cf
前面的连字符吗? 不应该是tar -cf
吗? 你有tar cf
。 我认为它不会将其识别为命令参数,因此没有创建文件。
在使用 root 访问(在 VPS/专用服务器中可用)时,我能够成功运行此脚本。 但是在共享服务器中,root 访问权限不可用,此脚本被服务器中止。 共享服务器对脚本可以运行的最长时间有限制(通常少于一分钟 - 但取决于托管)
尝试在 SSH 中手动运行该脚本,您将看到该脚本被服务器中止,导致创建的文件夹中没有文件。
在 GoDaddy 或任何其他平台中。 共享主机一般不提供shell访问或不允许ssh访问。 很多时候,您会遇到在 shell 中运行命令的问题,因为 shell 可执行命令已被托管服务提供商禁止。
这有助于他们以更好的方式向所有共享主机用户提供资源,而不会妨碍彼此的隔离。
您仍然可以在不同的托管平台中看到打开 SSH 和其他类似 shell 访问选项的选项。 参考下面链接在GoDaddy上激活SSH https://in.godaddy.com/help/enable-ssh-for-my-linux-hosting-account-16102
大多数优质托管站点都允许用户存档他们的程序文件/目录/站点。 同样,如果您使用数据库,数据库程序通常有一个链接,您可以在其中存档和/或导出表或整个数据库。 您将不得不寻找有关如何执行此操作的说明,因为它因平台和数据库系统以及托管站点而异。
归档文件/目录/站点和/或数据库后,您可以使用 FTP 程序将它们导出到您的计算机。 同样,您可以将它们复制到另一台计算机或云端。
我的偏好是将我想要导出的任何内容存档到 zip 或 tar 文件中。 出于安全原因,我还更喜欢将我的数据库文件保存到外部驱动器或将它们放在可刻录的 DVD 上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.