generated from edmundhung/remix-cloudflare-template
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathbackup.mjs
89 lines (73 loc) · 2.12 KB
/
backup.mjs
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
/* eslint-disable no-undef */
import fs from 'node:fs/promises';
import path from 'node:path';
import fetch from 'node-fetch';
async function listKeys(accountId, namespaceId, token) {
const response = await fetch(
`https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/keys`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
},
);
if (!response.ok) {
throw new Error(
`Fail retrieving all keys; Received response with status ${response.status} ${response.statusText}`,
);
}
const { result } = await response.json();
return result;
}
async function getKeyValue(accountId, namespaceId, key, token) {
const response = await fetch(
`https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${encodeURIComponent(
key,
)}`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
},
);
if (!response.ok) {
throw new Error(
`Fail retrieving value for key=${key}; Received response with status ${response.status} ${response.statusText}`,
);
}
return await response.json();
}
async function backup() {
const accountId = process.env.CF_ACCOUNT_ID;
const namespaceId = process.env.CF_NAMESPACE_ID;
const token = process.env.CF_API_TOKEN;
const list = await listKeys(accountId, namespaceId, token);
const result = [];
// We can't get all values in paralel due to rate-limiting from Cloudflare
for (const key of list) {
const value = await getKeyValue(accountId, namespaceId, key.name, token);
result.push({
key: key.name,
value,
metadata: key.metadata,
});
console.log(`(${result.length}/${list.length}) ${key.name} downloaded`);
}
console.log(`${result.length} keys loaded`);
const cwd = process.cwd();
const output = path.resolve(cwd, `./${namespaceId}.json`);
await fs.writeFile(output, JSON.stringify(result, null, 2));
}
backup().then(
() => {
console.log('Backup complete');
},
(e) => {
console.error('Unknown error caught during backup');
console.log(e);
},
);