-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackup.js
231 lines (196 loc) · 7 KB
/
backup.js
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env node
'use strict';
const fs = require('graceful-fs');
const path = require('path');
const pkg = require(path.join(__dirname, 'package.json'));
const { program } = require('commander');
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');
const prettyJs = require('pretty-js');
const prompt = require('prompt');
const opener = require('opener');
const colors = require('colors');
const encodePath = require('./encode-path');
const discovery = require('./discovery');
const rateLimited = require('./rate-limited');
const addQueryParamsToURL = require('./add-query-params-to-url');
program
.version(pkg.version)
.option('-o, --backup-dir <path>', 'backup directory path')
.option('-u, --user-address <user address>', 'user address (user@host)')
.option('-t, --token <token>', 'valid bearer token')
.option('-c, --category <category>', 'category (base directory) to back up')
.option('-p, --include-public', 'when backing up a single category, include the public folder of that category')
.option('-r, --rate-limit <time>', 'time interval for network requests in ms (default is 20)');
program.parse(process.argv);
const options = program.opts();
const ORIGIN = 'https://rs-backup.5apps.com';
const backupDir = options.backupDir;
const category = options.category || '';
const includePublic = options.includePublic || false;
const authScope = category.length > 0 ? category+':rw' : '*:rw';
const rateLimit = options.rateLimit || 20;
const retryCount = 3;
const retryDelay = 1000;
const retryMatch = /(ETIMEDOUT|socket hang up|Client network socket disconnected before secure TLS connection was established|ENETDOWN|ECONNRESET|ENOTFOUND)/;
const _retryMap = {};
let userAddress = options.userAddress;
let token = options.token;
let storageBaseUrl = null;
if (!(backupDir)) {
// TODO ask or use default
console.log('Please provide a backup directory path via the --backup-dir option');
process.exit(1);
}
const isDirectory = function(str) {
return str[str.length-1] === '/';
};
const initialDir = isDirectory(category) || category === '' ? category : category+'/';
let publicDir = null;
if (category !== '') {
publicDir = `public/${initialDir}`;
}
const handleError = function(error) {
console.log(colors.red(error.message));
process.exit(1);
};
const fetchDocument = function(path) {
_retryMap[path] = _retryMap[path] || 0;
const options = {
headers: { "Authorization": `Bearer ${token}`, "User-Agent": `RSBackup/${program._version}`, "Origin": ORIGIN }
};
return fetch(storageBaseUrl+encodePath(path), options)
.then(res => {
if ([200, 304].includes(res.status)) {
res.body.pipe(fs.createWriteStream(backupDir+'/'+path));
res.body.on('end', () => {
console.log('Wrote '+path);
return true;
});
} else {
console.log(`Error response for ${path}: ${res.status}`.red);
return false;
}
})
.catch(function (error) {
if (error.message.match(retryMatch) && (_retryMap[path] < retryCount)) {
console.log(colors.cyan(error.message));
console.log(colors.cyan(`Retrying ${ path }`));
_retryMap[path] += 1;
return new Promise(function (res) {
setTimeout(function () {
return res(fetchDocument(path));
}, retryDelay);
});
}
return handleError(error);
});
};
const fetchDocumentRateLimited = rateLimited(fetchDocument, rateLimit);
const fetchDirectoryContents = function(dir) {
_retryMap[dir] = _retryMap[dir] || 0;
mkdirp.sync(backupDir+'/'+dir);
const options = {
headers: { "Authorization": `Bearer ${token}`, "User-Agent": `RSBackup/${program._version}`, "Origin": ORIGIN }
};
return fetch(storageBaseUrl+encodePath(dir), options)
.then(res => {
if ([200, 304].includes(res.status)) {
return res.json()
} else if ([401, 403].includes(res.status)) {
throw(Error('App authorization token invalid or missing'))
} else {
handleError(res.error)
}
})
.then(listing => {
// TODO compare with potentially existing listing and only fetch changed dirs and docs
fs.writeFileSync(backupDir+'/'+dir+'000_folder-description.json',
prettyJs(JSON.stringify(listing), {quoteProperties: null}));
Object.keys(listing.items).forEach(key => {
if (isDirectory(key)) {
fetchDirectoryContentsRateLimited(dir+key);
} else {
fetchDocumentRateLimited(dir+key);
}
});
})
.catch(function (error) {
if (error.message.match(retryMatch) && (_retryMap[dir] < retryCount)) {
console.log(colors.cyan(error.message));
console.log(colors.cyan(`Retrying ${ dir }`));
_retryMap[dir] += 1;
return new Promise(function (res) {
setTimeout(function () {
return res(fetchDocument(dir));
}, retryDelay);
});
}
return handleError(error);
});
};
const fetchDirectoryContentsRateLimited = rateLimited(fetchDirectoryContents, rateLimit);
const lookupStorageInfo = function() {
return discovery.lookup(userAddress).then(storageInfo => {
let href = storageInfo.href;
if (href[href.length-1] !== '/') { href = href+'/'; }
storageBaseUrl = href;
return storageInfo;
}).catch(error => {
console.log('Lookup of '+userAddress+' failed:');
console.log(error);
process.exit(1);
});
};
const executeBackup = function() {
console.log('Starting backup...\n');
rimraf.sync(backupDir); // TODO incremental update
mkdirp.sync(backupDir);
fetchDirectoryContents(initialDir);
if (includePublic && publicDir) {
fetchDirectoryContents(publicDir);
}
};
const schemas = {
userAddress: {
name: 'userAddress',
description: 'User address (user@host):',
type: 'string',
pattern: /^.+@.+$/,
message: 'Please provide a valid user address. Example: tony@5apps.com',
required: true
},
token: {
name: 'token',
description: 'Authorization token:',
type: 'string',
required: true
}
};
// Start the show
if (token && userAddress) {
lookupStorageInfo().then(executeBackup);
} else {
console.log('No user address and/or auth token set via options. A browser window will open to connect your account.'.cyan);
prompt.message = '';
prompt.delimiter = '';
prompt.override = program;
prompt.start();
prompt.get(schemas.userAddress, (err, result) => {
userAddress = result.userAddress;
lookupStorageInfo().then(storageInfo => {
const authURL = addQueryParamsToURL(storageInfo.authURL, {
client_id: 'rs-backup.5apps.com',
redirect_uri: ORIGIN + '/',
response_type: 'token',
scope: authScope
});
opener(authURL);
prompt.get(schemas.token, (err, result) => {
token = result.token;
executeBackup();
});
});
});
}