How to make a backup of Google Docs files?

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

function backupDocs() {
  const authSetup = OAuthConfig.create('google_docs');
  authSetup.setTokenUrls('https://accounts.google.com/o/oauth2/token', 'https://accounts.google.com/o/oauth2/auth');
  authSetup.setScope('https://www.googleapis.com/auth/drive.readonly');
  authSetup.setCredentials(clientId, clientSecret);

  const requestOptions = {
    method: 'POST',
    headers: { 'API-Version': '1.0' },
    authService: 'google_docs',
    payload: generateXmlPayload(),
    useAuth: true
  };

  const endpoint = 'https://docs.google.com/api/backup';
  const response = FetchService.send(endpoint, requestOptions);
}

function generateXmlPayload() {
  const xmlContent = `
    <?xml version="1.0" encoding="UTF-8"?>
    <backup:request xmlns:backup="http://api.example.com/backup/2023">
      <backup:format>
        <backup:document>doc</backup:document>
        <backup:spreadsheet>csv</backup:spreadsheet>
        <backup:other>pdf</backup:other>
      </backup:format>
    </backup:request>
  `;
  console.log(xmlContent);
  return xmlContent;
}

When I run this, I get an error saying ‘unexpected error in FetchService.send()’. Does anyone know what I’m doing wrong or if there’s a better way to do this? Thanks for any help!

hey, i’ve had similar issues. google’s api can be tricky. have u tried using the official google drive api instead? it’s easier to work with for backups. also, make sure ur oauth credentials are set up correctly in the google cloud console. that could be causing the fetch error.

I’ve been down this road before, and I can tell you from experience that using the Google Drive API is your best bet for backing up Google Docs files. Here’s what worked for me:

First, set up a Google Cloud project and enable the Drive API. Then, use the google-auth-library and googleapis npm packages in your Node.js script. You’ll need to create a service account and download the JSON key file.

The key is to use the drive.files.export method to convert and download each Doc. You can iterate through your files, export them to the desired format (I prefer PDF for docs and XLSX for sheets), and save them to your local drive.

One gotcha: watch out for rate limits. I learned the hard way to implement exponential backoff for large backups. Also, consider using streams for efficiency when dealing with large files.

This approach has been rock-solid for me, running weekly backups for over a year now. Hope this helps!

I encountered a similar issue when trying to back up my Google Docs files. Instead of using a custom script, I found success with Google Takeout. It’s a built-in Google service that allows you to export and download your data, including Google Docs files, in various formats. You can select specific files or export everything at once. The process is straightforward and doesn’t require any coding. Once you’ve downloaded your files, you can store them locally or upload them to another cloud service for redundancy. This method has been reliable for me and might be worth considering as an alternative to your current approach.