使用php将文件备份到谷歌驱动器

moh*_*mad 9 php mysql google-drive-api

我在GoDaddy上有一个服务器和一个域名.

我想为要在Google云端硬盘上传的文件创建备份

这样我的所有文件和数据库都会在Google云端硬盘上显示数据.

我使用PHPMySQL我的数据库

经过一些研究,我发现" 使用PHP自动将您的Web服务器文件备份到GoogleDrive "并按照他的说法完成了.

google-api-php-clientbackuptogoogledrive存储库下载了这些文件.

我有一个客户端ID,客户端密码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");

?>
Run Code Online (Sandbox Code Playgroud)

现在的问题是我有两个数据库文件夹,没有问题,文件的第二个文件夹.但是这个文件夹上没有任何文件.

我怎么解决这个问题?

在此输入图像描述 在此输入图像描述

// 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/"; 
Run Code Online (Sandbox Code Playgroud)

Pao*_*olo 0

备份脚本似乎无法找到存储站点文件的目录。

引用您进行备份时遵循的教程:

// 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/";
Run Code Online (Sandbox Code Playgroud)

首先确保$sitedir正确设置了站点文件目录的相对路径(从主目录)。

它可能与 不同www/,例如public_html/对于 GoDaddy 上托管的网站。

如果上述正确,请尝试$home使用主目录的绝对路径手动设置变量。


更新

$homedir是主目录,$sitedir是相对于的网站根目录$homedir

因此,查看您发布的代码,很可能存在错误,这两个变量应该是:

// User home directory (absolute)
$homedir = "/home/mhmd2991/"; // <-- FIXED HERE
// Site directory (relative)
$sitedir = "public_html/";
Run Code Online (Sandbox Code Playgroud)

这假设您的网站根目录public_html位于您的主目录中mhmd2991

再次确保您的网站根目录实际上是public_html,而不是wwwhtml或其他任何目录。使用终端检查一下。