From cf105cf01d472582afd78c8980be0999339deb67 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Wed, 12 Mar 2025 14:10:15 +0000 Subject: [PATCH] Merge pull request #24267 from overleaf/bg-fix-init-option update backup script to use index when finding uninitialised projects GitOrigin-RevId: 04ce0654b3d42de1c1a9bb542482c2dd53540628 --- .../history-v1/storage/scripts/backup.mjs | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/services/history-v1/storage/scripts/backup.mjs b/services/history-v1/storage/scripts/backup.mjs index dded48b1ed..d73ef9dade 100644 --- a/services/history-v1/storage/scripts/backup.mjs +++ b/services/history-v1/storage/scripts/backup.mjs @@ -30,7 +30,7 @@ import { projectBlobsBucket, } from '../lib/backupPersistor.mjs' import { backupGenerator } from '../lib/backupGenerator.mjs' -import { promises as fs } from 'node:fs' +import { promises as fs, createWriteStream } from 'node:fs' import os from 'node:os' import path from 'node:path' import projectKey from '../lib/project_key.js' @@ -327,6 +327,7 @@ const optionDefinitions = [ type: Boolean, description: 'Initialize backups for all projects.', }, + { name: 'output', alias: 'o', type: String, description: 'Output file' }, { name: 'start-date', type: String, @@ -686,13 +687,14 @@ export async function initializeProjects(options) { let totalProjects = 0 const query = { - 'overleaf.history.id': { $exists: true }, - 'overleaf.backup.lastBackedUpVersion': { $exists: false }, - 'overleaf.backup.pendingChangeAt': { $exists: false }, - _id: { + 'overleaf.backup.lastBackedUpVersion': { $in: [null] }, + } + + if (options['start-date'] && options['end-date']) { + query._id = { $gte: objectIdFromInput(convertToISODate(options['start-date'])), $lt: objectIdFromInput(convertToISODate(options['end-date'])), - }, + } } const cursor = client @@ -703,6 +705,18 @@ export async function initializeProjects(options) { readPreference: READ_PREFERENCE_SECONDARY, }) + if (options.output) { + console.log("Writing project IDs to file: '" + options.output + "'") + const output = createWriteStream(options.output) + for await (const project of cursor) { + output.write(project._id.toHexString() + '\n') + totalProjects++ + } + output.end() + console.log('Wrote ' + totalProjects + ' project IDs to file') + return + } + for await (const project of cursor) { if (gracefulShutdownInitiated) { console.warn('graceful shutdown: stopping project initialization')