备份文件到谷歌驱动器使用PHP [英] backup files to google drive using php

查看:94
本文介绍了备份文件到谷歌驱动器使用PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在

  //用户主目录(绝对)
$ homedir =/ home / mhmd2991 / public_html /; //如果这不起作用,您可以自己提供完整路径
//站点目录(相对)
$ sitedir =public_html /;


解决方案

访问权限(可在VPS /专用服务器中使用)。但在共享服务器中,根访问不可用,此脚本被服务器中止。共享服务器对脚本可以运行的最长时间有限制(通常少于一分钟 - 但取决于主机)

尝试在SSH中手动运行脚本并您将看到脚本被服务器中止,导致创建的文件夹中没有文件。


I have a server and a domain name on GoDaddy.

I want to create a backup for my files to be uploaded on Google Drive

So that all my files and my database have their data on Google Drive.

I use PHP and MySQL for my database

After some research, I found "Automatically backing up your web server files to GoogleDrive with PHP" and did what he said.

I have downloaded the files google-api-php-client from the backuptogoogledrive repository.

And I have a client ID, client secret and an authCode

I edited the setting.inc and I put my own client ID, client secret and authCode. I also put my MySQL username, password and hostname.

In this page backuptogoogledrive it should create a .tar.gz folder and this folder should contain my website files. Then, this folder should upload it to my Google Drive and do the same thing for my database.

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

解决方案

I was able to run this script successfully while using root access (available in VPS / dedicated servers). But in shared server, where root access is not available, this script is aborted by the server. 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.

这篇关于备份文件到谷歌驱动器使用PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆