From ff40d09116eb3f10925318bb51ff444201ff2bc6 Mon Sep 17 00:00:00 2001 From: zkldi Date: Fri, 1 Oct 2021 18:22:43 +0100 Subject: [PATCH] Update anonymise DB scripts to be way more stable. --- ...nymise-db.ts => clone-and-anonymise-db.ts} | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) rename server/scripts/{anonymise-db.ts => clone-and-anonymise-db.ts} (57%) diff --git a/server/scripts/anonymise-db.ts b/server/scripts/clone-and-anonymise-db.ts similarity index 57% rename from server/scripts/anonymise-db.ts rename to server/scripts/clone-and-anonymise-db.ts index f7250001a..f4aa265b6 100644 --- a/server/scripts/anonymise-db.ts +++ b/server/scripts/clone-and-anonymise-db.ts @@ -1,8 +1,20 @@ -// -// - import CreateLogCtx from "lib/logger/logger"; import monk from "monk"; +import { execSync } from "child_process"; + +import { Command } from "commander"; + +const program = new Command(); +program + .option("--nsFrom ") + .option("--nsTo "); + +program.parse(process.argv); +const options = program.opts(); + +if (!options.nsTo || !options.nsFrom) { + throw new Error(`Expected --nsFrom and --nsTo options.`); +} const logger = CreateLogCtx(__filename); @@ -10,15 +22,14 @@ const logger = CreateLogCtx(__filename); * Strips all name and private information from the database. * For use for things like future tachi-db-dumps? * - * @param connectionString The database to anonymise. DO NOT PASS THE PRODUCTION DATABASE TO THIS! - * Clone the database with mongodump->mongorestore (This is the documented way to clone a db.) + * @param nsTo The database to anonymise. DO NOT PASS THE PRODUCTION DATABASE TO THIS! */ -async function CloneAndAnonymiseDB(connectionString: string) { - logger.info(`Connecting to ${connectionString}.`); +async function AnonymiseDB(nsTo: string) { + logger.info(`Connecting to ${nsTo}.`); - const clonedDB = monk(connectionString); + const clonedDB = monk(`127.0.0.1/${nsTo}`); - logger.info(`Connected to ${connectionString}.`); + logger.info(`Connected to ${nsTo}.`); const r1 = await clonedDB.get("user-private-information").update( {}, @@ -75,9 +86,21 @@ async function CloneAndAnonymiseDB(connectionString: string) { logger.info(`Removed all ${collection} documents.`, { r3 }); } + + logger.info(`Done! Closing.`); + + process.exit(0); +} + +function CloneDB(nsFrom: string, nsTo: string) { + logger.info(`Cloning Database. This will take a while.`); + execSync(`mongodump --gzip --archive=temp-dump --db=${nsFrom}`); + execSync(`mongorestore --archive=temp-dump --gzip --nsFrom='${nsFrom}.*' --nsTo='${nsTo}.*'`); + execSync(`rm temp-dump`); + logger.info(`Cloned!`); } if (require.main === module) { - // Maybe turn this into a commander program, it doesn't really matter. - CloneAndAnonymiseDB(process.argv[2]); + CloneDB(options.nsFrom, options.nsTo); + AnonymiseDB(options.nsTo); }