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
| const cloud = require('wx-server-sdk'); const request = require('request'); cloud.init({ env: 'xxxxxx' });
async function getAccessToken(appid, secret) { return new Promise((resolve, reject) => { request.get( `https: (err, res, body) => { if (err) { reject(err) } resolve(JSON.parse(body)) } ) }) }
async function createExportJob(access_token, env, collection) { const date = new Date().toISOString();
return new Promise((resolve, reject) => { request.post( `https: { body: JSON.stringify({ env, file_path: `${date}.json`, file_type: '1', query: `db.collection("${collection}").get()` }) }, (err, res, body) => { if (err) { reject(err) }
resolve(JSON.parse(body)) } ) }) }
async function waitJobFinished(access_token, env, job_id) { return new Promise((resolve, reject) => { const timer = setInterval(() => { request.post( `https: { body: JSON.stringify({ env, job_id }) }, (err, res, body) => { if (err) { reject(err) }
const { status, file_url } = JSON.parse(body)
console.log("查询")
if (status === 'success') { clearInterval(timer) resolve(file_url) } } ) }, 500) }) }
async function getJsonContent(file_url) { return new Promise((resolve, reject) => { request.get( `${file_url}`, (err, res, body) => { if (err) { reject(err) } resolve(body) } ) }) }
exports.main = async (event, context) => { const appid = "xxxxxxx"; const secret = "xxxxxxx"; const envId = "xxxxxxx" const backupFromTables = ["table1", "table2", "table3"]; const folderName = "databaseBackup" const dateString = (new Date()).toISOString()
var count = 0 var access_token = ""
try { const result = await getAccessToken(appid, secret) access_token = result.access_token
if (typeof access_token == null || access_token== "" || access_token == "undefined") { throw new Error(`获取 access_token 失败: ${result.errmsg}` || '获取 access_token 为空') } } catch (e) { throw new Error(`获取 access_token: ${e.message}`); }
for(index in backupFromTables) { const backupFromTable = backupFromTables[index]
createExportJob(access_token, envId, backupFromTable) .then(res => { console.log(res) if(res.errcode !== 0) { throw new Error("Can't get job_id") } console.log(res.job_id) return waitJobFinished(access_token, envId, res.job_id) }) .then(file_url => { return getJsonContent(file_url) }) .then(fileContent => { return cloud.uploadFile({ cloudPath: './' + folderName + '/' + dateString.slice(0,10) + '/' + dateString + '_' + backupFromTable + '.json', fileContent: fileContent }) }) .then(res => { count++ console.log("--->---- " + count + "/" + backupFromTables.length+ ":" + backupFromTable + "已完成备份 ---<----") }) .catch( error => { throw new Error(`导出数据库异常: ${error.message}`); }) }
}
|