Backing up essential files is a fundamental part of digital security, yet many users still rely on manual uploads to keep their data safe. If you use Google Drive for personal or professional storage, manually copying files between folders or to external storage is not only tedious but prone to human error. Fortunately, you can fully automate this process using Google Apps Script—a powerful, JavaScript-based platform built directly into Google Workspace.
In this guide, we will walk you through how to automate Google Drive backups using Google Apps Script. You will learn how to write a simple script that automatically copies files from one folder to another on a scheduled basis, ensuring your critical documents are always backed up without requiring third-party software.
Why Use Google Apps Script for Drive Backups?
While there are numerous third-party backup solutions available, using Google Apps Script offers several unique advantages:
- Native Integration: It runs natively within the Google ecosystem, meaning you do not need to grant access to external, potentially insecure third-party applications.
- Cost-Effective: Google Apps Script is completely free to use with your existing Google Account.
- Customisation: You can tailor the script to backup specific file types, exclude certain documents, or run at precise intervals that suit your workflow.
- Cloud-Based Automation: The script runs on Google’s servers, so your computer does not need to be powered on for the backup to occur.
Prerequisites for Automating Your Backup
Before writing the script, ensure you have the following prepared:
- A Google Account with access to Google Drive.
- A Source Folder containing the files you wish to back up.
- A Destination Folder where the backed-up files will be stored.
You will need the unique Folder IDs for both the source and destination folders. You can find a Folder ID by opening the folder in Google Drive on your web browser and looking at the URL. The ID is the long string of characters following folders/.
Step-by-Step Guide: Writing the Backup Script
Step 1: Access Google Apps Script
To begin, you need to open the Apps Script editor:
- Open your web browser and navigate to script.google.com.
- Click on New project in the top left corner.
- Rename the project to something identifiable, such as “Drive Auto Backup”.
Step 2: Input the Automation Code
In the script editor, delete any existing code (usually an empty myFunction() block) and paste the following JavaScript code:
function backupDriveFolder() {
// Replace these with your actual folder IDs
var sourceFolderId = 'YOUR_SOURCE_FOLDER_ID_HERE';
var destinationFolderId = 'YOUR_DESTINATION_FOLDER_ID_HERE';
var sourceFolder = DriveApp.getFolderById(sourceFolderId);
var destinationFolder = DriveApp.getFolderById(destinationFolderId);
// Create a new backup folder with the current date
var date = new Date();
var backupFolderName = 'Backup - ' + Utilities.formatDate(date, Session.getScriptTimeZone(), 'yyyy-MM-dd HH:mm');
var newBackupFolder = destinationFolder.createFolder(backupFolderName);
// Get all files from the source folder
var files = sourceFolder.getFiles();
// Iterate through files and copy them to the new backup folder
while (files.hasNext()) {
var file = files.next();
file.makeCopy(file.getName(), newBackupFolder);
}
Logger.log('Backup completed successfully.');
}
Ensure you replace YOUR_SOURCE_FOLDER_ID_HERE and YOUR_DESTINATION_FOLDER_ID_HERE with the actual IDs you gathered earlier.
Step 3: Test and Authorise the Script
Before automating the process, you must run the script manually to grant it the necessary permissions:
- Click the Save icon (floppy disk) in the toolbar.
- Select
backupDriveFolderfrom the function dropdown menu and click Run. - A prompt will appear stating “Authorization required”. Click Review permissions.
- Select your Google Account. If you receive a “Google hasn’t verified this app” warning, click Advanced and then Go to Drive Auto Backup (unsafe).
- Click Allow to grant the script permission to view and manage your Google Drive files.
Once authorised, check your Destination Folder in Google Drive. You should see a newly created folder stamped with the current date and time, containing copies of your source files.
Step 4: Schedule the Automation (Time-Driven Triggers)
The true power of Google Apps Script lies in its ability to run automatically. We will use a Time-Driven Trigger to schedule our backup.
- In the Apps Script editor, click the Triggers icon on the left sidebar (it looks like an alarm clock).
- Click the Add Trigger button in the bottom right corner.
- Configure the trigger settings as follows:
- Choose which function to run:
backupDriveFolder - Choose which deployment should run:
Head - Select event source:
Time-driven - Select type of time based trigger:
Day timer(or Week/Month, depending on your needs) - Select time of day: Choose a quiet time, such as
Midnight to 1am.
- Choose which function to run:
- Click Save.
Your script will now run automatically at the specified interval, creating a dated backup of your files without any manual intervention.
Limitations and Best Practices
While this automation is highly effective, there are a few limitations to keep in mind:
- Execution Time Limits: Google Apps Script has a maximum execution time of 6 minutes per script. If your source folder contains hundreds of large files, the script may time out before completing. This solution is best suited for essential document backups rather than terabytes of video files.
- Storage Quotas: Because this script creates entirely new copies of your files every time it runs, it will consume your Google Drive storage quickly. Regularly review and delete older backups to free up space.
- Subfolders: The basic script provided above only copies files in the root of the source folder. It does not recursively copy subfolders.
By leveraging Google Apps Script, you can easily automate Google Drive backups, adding a robust layer of protection to your workflow while saving valuable time.