Update: I now use a slightly different version of this script, which creates a single zip file instead of one per document, and puts a timestamp in the filename rather than overwriting the previous backup file. That version can be found at https://github.com/brokensandals/export-google-docs.
Google Apps Script that exports all your Google Docs/Sheets/Slides into docx/xlsx/pptx files and PDFs into a folder in your Google Drive. For more info and step-by-step setup instructions, see here: http://brokensandals.net/google-docs-backup
Replace INSERT_FOLDER_ID_HERE with the ID of the folder you want backups to be placed in.
Create a trigger to run the backupAll
function if you want to do this on a schedule (e.g. nightly).
Notes:
- By default, only files that you own (as opposed to files others have shared with you) will be backed up.
Remove the
file.getOwner()
check from thebackupAll
method if you want to change that. - For each file, both an Office file (docx/xlsx/pptx) and a PDF are generated, and combined into a zip file that's placed in the backup folder. Zipping the backup files ensures that they don't clutter up the recent activity list for Docs/Sheets/Slides.
- The script depends on the lastUpdated dates being correct on both the input files and the files in the backup directory.
If that seems problematic, you could change the
createOrUpdateFileForBlob
method to delete existing backup files rather than updating them.
As always, this code may have defects that prevent it from working properly. Use at your own risk and remember to periodically verify that your backups are actually working as expected.
Hi @freetimecoder! Good find regarding exportLinks, I didn't realize that existed. Using that, I was able to get the script to download exports of files larger than 10MB, but it fails when it goes to upload them. I think this is just a limitation of UrlFetchApp, or maybe just the particular way I'm using UrlFetchApp, not the Google Drive API itself. I can't devote any more time to it right now, but here's the code in case you want to play with it: https://github.com/brokensandals/export-google-docs/tree/zip-per-document-large-files (the
updateFile
method is what would have to be fixed).(If you get that working, though, you may still run into another issue if you have a lot of large files: there's a limit to how long a script can run, and streaming a bunch of large files to/from the drive API might take too long.)
Regarding retaining the folder structure, that would take some work. One option would be to use the https://developers.google.com/apps-script/reference/drive/file#getParents() method on the
file
objects to find the parent folder, and walk that recursively until you get to the root, building a list of all the folders along the way. (This is complicated by the fact that a file can have multiple parents, but if you never use that feature you could just use the first parent. I think Google is removing the multiple-parents feature later this year anyway.) Then you could go back through the list and use https://developers.google.com/apps-script/reference/drive/folder#createFolder(String) to create corresponding folders inside the backup directory.Alternatively, you could change the
backupAll
method so that instead of just requesting all files for a given mime type, it starts at https://developers.google.com/apps-script/reference/drive/drive-app#getRootFolder() and walks the directory tree recursively, so that you'd always know where you are in the tree. Then you can create corresponding folders in the backup directory as you go along.