Skip to content

Instantly share code, notes, and snippets.

@samber
Last active November 11, 2017 22:53
Show Gist options
  • Select an option

  • Save samber/cbb090bc9fb0d5577f7c706ad016209d to your computer and use it in GitHub Desktop.

Select an option

Save samber/cbb090bc9fb0d5577f7c706ad016209d to your computer and use it in GitHub Desktop.
Google Apps Script for building a backup of a folder, from Google Drive to Dropbox, every night
// To store personal documents that really matter (such as tax reports, bank terms,
// employment conditions...) you should opt for a cloud storage (Dropbox, Google Drive...).
// Even if famous SaaS services might be safe enough to not make backups by ourselves, it
// may be even safer to not rely on them and setup this short script for copying a Drive folder
// into Dropbox.
// To generate a Dropbox access token, please follow this guide: http://99rabbits.com/get-dropbox-access-token/
// Don't forget to setup a cronjob:
// 1- Click on 'Edit' menu > 'Current project's triggers'
// 2- Click on 'No triggers set up. Click here to add one now.'
// 3- Select the 'main' function in the 'run' column
// 4- Setup a daily cron at '4am to 5am'
// 5- Click on 'notifications'
// 6- Request daily notifications at 5am
// 7- Click on 'Save' twice
// 8- Enjoy \o/
var driveFolderId = 'changeme';
var dropboxAccessToken = 'changeme';
function buildArchive() {
var folder = DriveApp.getFolderById(driveFolderId);
var zipFileName = folder.getName()+'.zip';
var zipped = Utilities.zip(getBlobs(folder, ''), zipFileName);
return zipped;
}
// https://stackoverflow.com/a/28018270
function getBlobs(rootFolder, path) {
var blobs = [];
var names = {};
var files = rootFolder.getFiles();
while (files.hasNext()) {
var file = files.next().getBlob();
var n = file.getName();
while(names[n]) { n = '_' + n }
names[n] = true;
blobs.push(file.setName(path+n));
}
names = {};
var folders = rootFolder.getFolders();
while (folders.hasNext()) {
var folder = folders.next();
var n = folder.getName();
while(names[n]) { n = '_' + n }
names[n] = true;
var fPath = path+n+'/';
blobs.push(Utilities.newBlob([]).setName(fPath)); //comment/uncomment this line to skip/include empty folders
blobs = blobs.concat(getBlobs(folder, fPath));
}
return blobs;
}
function sendArchiveToDropbox(blob) {
var args = {
'path': '/' + blob.getName(),
'mode': {
".tag":"overwrite",
},
"autorename": true,
"mute":true,
};
var options = {
'method' : 'post',
'payload' : blob.getBytes(),
'contentType': 'application/octet-stream',
'headers': {
'Authorization': 'Bearer ' + dropboxAccessToken,
'Content-Type': blob.getContentType(),
'Dropbox-API-Arg': JSON.stringify(args),
},
};
UrlFetchApp.fetch('https://content.dropboxapi.com/2/files/upload', options);
}
function main() {
var archive = buildArchive();
sendArchiveToDropbox(archive);
}
@samber

samber commented Nov 11, 2017

Copy link
Copy Markdown
Author

If you reach 6min timeout, you probably need to backup subdirectories separately :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment