我在 GoDaddy 上有一个服务器和一个域名.
我想为要上传到 Google Drive 的文件创建备份
这样我所有的文件和数据库都在 Google Drive 上有它们的数据.
我的数据库使用 PHP
和 MySQL
经过一些研究,我找到了“Automatically backing up your web server files to GoogleDrive with PHP”并按照他说的做了。
我已经从 backuptogoogledrive repository 下载文件 google-api-php-client
.
我有一个客户端 ID、客户端密码 和一个authCode
我编辑了 setting.inc 并放入了我自己的客户端 ID、客户端密码 和 授权码。我还输入了我的 MySQL
用户名、密码和主机名。
在此页面 backuptogoogledrive
中,它应该创建一个 .tar.gz
文件夹,并且此文件夹应该 包含我的网站文件。然后,这个文件夹应该上传到我的Google Drive对我的数据库做同样的事情。
<?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
或其他任何内容。使用终端检查。
关于php - 使用 PHP 将文件备份到谷歌驱动器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46828881/