|
var BACKUP_FOLDER_ID = 'INSERT_FOLDER_ID_HERE'; |
|
|
|
var NATIVE_MIME_TYPES = {}; |
|
NATIVE_MIME_TYPES[MimeType.GOOGLE_DOCS] = MimeType.MICROSOFT_WORD; |
|
NATIVE_MIME_TYPES[MimeType.GOOGLE_SHEETS] = MimeType.MICROSOFT_EXCEL; |
|
NATIVE_MIME_TYPES[MimeType.GOOGLE_SLIDES] = MimeType.MICROSOFT_POWERPOINT; |
|
|
|
var NATIVE_EXTENSIONS = {}; |
|
NATIVE_EXTENSIONS[MimeType.GOOGLE_DOCS] = 'docx'; |
|
NATIVE_EXTENSIONS[MimeType.GOOGLE_SHEETS] = 'xlsx'; |
|
NATIVE_EXTENSIONS[MimeType.GOOGLE_SLIDES] = 'pptx'; |
|
|
|
//Really these urls should be queried via exportLinks, but it's faster to hard code |
|
var NATIVE_EXPORT_URLS = {}; |
|
NATIVE_EXPORT_URLS[MimeType.GOOGLE_DOCS] = "https://docs.google.com/feeds/download/documents/export/Export?id="; |
|
NATIVE_EXPORT_URLS[MimeType.GOOGLE_SHEETS] = "https://docs.google.com/spreadsheets/export?id="; |
|
NATIVE_EXPORT_URLS[MimeType.GOOGLE_SLIDES] = "https://docs.google.com/feeds/download/presentations/Export?id="; |
|
|
|
//Only backup owned files, not shared files |
|
var ONLY_OWNED = true; |
|
|
|
var BACKUP_MIME_TYPES = Object.keys(NATIVE_MIME_TYPES); |
|
|
|
function backupAll() { |
|
const backupFolder = DriveApp.getFolderById(BACKUP_FOLDER_ID); |
|
|
|
BACKUP_MIME_TYPES.forEach(function(mimeType) { |
|
|
|
//Iterate through all files on the drive |
|
var files = DriveApp.getFilesByType(mimeType); |
|
while (files.hasNext()) { |
|
var file = files.next(); |
|
|
|
//If owner check is enabled, skip files that are not owned |
|
if(ONLY_OWNED && file.getOwner()) { |
|
if (file.getOwner().getEmail() != Session.getActiveUser().getEmail()) { |
|
continue; |
|
} |
|
} |
|
|
|
//Check if older backups exist of this file |
|
var existingFiles = backupFolder.getFilesByName(file.getName()+"."+NATIVE_EXTENSIONS[mimeType]) |
|
if(existingFiles.hasNext()) { |
|
var existingFile = existingFiles.next(); |
|
|
|
//Don't backup if existing backup is up to date |
|
if(file.getLastUpdated() < existingFile.getLastUpdated()) |
|
{ |
|
continue; |
|
} |
|
} |
|
|
|
//Backup file |
|
backup(file, backupFolder); |
|
} |
|
}); |
|
} |
|
|
|
function backup(file, folder) { |
|
try { |
|
var native = getNativeBlob(file); |
|
createOrUpdateFileForBlob(native, folder); |
|
} catch (e) { |
|
// Logs an ERROR message. |
|
console.error('backup() yielded an error: ' + e); |
|
} |
|
} |
|
|
|
function createOrUpdateFileForBlob(blob, folder) { |
|
var existingFiles = folder.getFilesByName(blob.getName()); |
|
if (existingFiles.hasNext()) { |
|
var file = existingFiles.next(); |
|
updateFile(file, blob); |
|
} else { |
|
var tmp = Utilities.newBlob([], blob.getContentType(), blob.getName()); |
|
// folder.createFile can only handle blobs up to 10MB, so we'll create |
|
// an empty file first and then upload to it. |
|
// We could use the upload API to create and upload the file at the same |
|
// time, but that's a bit more work. |
|
var file = folder.createFile(tmp); |
|
updateFile(file, blob); |
|
} |
|
} |
|
|
|
//Upload file over existing file |
|
function updateFile(file, blob) { |
|
const url = 'https://www.googleapis.com/upload/drive/v2/files/' + file.getId() + '?uploadType=media'; |
|
|
|
const params = { |
|
method: 'put', |
|
headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() }, |
|
payload: blob |
|
}; |
|
|
|
var response = UrlFetchApp.fetch(url, params); |
|
|
|
if (response.getResponseCode() < 200 || response.getResponseCode() > 299) { |
|
throw 'Failed to update file named ' + file.getName(); |
|
} |
|
} |
|
|
|
function getNativeBlob(file) { |
|
const nativeMimeType = NATIVE_MIME_TYPES[file.getMimeType()]; |
|
const extension = NATIVE_EXTENSIONS[file.getMimeType()]; |
|
const exportURL = NATIVE_EXPORT_URLS[file.getMimeType()]; |
|
|
|
//Construct query url |
|
const url = exportURL + file.getId() + '&exportFormat='+extension; |
|
|
|
const params = { |
|
method: 'get', |
|
headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() } |
|
}; |
|
|
|
//Download converted file |
|
const blob = UrlFetchApp.fetch(url, params).getBlob(); |
|
|
|
var fileName = file.getName() +"."+ extension; |
|
|
|
//Set filename |
|
blob.setName(fileName); |
|
return blob; |
|
} |
Great code, thanks a lot!
I have a lot of folders (directories). And this script backs all the docs up into one single file and ignores the directories. Could you please help to adjust the script so it keeps all the folders and structure?
Also, I'm trying to get rid of the pdf duplicates..
Thanks in advance!