How to generate a backup of Google Docs files?

I’m trying to make a backup of my Google Docs files but I’m running into trouble. Here’s the code I’m using:

function backupDocs() {
  var authSetup = UrlFetchApp.addOAuthService('googleDocs');
  authSetup.setAccessTokenUrl('https://accounts.google.com/o/oauth2/token');
  authSetup.setRequestTokenUrl('https://accounts.google.com/o/oauth2/auth?scope=https://docs.google.com/feeds/');
  authSetup.setAuthorizationUrl('https://accounts.google.com/o/oauth2/auth');
  authSetup.setConsumerKey(myConsumerKey);
  authSetup.setConsumerSecret(mySecretKey);

  var requestOptions = {
    'method': 'POST',
    'headers': { 'GData-Version': '3.0' },
    'oAuthServiceName': 'googleDocs',
    'payload': createXmlPayload(),
    'oAuthUseToken': 'always'
  };

  var apiEndpoint = 'https://docs.google.com/feeds/default/private/backup';
  var response = UrlFetchApp.fetch(apiEndpoint, requestOptions);
}

function createXmlPayload() {
  var xmlContent = '<?xml version="1.0" encoding="UTF-8"?>' +
    '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:docs="http://schemas.google.com/docs/2007">' +
    '<docs:backupFormat source="application/vnd.google-apps.document" target="application/msword"/>' +
    '<docs:backupFormat source="application/vnd.google-apps.spreadsheet" target="text/csv"/>' +
    '<docs:backupFormat source="application/pdf" target="application/pdf"/>' +
    '</entry>';
  Logger.log(xmlContent);
  return xmlContent;
}

When I run this, I get an error saying ‘unexpected error in UrlFetchApp.fetch()’. I’m not sure what’s going wrong. Does anyone know how to fix this or suggest a better approach to backing up Google Docs files? Any help would be great. Thanks!

hey there, have u tried using the google drive api instead? its way more reliable for backups. i had similar issues before switching. you’ll need to enable the api, get credentials, and use a library like pythons google-api-python-client. its a bit more work but totally worth it for reliable backups!

From my experience, using the Google Drive API for backing up Google Docs files has proven far more reliable than older methods. Begin by enabling the API in your Google Cloud Console and setting up OAuth credentials. I used the google-api-python-client for Python, which streamlined the process considerably. First, set up authentication and create a Drive API service. Then, list your files and incorporate code to download them. For instance, consider the following outline:

from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow

# Set up authentication (add your code here)

# Create Drive API service
service = build('drive', 'v3', credentials=creds)

# List and download files (insert download code here)

This method is much more dependable than using outdated APIs. Remember to address pagination in case of numerous files and to implement robust error handling.

I’ve been down this road before, and I feel your pain with the Google Docs backup struggle. From my experience, the Google Drive API is definitely the way to go. I switched to it a while back and it’s been a game-changer.

Here’s what worked for me: I set up a Python script using the google-api-python-client library. It was a bit of a learning curve at first, but once I got it running, it’s been super reliable. I schedule it to run weekly, and it backs up all my Docs, Sheets, and Slides without a hitch.

One tip: make sure you’re using the latest version of the API and double-check your OAuth setup. That tripped me up initially. Also, consider backing up to both local storage and cloud storage for extra peace of mind.

If coding isn’t your thing, there are some solid third-party tools out there too. Just be careful with permissions and read reviews before committing to one.