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

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

0

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

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

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

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

We will never send any spam emails. Promise.