appscript.dev
Automation Intermediate Drive

Build a version-snapshot system for key files

Keep dated copies of Northwind's critical Sheets before risky edits.

Published Sep 22, 2025

A handful of files run Northwind: the invoices sheet, the contracts log, the sales pipeline. Google’s own revision history covers a lot, but it is awkward to browse, easy to overrun on a busy day, and useless if a file is deleted outright. When someone pastes over the wrong range, you want a clean copy from this morning — not a scroll through three hundred autosaves.

This script makes a dated copy of each key file into a snapshots folder. Run it on a daily trigger and you build up a shelf of restore points; run it by hand before a risky bulk edit and you have an instant undo. The copies are ordinary files, so recovering one is just a matter of opening it.

What you’ll need

  • The IDs of the files you want to snapshot — the string after /d/ in each file’s URL.
  • A dedicated folder to hold the snapshots, and its folder ID.
  • Edit access to every key file and the snapshots folder.
  • An eye on storage. Each run adds one copy per file; see “Watch out for”.

The script

// The files to snapshot on every run. Add or remove IDs as needed.
const KEY_FILE_IDS = ['1abcInvoicesId', '1abcContractsId', '1abcPipelineId'];

// The folder that collects the dated copies.
const SNAPSHOT_FOLDER_ID = '1abcSnapshotsFolderId';

// Timestamp format for the copy name. Minutes are included so two runs
// on the same day never collide.
const STAMP_FORMAT = 'yyyy-MM-dd-HHmm';

/**
 * Makes a dated copy of every key file into the snapshots folder.
 * Designed to run on a daily trigger, but safe to run by hand too.
 */
function snapshotKeyFiles() {
  // 1. Resolve the snapshots folder. Bail out if the ID is wrong.
  let folder;
  try {
    folder = DriveApp.getFolderById(SNAPSHOT_FOLDER_ID);
  } catch (err) {
    Logger.log('Could not open the snapshots folder — check SNAPSHOT_FOLDER_ID.');
    return;
  }

  // 2. Bail out if there is nothing configured to snapshot.
  if (!KEY_FILE_IDS.length) {
    Logger.log('No key files configured — nothing to snapshot.');
    return;
  }

  // 3. Build one timestamp for the whole run so every copy shares it.
  const stamp = Utilities.formatDate(new Date(), 'GMT', STAMP_FORMAT);

  // 4. Copy each file in turn, skipping any that cannot be opened.
  let copied = 0;
  for (const id of KEY_FILE_IDS) {
    try {
      const file = DriveApp.getFileById(id);
      file.makeCopy(`${file.getName()} ${stamp}`, folder);
      copied++;
    } catch (err) {
      Logger.log(`Skipped ${id} — could not copy it: ${err.message}`);
    }
  }

  Logger.log(`Snapshot complete: copied ${copied} of ${KEY_FILE_IDS.length} file(s).`);
}

How it works

  1. snapshotKeyFiles resolves SNAPSHOT_FOLDER_ID inside a try/catch. If the folder cannot be opened, it logs a clear message and stops before touching anything.
  2. It checks that KEY_FILE_IDS is not empty — an empty list is a misconfigured script, not a reason to run silently.
  3. It builds a single timestamp with STAMP_FORMAT at the start of the run, so every copy from that run carries the same stamp and sorts together.
  4. It loops over the file IDs, opening each one and calling makeCopy with a name of <original name> <stamp> into the snapshots folder. Each copy is wrapped in its own try/catch, so one missing file does not stop the rest.
  5. It logs how many files were copied versus how many were configured, which makes a partial failure obvious in the trigger’s execution history.

Example run

Say the three key files are named Invoices, Contracts, and Pipeline, and the script runs at 06:00 on 22 September 2025. The snapshots folder gains three new files:

New copy in the snapshots folder
Invoices 2025-09-22-0600
Contracts 2025-09-22-0600
Pipeline 2025-09-22-0600

The next day’s run adds another three with a 2025-09-23-0600 stamp. Because the timestamp leads the sort within each name, the snapshots line up chronologically in the folder.

Trigger it

Run this daily so a restore point always exists from earlier in the day:

  1. In the Apps Script editor, open Triggers (the clock icon).
  2. Click Add Trigger.
  3. Choose snapshotKeyFiles, event source Time-driven, type Day timer, and a quiet hour such as midnight to 1am.
  4. Save. Before any risky bulk edit, also run snapshotKeyFiles by hand for an immediate restore point.

Watch out for

  • Snapshots accumulate. A daily run on three files adds roughly ninety copies a month. Pair this with a cleanup that deletes copies older than a set age, or the snapshots folder will balloon and eat into Drive storage.
  • The minute is part of the name for a reason. If you trim STAMP_FORMAT down to the day only, two runs on the same day produce two files with identical names — Drive allows that, but it makes the right one hard to pick.
  • makeCopy duplicates the file as it stands at run time. It does not capture Google’s revision history, so a restored snapshot starts its own fresh history.
  • Copying is per-file and not atomic. If the script fails midway, you may have snapshotted some files and not others — the run log shows exactly which, so check it after a failed run.

Related