In this example 45 daily backups are stored on Google Drive. While keeping only 7 backups in HA. You can change these numbers.
Once daily Google Apps Script copying new HA backup Google Drive Backups to an archive folder, then checking that folder to delete files over 45 days old.
Automate with Google Apps Script
Google Apps Script is a free, cloud-based scripting platform that integrates with Google Drive. Here’s how to set it up: (This assumes you have Google Drive Setup in HA already)
Step 1: Set Up Your Google Drive Folders
- Source Folder: This is where Home Assistant uploads its backups (e.g., “Home Assistant”).
- Archive Folder: Create a second folder for long-term storage (e.g., “Home Assistant Archive”).
Step 2: Create a Google Apps Script
- Go to script.google.com and sign in with your Google account.
- Click New Project.
• 3. Replace the default code with the following script (customize the folder IDs and retention period as needed):
function manageBackups() {
// Replace these with your folder IDs (find them in the URL of the folder in Google Drive)
var sourceFolderId = "YOUR_SOURCE_FOLDER_ID"; // e.g., "1aBcDeFgHiJkLmNoPqRsTuVwXyZ"
var archiveFolderId = "YOUR_ARCHIVE_FOLDER_ID"; // e.g., "1xYzAbCdEfGhIjKlMnOpQrStUvW"
// Get the folders
var sourceFolder = DriveApp.getFolderById(sourceFolderId);
var archiveFolder = DriveApp.getFolderById(archiveFolderId);
// Step 1: Copy new files from source to archive
var sourceFiles = sourceFolder.getFiles();
while (sourceFiles.hasNext()) {
var file = sourceFiles.next();
var fileName = file.getName();
// Check if file already exists in archive to avoid duplicates
var archiveFiles = archiveFolder.getFilesByName(fileName);
if (!archiveFiles.hasNext()) {
file.makeCopy(fileName, archiveFolder);
}
}
// Step 2: Delete files older than 45 days in archive
var cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - 45); // 45 days ago
var archiveFiles = archiveFolder.getFiles();
while (archiveFiles.hasNext()) {
var file = archiveFiles.next();
var lastUpdated = file.getLastUpdated();
if (lastUpdated < cutoffDate) {
file.setTrashed(true); // Moves to trash
}
}
}
- Find Folder IDs:
- Open each folder in Google Drive.
- The ID is the string in the URL after /folders/ (e.g., https://drive.google.com/drive/folders/1aBcDeFgHiJkLmNoPqRsTuVwXyZ → ID is 1aBcDeFgHiJkLmNoPqRsTuVwXyZ).
- Replace YOUR_SOURCE_FOLDER_ID and YOUR_ARCHIVE_FOLDER_ID with the respective IDs.
Step 3: Schedule the Script
- In the Google Apps Script editor, click the clock icon (Triggers) on the left.
- Click + Add Trigger (bottom right).
- Configure:
- Function: manageBackups
- Event Source: Time-driven
- Type: Day timer (or adjust to your preference, e.g., every day).
- Time: Choose a time when backups are likely complete (e.g., 3 AM).
- Save the trigger.
Step 4: Configure Home Assistant Retention
- In Home Assistant, go to Settings > System > Backups.
- Set the retention to keep only 7 backups locally (this is managed by Home Assistant’s native backup settings or your current method).
- Ensure backups are still uploaded to the “Home Assistant Backups” folder in Google Drive (via your existing method).
How It Works
- Home Assistant keeps 7 backups locally and uploads them to the source folder.
- The script runs daily (or as scheduled), copying new backups to the archive folder and deleting anything older than 45 days from the archive.
• • Over time, the archive folder will accumulate up to 45 backups (if daily), while the source folder mirrors Home Assistant’s 7-backup limit.