Backup Web Pages to Google Drive Automatically

This Google Script will take a daily snapshot of a particular web page and saves it as an HTML file in the Google Drive. The code can be extended to backup your native Google Document in standard formats like PDF.

The Google Script by @hijonathan will create daily (or hourly or weekly) backups of any web pages and save it as an HTML file in your Google Drive. You can choose to overwrite the existing file or the backup process can create new copies. The files are saved in date-based folders making it easier for you to retrieve the backup for any particular day.

You’ll have to specify the web page URL and then create a time-based trigger that will run the createBackup() method at specified intervals. Also, only the HTML content of the web page is saved and not the CSS, JS or other associated files.

/* Credit: https://github.com/hijonathan */

var RESOURCE_URL = 'https://news.google.com',
  BACKUP_FOLDER_ID = '',
  FOLDER_NAME_FORMAT = 'yyyy-MM-dd',
  FILE_NAME_FORMAT = "yyyy-MM-dd'T'HH:00:00",
  // Customize your file extension.
  FILE_EXT = '.html',
  // Folder names are all going to look like this.
  now = new Date(),
  FOLDER_NAME = Utilities.formatDate(now, 'GMT', FOLDER_NAME_FORMAT),
  FILE_NAME = Utilities.formatDate(now, 'GMT', FILE_NAME_FORMAT) + FILE_EXT;

function createBackup() {
  var folder = getFolder(FOLDER_NAME);
  createBackupFile(folder, FILE_NAME, fetchData());
}

// Ensures we're always working within the backup directory.
function getFolder(name) {
  var backupFolder = getBackupFolder(),
    folders = backupFolder.getFoldersByName(name);

  if (folders.hasNext()) {
    folder = folders.next();
  } else {
    folder = backupFolder.createFolder(name);
  }
  return folder;
}

// Returns the root folder where our backups exist.
function getBackupFolder() {
  return DriveApp.getFolderById(BACKUP_FOLDER_ID);
}

function createBackupFile(folder, filename, data, overwrite) {
  if (overwrite) {
    // Technically we're not overwriting here. We're just deleting
    // the duplicates.
    var existingFiles = folder.getFilesByName(filename);
    while (existingFiles.hasNext()) {
      var file = existingFiles.next();
      folder.removeFile(file);
    }
  }
  folder.createFile(filename, data);
}

function fetchData() {
  var exportUrl = RESOURCE_URL;
  return UrlFetchApp.fetch(exportUrl);
}

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫶🏻