php-google-backup : backup Google Apps data with PHP and Zend Framework GData
I have to admit that I cannot do without the whole range of Google Apps, everyday I intensely use GMail, Calendar, Docs and Reader.
php-google-backup is a simple command line php script, based on Zend Framework GData API, that allows you to backup your Google Apps data to your hard disk.
Note that this first version backups only Google Docs (Documents), but I’m planning to extend it to Google Calendar and possibly to Gmail.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | <?php // version 1.0 require_once('Zend/Gdata.php'); require_once('Zend/Gdata/ClientLogin.php'); require_once('Zend/Gdata/Docs.php'); $CONFIG = array( "username" => "myUsername", "password" => "myPassword", "backupPath" => "/home/paolo/Documents/google" ); $documentTypes = array( array("getter" => "document", "downloadGDocsPath" => "documents", "search" => "document", "format" => "odt"), array("getter" => "document", "downloadGDocsPath" => "documents", "search" => "pdf", "format" => "pdf"), array("getter" => "document", "downloadGDocsPath" => "presentations", "search" => "presentation", "format" => "ppt"), array("getter" => "spreadsheet", "downloadGDocsPath" => "spreadsheets", "search" => "spreadsheet", "format" => "ods") ); echo "Backup Google Documents...\n"; try { $client = Zend_Gdata_ClientLogin::getHttpClient($CONFIG["username"], $CONFIG["password"], "writely"); $docs = new Zend_Gdata_Docs($client); foreach($documentTypes as $docType) { $feed = $docs->getDocumentListFeed("http://docs.google.com/feeds/documents/private/full/-/".$docType["search"]); foreach($feed as $item) { echo "\t".$item->getTitle()." (last updated ".$item->getUpdated().")... "; $id = substr($item->getId(), strlen(Zend_Gdata_Docs::DOCUMENTS_LIST_FEED_URI) + 4 + strlen($docType["search"])); $lastUpdatedTime = strtotime($item->getUpdated()); $saveFilePath = $CONFIG["backupPath"]."/".$id.".".$docType["format"]; if(file_exists($saveFilePath)) { if(filemtime($saveFilePath) > $lastUpdatedTime) { echo "done!\n"; continue; } } $flagDownload = FALSE; if($docType["getter"] == "document") { $client->setUri("http://docs.google.com/feeds/download/".$docType["downloadGDocsPath"]."/Export?docID=".$id."&exportFormat=".$docType["format"]); $flagDownload = TRUE; } else if($docType["getter"] == "spreadsheet") { $client->setUri("http://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=".$id."&fmcmd=".$docType["format"]); $flagDownload = TRUE; } if($flagDownload) { $response = $client->request(); $fp = fopen($saveFilePath, "wb"); fwrite($fp, $response->getBody()); fclose($fp); echo "done!\n"; } else { echo "error!\n"; } } } } catch(Exception $e) { echo $e->getMessage(); } echo "done!\n"; ?> |
Enjoy :)


[...] simple php command line script based on Zend Framework GData API. Now I release first version of php-google-backup only with this feature but I’m planning to extend it to GMail, Google Calendar and Google [...]
I don’t know how to use this code. Can I copy and paste into engine and run?
Thanks
This is a php script, you have to copy and past the code to a text file and run it with php…
I used your code to backup my files but I had to make a few chages because I was getting errors. Some document variables were objects but the script treated them as strings so I added getText(). Maybe this is a zend version issue. Also, my version saves files using document titles, not ids.
foreach($feed->entries as $item) {
$title = $item->getTitle()->getText();
$updated = $item->getUpdated()->getText();
echo “\t$title (last updated $updated)… “;
$id = substr($item->getId()->getText(), strlen(Zend_Gdata_Docs::DOCUMENTS_LIST_FEED_URI) 4 strlen($docType["search"]));
$lastUpdatedTime = strtotime($updated);
$cleanTitle = str_replace (’ ‘, ‘_’, $title); //replace space with underdash
$cleanTitle = preg_replace (”/\W/”, ”, $cleanTitle); //remove special chars
$saveFilePath = $CONFIG["backupPath"].”/”.$cleanTitle.”.”.$docType["format"];
interesting. Thank you for the script. I will try it out. Can you tell me what these file refer to?
require_once(’Zend/Gdata.php’);
require_once(’Zend/Gdata/ClientLogin.php’);
require_once(’Zend/Gdata/Docs.php’);
Do I have to create those?
I’m not a programmer, but I can somewhat follow the code. I know how to run a php script.
I take it the Zend framework is part of a typical server setup right?
If I can figure this out I would be happy to write an instruction sheet for peeps like me.
Thanks,
Mark
If you want it works, you could get my script (script.php) and put it in a directory called for example myscript/.
Then download Zend framework and put it in myscript/Zend so that exists the path myscript/Zend/Gdata.php.
Now run php script.php and it should work.
Backing Up Google Docs automatically from Linux…
I’m not quite sure why a working example of doing this is so hard to find!
As far as I can tell, at the time of this writing there are two choices of ready-made (and free) apps that do this: GDocBackup and php-google-backup, a tiny php script (on…