mirror of
https://github.com/zkldi/Tachi.git
synced 2026-09-29 02:18:09 +03:00
Update anonymise DB scripts to be way more stable.
This commit is contained in:
@@ -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 <Database to clone from.>")
|
||||
.option("--nsTo <Database to clone and anonymise to.>");
|
||||
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user