-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCode.gs
More file actions
185 lines (163 loc) · 5.49 KB
/
Code.gs
File metadata and controls
185 lines (163 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const FOLDER_ID = ['FOLDER_ID_1','FOLDER_ID_2'];
const BUCKET_NAME = 'bucket-name';
const FORCE_UPLOAD = false; // Change to true to reupload all the folder.
const USE_SA = false; // You can use a service account to manage access to bucket
/*
* Daily backup of Google Drive folders to Google Coud Storage
*
* Made by Stéphane Giron (https://twitter.com/st3phcloud)
* 0. Enter folders in FOLDER_ID and Cloud Storage bucket name in BUCKET_NAME
* 1. Launch createTrigger() a first time to validate scope
* 2. Run a second time the createTrigger() function
* 3. For manual launch, run function onleebackup()
*
*/
function onleebackup(){
for(let i = 0 ; i < FOLDER_ID.length ; i++){
uploadFilesFromFolderToCloudStorage(FOLDER_ID[i])
}
}
function createTrigger(){
ScriptApp.newTrigger("onleebackup")
.timeBased()
.atHour(5)
.everyDays(1)
.create();
}
function deleteTriggers(){
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
}
function uploadFilesFromFolderToCloudStorage(folderId) {
const query = '"'+folderId+'" in parents and trashed = false and ' +
'mimeType != "application/vnd.google-apps.folder"';
let files;
let pageToken = null;
const now = new Date().getTime();
do {
try {
files = Drive.Files.list({
q: query,
maxResults: 100,
pageToken: pageToken,
supportsAllDrives:true,
includeItemsFromAllDrives:true
});
if (!files.items || files.items.length === 0) {
console.log('No files found.');
return;
}
for (let i = 0; i < files.items.length; i++) {
let file = files.items[i];
if(file.mimeType == 'application/vnd.google-apps.shortcut'){
try{
file = getFileFromShortcut(file.id)
}catch(e){
console.log('Error you don\'t have access to file for shortcut id : '+file.id);
continue;
}
}
let lastUpdate = getKeyUpdate(file.id);
console.log('Last update : '+lastUpdate)
let modifiedDate = new Date(file.modifiedDate).getTime();
if(lastUpdate && !FORCE_UPLOAD){
if(modifiedDate > lastUpdate){
let upoloaded = uploadFileToCloudStorage(file,folderId)
console.log('%s (ID: %s) has been updated', file.title, file.id);
insertKeyProp('synced_date',modifiedDate,file.id)
}else{
console.log('File with ID '+file.id+' not uploaded, no change on the file.')
}
}else{
let uploaded = uploadFileToCloudStorage(file,folderId);
console.log('%s (ID: %s) has been uploaded', file.title, file.id);
console.log('Cloud Storage link %s',uploaded.selfLink)
insertKeyProp('synced_date',modifiedDate,file.id)
insertKeyProp('id_storage',uploaded.id,file.id)
}
}
pageToken = files.nextPageToken;
} catch (err) {
console.log('Failed with error %s', err.message);
}
} while (pageToken);
}
const mime = {
"application/vnd.google-apps.document":{extension:"docx",type:MimeType.MICROSOFT_WORD},
"application/vnd.google-apps.presentation":{extension:"pptx",type:MimeType.MICROSOFT_POWERPOINT},
"application/vnd.google-apps.spreadsheet":{extension:"xlsx",type:MimeType.MICROSOFT_EXCEL},
"application/vnd.google-apps.drawing":{extension:"png",type:MimeType.PNG}
}
function uploadFileToCloudStorage(file,folderId){
let blob ; let fileName ;
if(mime[file.mimeType]){
blob = UrlFetchApp.fetch(file.exportLinks[mime[file.mimeType].type],{
method: 'GET',
headers: {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken(),
}
}).getBlob();
fileName = file.title+'.'+mime[file.mimeType].extension ;
// DriveApp.getFileById(file.id).getAs(mime[file.mimeType])
}else{
blob = DriveApp.getFileById(file.id).getBlob();
fileName = file.title ;
}
const bytes = blob.getBytes();
const url = 'https://www.googleapis.com/upload/storage/v1/b/BUCKET/o?uploadType=media&name=FILE'
.replace('BUCKET', BUCKET_NAME)
.replace('FILE', folderId + '/' + encodeURIComponent(fileName));
const options = {
method: 'POST',
contentLength: bytes.length,
contentType: blob.getContentType(),
payload: bytes,
headers: {
Authorization: 'Bearer ' + getToken(),
}
};
const req = UrlFetchApp.fetch(url, options);
const rep = JSON.parse(req.getContentText());
return rep;
}
function getToken(){
if(USE_SA){
var service = getService();
if (service.hasAccess()) {
return service.getAccessToken();
}
throw 'Service account don\'t have access to the bucket or is badly setup.';
}
return ScriptApp.getOAuthToken();
}
function getFileFromShortcut(id){
let rep = UrlFetchApp.fetch('https://www.googleapis.com/drive/v3/files/'+id+'?fields=shortcutDetails(targetId)',
{method: 'GET',
headers: {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken(),
}
});
return Drive.Files.get(JSON.parse(rep).shortcutDetails.targetId)
}
function insertKeyProp(keyid,keyval,fileId){
try{
Drive.Properties.insert({key:keyid,value:keyval},fileId)
}catch(e){
console.log('Error to set property for file id : '+fileId)
console.log('Error : '+e.message)
}
return true
}
function getKeyUpdate(fileId){
return getProp('synced_date',fileId)
}
function getProp(keyid,fileId){
try{
let prop = Drive.Properties.get(fileId,keyid)
return prop.value
}catch(e){
return false;
}
}