Backing up your data is one of the most critical habits you can develop. While Google Drive offers a reliable cloud storage solution, having an automated backup mechanism ensures your important files are duplicated and safe from accidental deletion. Instead of relying on third-party services that require monthly subscriptions, you can automate Google Drive backups entirely for free using Google Apps Script.
Google Apps Script is a powerful, JavaScript-based platform that allows you to interact directly with Google Workspace services. In this guide, you will learn how to create a script that automatically copies files from one Google Drive folder to a backup folder on a regular schedule.
Why Automate Backups with Google Apps Script?
- Cost-Effective: It is completely free to use since it relies on your existing Google account.
- Customisable: You have full control over which folders are backed up and how often the backup runs.
- Secure: The script runs natively within your Google account, meaning you do not need to grant access to external, third-party applications.
Prerequisites for the Backup Script
Before you begin, ensure you have the following:
- A Google account (either a personal Gmail account or a Google Workspace account).
- A source folder in your Google Drive containing the files you wish to back up.
- A destination folder where the backups will be stored.
Pro Tip: To make the process easier, copy the Folder IDs of both your source and destination folders. You can find the Folder ID in the URL when you open the folder in Google Drive (it is the long string of characters after /folders/).
Step-by-Step Guide to Automating Google Drive Backups
Step 1: Open Google Apps Script
- Navigate to your Google Drive.
- Click on New in the top-left corner, go to More, and select Google Apps Script.
- If you do not see Apps Script in the menu, click on Connect more apps, search for “Google Apps Script”, and install it.
- Once the script editor opens, name your project something descriptive, such as “Drive Backup Automation”.
Step 2: Add the Backup Script
Delete any existing code in the editor and paste the following script. Be sure to replace the placeholder Folder IDs with your actual IDs.
function backupGoogleDriveFolder() {
// Replace these with your actual folder IDs
var sourceFolderId = 'YOUR_SOURCE_FOLDER_ID';
var backupFolderId = 'YOUR_BACKUP_FOLDER_ID';
var sourceFolder = DriveApp.getFolderById(sourceFolderId);
var backupFolder = DriveApp.getFolderById(backupFolderId);
var files = sourceFolder.getFiles();
while (files.hasNext()) {
var file = files.next();
// Create a timestamp to append to the backup file name
var timestamp = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy-MM-dd HH:mm:ss");
var backupFileName = file.getName() + " - Backup " + timestamp;
// Copy the file to the backup folder
file.makeCopy(backupFileName, backupFolder);
}
Logger.log('Backup completed successfully.');
}
This script identifies all files in your source folder, creates a copy of each file, appends the current date and time to the filename to avoid confusion, and saves the copied file into your destination folder.
Step 3: Test the Script
Before automating the process, it is essential to ensure the script works correctly.
- Click the Save icon (floppy disk) in the toolbar.
- Click the Run button.
- Google will prompt you to authorise the script. Click Review permissions, select your Google account, and click Allow. (If you see a “Google hasn’t verified this app” warning, click Advanced and then Go to Drive Backup Automation).
- Check your destination folder in Google Drive. You should see copies of your files with the current timestamp appended to their names.
Step 4: Automate the Backup Schedule
Now that the script is working, you can set it to run automatically on a schedule using Time-driven Triggers.
- In the Apps Script editor, click the Triggers icon (it looks like an alarm clock) on the left-hand sidebar.
- Click the Add Trigger button in the bottom right corner.
- Set the following options:
- Choose which function to run: backupGoogleDriveFolder
- Select event source: Time-driven
- Select type of time based trigger: Choose your preferred frequency (e.g., Day timer, Week timer).
- Select time of day: Choose a time when you are less likely to be actively working in the folder (e.g., Midnight to 1 AM).
- Click Save.
Best Practices and Limitations
While this method is highly effective for backing up individual folders, there are a few limitations to keep in mind. Google Apps Script has a maximum execution time of 6 minutes per run. If your source folder contains hundreds of large files, the script may time out before completing the backup. To avoid this, use this script for specific, high-priority folders rather than your entire Google Drive.
Additionally, this basic script only copies files directly inside the source folder. It does not recursively back up subfolders. If you need to back up a complex folder structure, you would need a more advanced script that iterates through subdirectories.
By implementing this automated backup solution, you add an essential layer of security to your most important documents, ensuring that accidental deletions or modifications never result in permanent data loss.