简体   繁体   English

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

[英]backup files to google drive using PHP

I have a server and a domain name on GoDaddy .我在GoDaddy上有一个服务器和一个域名。

I want to create a backup for my files to be uploaded on Google Drive我想为要上传到Google 云端硬盘的文件创建备份

So that all my files and my database have their data on Google Drive .这样我所有的文件和数据库都可以在Google Drive上找到它们的数据。

I use PHP and MySQL for my database我的数据库使用PHPMySQL

After some research, I found " Automatically backing up your web server files to GoogleDrive with PHP " and did what he said.经过一些研究,我发现“ 使用 PHP 自动将您的 web 服务器文件备份到 GoogleDrive ”,然后按照他说的做了。

I have downloaded the files google-api-php-client from the backuptogoogledrive repository .我已经从backuptogoogledrive 存储库下载了文件google-api-php-client

And I have a client ID , client secret and an authCode我有一个client IDclient secret和一个authCode

I edited the setting.inc and I put my own client ID , client secret and authCode .我编辑了 setting.inc 并放入了自己的客户端 ID客户端密码authCode I also put my MySQL username, password and hostname.我还输入了我的MySQL用户名、密码和主机名。

In this page backuptogoogledrive it should create a .tar.gz folder and this folder should contain my website files.在此页面backuptogoogledrive ,它应该创建一个.tar.gz文件夹,并且该文件夹应该包含我的网站文件。 Then, this folder should upload it to my Google Drive and do the same thing for my database.然后,此文件夹应将其上传到我的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");

?>

The problem now is that I have two folders for the database and there's no problem on it, and a second folder for the files.现在的问题是我有两个数据库文件夹并且没有问题,还有第二个文件文件夹。 But this folder doesn't have any files on it.但是这个文件夹里面没有任何文件。

How can I solve this problem?我怎么解决这个问题?

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

// 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/"; 

It seem the backup script is unable to find the directory where the site files are stored.备份脚本似乎无法找到存储站点文件的目录。

Quoting from the tutorial you followed to do the backup:引用您遵循的教程进行备份:

// 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/";

First ensure that $sitedir is set properly with the relative path (from the home directory) to the site files directory.首先确保$sitedir使用相对路径(从主目录)到站点文件目录正确设置。

It could be something different than www/ , for example public_html/ for a website hosted on GoDaddy.它可能与www/不同,例如public_html/用于托管在 GoDaddy 上的网站。

If the above is correct try to set $home variable manually with the absolute path of the home directory.如果以上正确,请尝试使用主目录的绝对路径手动设置$home变量。


Update更新

$homedir is the home directory and $sitedir is the website root relative to $homedir $homedir是主目录, $sitedir是相对于$homedir的网站根目录

so looking at the code you posted it's quite likely there is a mistake, the two variable should be:所以看看你发布的代码很可能有一个错误,两个变量应该是:

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

This assumes your website root directory is public_html and is located inside your home directory mhmd2991这假设您的网站根目录是public_html并且位于您的主目录中mhmd2991

Again make sure your website root directory is actually public_html and not www or html or anything else.再次确保您的网站根目录实际上是public_html而不是wwwhtml或其他任何内容。 Check it out using the terminal.使用终端检查一下。

Dont you miss a hyphen in front of cf in your tar?你不会错过你的焦油中cf前面的连字符吗? Isnt it supposed to be tar -cf ?不应该是tar -cf吗? You have tar cf .你有tar cf I dont think it recognises it as a command parameter, so there is no file created.我认为它不会将其识别为命令参数,因此没有创建文件。

I was able to run this script successfully while using root access (available in VPS / dedicated servers).在使用 root 访问(在 VPS/专用服务器中可用)时,我能够成功运行此脚本。 But in shared server, where root access is not available, this script is aborted by the server.但是在共享服务器中,root 访问权限不可用,此脚本被服务器中止。 Shared servers have restrictions on how much maximum time a script can run (usually less than one minute - but depends on the hosting)共享服务器对脚本可以运行的最长时间有限制(通常少于一分钟 - 但取决于托管)

Try to manually run the script in SSH and you will see that the script is aborted by the server which results in no files in the folder created.尝试在 SSH 中手动运行该脚本,您将看到该脚本被服务器中止,导致创建的文件夹中没有文件。

In GoDaddy, or any other platforms.在 GoDaddy 或任何其他平台中。 The shared hosting generally do not provide shell access or do not allow ssh access.共享主机一般不提供shell访问或不允许ssh访问。 Many times, you will face issues running command in the shell, as shell executable command are banned by the hosting provider.很多时候,您会遇到在 shell 中运行命令的问题,因为 shell 可执行命令已被托管服务提供商禁止。

This helps them to provide resource to all the shared hosting users in a better way without hampering the isolation of one another.这有助于他们以更好的方式向所有共享主机用户提供资源,而不会妨碍彼此的隔离。

You can still see options to turn on SSH nad other similar shell access options in different hosting platforms.您仍然可以在不同的托管平台中看到打开 SSH 和其他类似 shell 访问选项的选项。 Refer to the link below to activate SSH on GoDaddy https://in.godaddy.com/help/enable-ssh-for-my-linux-hosting-account-16102参考下面链接在GoDaddy上激活SSH https://in.godaddy.com/help/enable-ssh-for-my-linux-hosting-account-16102

Most quality hosting sites allow users to archive their program files/directory/site.大多数优质托管站点都允许用户存档他们的程序文件/目录/站点。 Similarly, if you are using databases, the database program usually has a link where you can archive and or export tables or an entire database.同样,如果您使用数据库,数据库程序通常有一个链接,您可以在其中存档和/或导出表或整个数据库。 You will have to look for the instructions on how to do this as it varies between platforms and database systems, and the hosting site.您将不得不寻找有关如何执行此操作的说明,因为它因平台和数据库系统以及托管站点而异。

Once you have archived your files/directory/site and/or databases you can use an FTP program to export them to your computer.归档文件/目录/站点和/或数据库后,您可以使用 FTP 程序将它们导出到您的计算机。 Likewise you can then copy them to another computer or the cloud.同样,您可以将它们复制到另一台计算机或云端。

My preference is to archive whatever I want to export into zip or tar files.我的偏好是将我想要导出的任何内容存档到 zip 或 tar 文件中。 For security reasons I also prefer to save my database files to an external drive or put them on a recordable DVD.出于安全原因,我还更喜欢将我的数据库文件保存到外部驱动器或将它们放在可刻录的 DVD 上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM