【发布时间】:2018-03-31 10:21:41
【问题描述】:
我在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/";
【问题讨论】:
-
@DaImTo 你能解释一下吗
-
你是说它适用于数据库文件夹备份,但不适用于文件?
-
@Don'tPanic 是的,我说的是
-
我没有看到任何创建文件夹的代码,只有
.gz文件。你能澄清一下吗? -
@Don'tPanic 在我检查了我认为数据库工作的文件后,因为它创建了一个文件而不是文件夹,你可以检查上面的 2 张图片
标签: php mysql google-drive-api