[英]backup files to google drive using PHP
我在GoDaddy上有一個服務器和一個域名。
我想為要上傳到Google 雲端硬盤的文件創建備份
這樣我所有的文件和數據庫都可以在Google Drive上找到它們的數據。
我的數據庫使用PHP
和MySQL
經過一些研究,我發現“ 使用 PHP 自動將您的 web 服務器文件備份到 GoogleDrive ”,然后按照他說的做了。
我已經從backuptogoogledrive 存儲庫下載了文件google-api-php-client
。
我有一個client ID , client secret和一個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");
?>
現在的問題是我有兩個數據庫文件夾並且沒有問題,還有第二個文件文件夾。 但是這個文件夾里面沒有任何文件。
我怎么解決這個問題?
// 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
或其他任何內容。 使用終端檢查一下。
你不會錯過你的焦油中cf
前面的連字符嗎? 不應該是tar -cf
嗎? 你有tar cf
。 我認為它不會將其識別為命令參數,因此沒有創建文件。
在使用 root 訪問(在 VPS/專用服務器中可用)時,我能夠成功運行此腳本。 但是在共享服務器中,root 訪問權限不可用,此腳本被服務器中止。 共享服務器對腳本可以運行的最長時間有限制(通常少於一分鍾 - 但取決於托管)
嘗試在 SSH 中手動運行該腳本,您將看到該腳本被服務器中止,導致創建的文件夾中沒有文件。
在 GoDaddy 或任何其他平台中。 共享主機一般不提供shell訪問或不允許ssh訪問。 很多時候,您會遇到在 shell 中運行命令的問題,因為 shell 可執行命令已被托管服務提供商禁止。
這有助於他們以更好的方式向所有共享主機用戶提供資源,而不會妨礙彼此的隔離。
您仍然可以在不同的托管平台中看到打開 SSH 和其他類似 shell 訪問選項的選項。 參考下面鏈接在GoDaddy上激活SSH https://in.godaddy.com/help/enable-ssh-for-my-linux-hosting-account-16102
大多數優質托管站點都允許用戶存檔他們的程序文件/目錄/站點。 同樣,如果您使用數據庫,數據庫程序通常有一個鏈接,您可以在其中存檔和/或導出表或整個數據庫。 您將不得不尋找有關如何執行此操作的說明,因為它因平台和數據庫系統以及托管站點而異。
歸檔文件/目錄/站點和/或數據庫后,您可以使用 FTP 程序將它們導出到您的計算機。 同樣,您可以將它們復制到另一台計算機或雲端。
我的偏好是將我想要導出的任何內容存檔到 zip 或 tar 文件中。 出於安全原因,我還更喜歡將我的數據庫文件保存到外部驅動器或將它們放在可刻錄的 DVD 上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.