appscript.dev
Automation Intermediate Drive

Build a scheduled backup of key folders

Copy Northwind's critical Drive folders to an archive folder on a weekly cadence.

Published Jul 8, 2025

A couple of Northwind’s Drive folders hold work that would genuinely hurt to lose — signed contracts and the invoice archive. Drive itself is reliable, but it does not protect against the everyday accidents: a file dragged into the wrong place, a folder deleted by someone who thought it was a duplicate, an edit that overwrites the wrong version.

This script takes a weekly snapshot. It creates a dated folder inside an archive root, then deep-copies each critical folder into it — every file and every sub-folder. Because each backup lives in its own dated folder, you keep a history of weekly snapshots and can always reach back to how things looked before a mistake.

What you’ll need

  • The Drive folder IDs of every folder you want backed up. A folder ID is the last part of its URL when open in Drive.
  • A single archive root folder where the dated snapshots will be created, and its folder ID.
  • Enough Drive storage for the snapshots — each weekly run duplicates the full contents of every source folder.

The script

// The folders to back up each run, by Drive folder ID.
const SOURCE_FOLDERS = ['1abcContractsId', '1abcInvoicesId'];

// The archive root: each run creates a dated snapshot folder inside it.
const ARCHIVE_ROOT = '1abcArchivesId';

/**
 * Creates a dated snapshot folder in the archive root and deep-copies
 * every source folder into it.
 */
function backupKeyFolders() {
  // 1. Create a snapshot folder named for today's date.
  const stamp = Utilities.formatDate(new Date(), 'GMT', 'yyyy-MM-dd');
  const snapshot = DriveApp.getFolderById(ARCHIVE_ROOT).createFolder(stamp);

  // 2. Deep-copy each source folder into the snapshot.
  for (const id of SOURCE_FOLDERS) {
    const source = DriveApp.getFolderById(id);
    copyFolder(source, snapshot.createFolder(source.getName()));
    Logger.log(`Backed up: ${source.getName()}`);
  }
  Logger.log(`Snapshot complete: ${stamp}`);
}

/**
 * Recursively copies every file and sub-folder from src into dest.
 *
 * @param {Folder} src  The folder being copied from.
 * @param {Folder} dest The matching folder being copied into.
 */
function copyFolder(src, dest) {
  // Copy every file in this folder.
  const files = src.getFiles();
  while (files.hasNext()) {
    files.next().makeCopy(dest);
  }

  // Recurse into every sub-folder, recreating it inside dest.
  const subFolders = src.getFolders();
  while (subFolders.hasNext()) {
    const sub = subFolders.next();
    copyFolder(sub, dest.createFolder(sub.getName()));
  }
}

How it works

  1. backupKeyFolders formats today’s date into a yyyy-MM-dd stamp and creates a folder with that name inside the archive root — this is the week’s snapshot.
  2. It loops over every ID in SOURCE_FOLDERS, opening each source folder.
  3. For each one it creates a matching sub-folder inside the snapshot, then hands both folders to copyFolder.
  4. copyFolder iterates the source folder’s files and copies each into the destination with makeCopy.
  5. It then iterates the source folder’s sub-folders, recreates each one inside the destination, and calls itself on the pair — so the copy descends the entire tree.
  6. The result is a complete, dated mirror of every critical folder, sitting safely in the archive root.

Example run

Suppose the Contracts folder contains three PDFs and a Signed sub-folder, and Invoices contains twelve files. A run on 2025-07-08 produces:

Archives/
  2025-07-08/
    Contracts/
      contract-acme.pdf
      contract-beltran.pdf
      contract-corso.pdf
      Signed/
        ...
    Invoices/
      inv-0001.pdf
      ... (12 files)

The following Monday a 2025-07-15/ folder appears alongside it, giving Northwind a fresh weekly snapshot without disturbing the previous one.

Trigger it

Run this once a week so a snapshot is always at most seven days old:

  1. In the Apps Script editor open Triggers (the clock icon).
  2. Add a trigger for backupKeyFolders, time-driven, Week timer, on a quiet day such as Sunday, set to an overnight hour.
  3. Save and approve the Drive authorisation prompt.

Watch out for

  • Storage adds up fast. Every run duplicates the full contents of every source folder, so weekly snapshots of large folders will fill the account’s quota. Prune old snapshots on a schedule, or keep only the last few.
  • Apps Script has a six-minute execution limit. A folder tree with thousands of files can time out mid-copy, leaving a partial snapshot — split very large folders across separate runs.
  • makeCopy copies file content but not sharing settings, comments, or revision history. The backup is the data, not a perfect clone of every file’s metadata.
  • Drive has a daily file-creation quota. A big backup creates one new file per source file and can hit that ceiling.
  • Files in the source folders that are only shared with you (not owned by you) can still be copied, but the copy is owned by the script’s account — fine for a backup, worth knowing.
  • The script does not de-duplicate against earlier snapshots; each run is a full copy, not an incremental one.

Related