Page MenuHomePhabricator

D4283.diff
No OneTemporary

D4283.diff

diff --git a/keyserver/src/cron/backups.js b/keyserver/src/cron/backups.js
--- a/keyserver/src/cron/backups.js
+++ b/keyserver/src/cron/backups.js
@@ -19,6 +19,7 @@
type BackupConfig = {
+enabled: boolean,
+directory: string,
+ +maxDirSizeMiB?: ?number,
};
function getBackupConfig(): Promise<?BackupConfig> {
@@ -74,6 +75,8 @@
console.warn(`saveBackup threw for ${filename}`, e);
await unlink(filePath);
}
+
+ await deleteOldBackupsIfSpaceExceeded();
}
function mysqldump(
@@ -247,4 +250,27 @@
return filteredBackups;
}
+async function deleteOldBackupsIfSpaceExceeded() {
+ const backupConfig = await getBackupConfig();
+ invariant(backupConfig, 'backupConfig should be non-null');
+ const { maxDirSizeMiB } = backupConfig;
+ if (!maxDirSizeMiB) {
+ return;
+ }
+
+ const sortedBackupInfos = await getSortedBackupInfos();
+ const mostRecentBackup = sortedBackupInfos.pop();
+ let bytesLeft = maxDirSizeMiB * 1024 * 1024 - mostRecentBackup.bytes;
+
+ const deleteBackupPromises = [];
+ for (let i = sortedBackupInfos.length - 1; i >= 0; i--) {
+ const backupInfo = sortedBackupInfos[i];
+ bytesLeft -= backupInfo.bytes;
+ if (bytesLeft <= 0) {
+ deleteBackupPromises.push(deleteBackup(backupInfo.filename));
+ }
+ }
+ await Promise.all(deleteBackupPromises);
+}
+
export { backupDB };

File Metadata

Mime Type
text/plain
Expires
Thu, Nov 28, 6:33 PM (21 h, 38 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2594595
Default Alt Text
D4283.diff (1 KB)

Event Timeline