Google Drive AutomationDifficulty: Beginner⚡ Time-driven (Nightly 1 AM)

Automated Daily Spreadsheet Backup to Google Drive Archive

Creates a point-in-time timestamped backup copy of a critical production Google Sheet inside a dedicated backup folder every night.

Required OAuth Scopes: https://www.googleapis.com/auth/drive • https://www.googleapis.com/auth/spreadsheets
Code.gs (Apps Script V8 Runtime)
function backupSpreadsheetDaily() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const backupFolderId = 'PASTE_BACKUP_FOLDER_ID_HERE';
  const backupFolder = DriveApp.getFolderById(backupFolderId);
  
  const timestamp = Utilities.formatDate(new Date(), 'GMT', 'yyyy-MM-dd_HHmmss');
  const backupName = ss.getName() + '_Backup_' + timestamp;
  
  DriveApp.getFileById(ss.getId()).makeCopy(backupName, backupFolder);
}

Line-by-Line Code Explanation

Line 2-4: Resolves source spreadsheet and destination backup folder.
Line 6-7: Formats timestamp string for clean archival sorting.
Line 9: Creates an exact snapshot copy.

Step-by-Step Setup & Deployment

  1. Create a "Sheet Backups" folder in Google Drive and paste its ID in Line 3.
  2. Set a time-driven trigger to run daily at 1 AM.

Best Practices for Quota & Rate Limit Management

When deploying this automation in Google Workspace, adhere to these operational principles:

  • Batch Reads & Writes: Never execute getValue() or setValue() inside loops. Read ranges into memory in 1 call, process arrays in V8 memory, and commit changes with a single setValues() call.
  • Execution Timeouts: Apps Script terminates scripts exceeding 6 minutes. For multi-thousand row datasets, implement batch pagination with resume timestamps stored in PropertiesService.
  • OAuth Permissions: Scope your manifest (appsscript.json) narrowly to prevent security prompts from alarming external team members.

Frequently Asked Questions

Is Google Apps Script completely free to use?

Yes. Google Apps Script is built into all free Google accounts and Google Workspace editions. There are no server fees or subscription costs for running scripts within daily quotas.

Can I run this script on an automated schedule without keeping my computer on?

Yes. Time-driven triggers execute on Google's cloud infrastructure. Your script will run reliably even when your computer is closed or offline.

More Google Drive Automation Snippets