Skip to content

Commit

Permalink
Update dump.js
Browse files Browse the repository at this point in the history
  • Loading branch information
emengweb authored Dec 12, 2024
1 parent 84c64c7 commit 8755711
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ async function dumpMongoAllDatabases() {
* @returns {Promise<void>}
* @throws {Error} 当数据库连接或备份过程出错时抛出异常
*/
async function dumpMongoDatabase() {
async function dumpMongoDatabase(dbName) {
const uri = `mongodb://${process.env.DATABASE_USER || process.env.MONGO_ROOT_USERNAME}:${process.env.DATABASE_PASSWORD || process.env.MONGO_ROOT_PASSWORD}@${process.env.DATABASE_HOST || process.env.MONGO_HOST}:${process.env.DATABASE_PORT || process.env.MONGO_PORT || '27017'}/`;
const dbName = process.env.DATABASE_NAME || process.env.MONGO_DATABASE; // 替换成你的数据库名称
// const dbName = process.env.DATABASE_NAME || process.env.MONGO_DATABASE; // 替换成你的数据库名称
try {
if(!dbName){
console.error('DATABASE_NAME or MONGO_DATEBASE is not set');
Expand All @@ -137,7 +137,13 @@ async function dumpMongoDatabase() {
const collectionName = collection.collectionName;

// 创建一个文件来存储 dump 数据
const dumpFile = `${dumpDir}/${collectionName}.json`;
const dumpFile = `${dumpDir}/${dbName}/${collectionName}.json`;

// 创建文件夹
const collectionDir = `${dumpDir}/${dbName}`;
if (!fs.existsSync(collectionDir)) {
fs.mkdirSync(collectionDir);
}

// 读取集合数据
const documents = await collection.find().toArray();
Expand Down Expand Up @@ -636,7 +642,13 @@ async function main() {
case "mongo":
case "mongodb":
if(process.env.DATABASE_NAME){
await dumpMongoDatabase()
// 备份数据库可能为多个,使用,进行分割
const databaseNames = process.env.DATABASE_NAME.split(',');
for(const databaseName of databaseNames){
console.log(`Backuping database: ${databaseName}`);
await dumpMongoDatabase(databaseName)
console.log(`Backuping database: ${databaseName} completed!`);
}
}else{
await dumpMongoAllDatabases()
}
Expand Down

0 comments on commit 8755711

Please sign in to comment.