Last active
May 11, 2022 11:47
-
-
Save diogotito/a3d81b096d0e63553405e4ea9d7a8315 to your computer and use it in GitHub Desktop.
Google Apps Script to zip all revisions of all files in a directory and put it somewhere
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Drive service needed (in the sidebar, click the "+" next to "Services", select "Drive", then click "Add") | |
// To get a folder/file ID: Right click on Drive, Get link, manually copy the last part | |
const FOLDER_TO_BACKUP_ID = ScriptProperties.getProperty("Source Folder ID") || "... default folder ID ..." | |
const FOLDER_DEST_ID = ScriptProperties.getProperty("Dest Folder ID") || "... default folder ID ..." | |
const ZIP_NAME = "SOMETHING_backups.zip" | |
function zipAllFileVersionsInDirectory() { | |
let myFolder = DriveApp.getFolderById(FOLDER_TO_BACKUP_ID) | |
let filesIterator = myFolder.getFiles() | |
let blobsToZip = [] | |
while (filesIterator.hasNext()) { | |
let file = filesIterator.next() | |
Drive.Revisions.list(file.getId()).items.forEach((rev, i, {length}) => { | |
console.log(`Pinning and fetching "${file.getName()}" revision [${i + 1}/${length}]`) | |
Drive.Revisions.update({pinned: true}, file.getId(), rev.id) | |
let revBlob = UrlFetchApp.fetch(rev.downloadUrl, { | |
headers: { | |
Authorization: "Bearer " + ScriptApp.getOAuthToken() | |
} | |
}).getBlob() | |
revBlob.setName( file.getName() // Directory inside zip | |
+ "/" + rev.modifiedDate.replaceAll(':', '_') // Use modified date as file name inside zip | |
+ "." + file.getName().match(/\.[^.]*$/)[0] ) // Keep extension | |
blobsToZip.push(revBlob) | |
}) | |
} | |
console.log(`Creating ${ZIP_NAME}`) | |
let zipBlob = Utilities.zip(blobsToZip, ZIP_NAME) | |
let folder = DriveApp.getFolderById(FOLDER_DEST_ID) | |
console.log(`Storing zip at ${folder.getName()}/ (${folder.getUrl()})`) | |
folder.createFile(zipBlob) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment