feat: Backup w/ readDir

This commit is contained in:
J H
2023-03-08 10:55:48 -07:00
committed by GitHub
parent 180dd701a0
commit f914826959

View File

@@ -1,5 +1,5 @@
import { ok } from "./util.ts"; import { ok } from "./util";
import * as T from "./types.ts"; import * as T from "./types";
export const DEFAULT_OPTIONS: T.BackupOptions = { export const DEFAULT_OPTIONS: T.BackupOptions = {
delete: true, delete: true,
@@ -33,28 +33,24 @@ type BackupSet = {
* srcVolume: 'main', srcPath:'smallData/', dstPath: 'main/smallData/', dstVolume: : Backups.BACKUP * srcVolume: 'main', srcPath:'smallData/', dstPath: 'main/smallData/', dstVolume: : Backups.BACKUP
* }, { * }, {
* srcVolume: 'main', srcPath:'bigData/', dstPath: 'main/bigData/', dstVolume: : Backups.BACKUP, options: {exclude:['bigData/excludeThis']}} * srcVolume: 'main', srcPath:'bigData/', dstPath: 'main/bigData/', dstVolume: : Backups.BACKUP, options: {exclude:['bigData/excludeThis']}}
* ).build() * ).build()q
* ``` * ```
*/ */
export class Backups { export class Backups {
static BACKUP = "BACKUP" as const; static BACKUP = "BACKUP" as const;
constructor( constructor(private options = DEFAULT_OPTIONS, private backupSet = [] as BackupSet[]) {}
private options = DEFAULT_OPTIONS,
private backupSet = [] as BackupSet[],
) {
}
static volumes(...volumeNames: string[]) { static volumes(...volumeNames: string[]) {
return new Backups().addSets(...volumeNames.map((srcVolume) => ({ return new Backups().addSets(
srcVolume, ...volumeNames.map((srcVolume) => ({
srcPath: "./", srcVolume,
dstPath: `./${srcVolume}/`, srcPath: "./",
dstVolume: Backups.BACKUP, dstPath: `./${srcVolume}/`,
}))); dstVolume: Backups.BACKUP,
}))
);
} }
static addSets( static addSets(...options: BackupSet[]) {
...options: BackupSet[]
) {
return new Backups().addSets(...options); return new Backups().addSets(...options);
} }
static with_options(options?: Partial<T.BackupOptions>) { static with_options(options?: Partial<T.BackupOptions>) {
@@ -68,23 +64,51 @@ export class Backups {
return this; return this;
} }
volumes(...volumeNames: string[]) { volumes(...volumeNames: string[]) {
return this.addSets(...volumeNames.map((srcVolume) => ({ return this.addSets(
srcVolume, ...volumeNames.map((srcVolume) => ({
srcPath: "./", srcVolume,
dstPath: `./${srcVolume}/`, srcPath: "./",
dstVolume: Backups.BACKUP, dstPath: `./${srcVolume}/`,
}))); dstVolume: Backups.BACKUP,
} }))
addSets(
...options: BackupSet[]
) {
options.forEach((x) =>
this.backupSet.push({ ...x, options: { ...this.options, ...x.options } })
); );
}
addSets(...options: BackupSet[]) {
options.forEach((x) => this.backupSet.push({ ...x, options: { ...this.options, ...x.options } }));
return this; return this;
} }
build() { build() {
const createBackup: T.ExpectedExports.createBackup = async (effects) => { const createBackup: T.ExpectedExports.createBackup = async ({ effects }) => {
const previousItems = (
await effects
.readDir({
volumeId: Backups.BACKUP,
path: ".",
})
.catch(() => [])
).map((x) => `${x}`);
const backupPaths = this.backupSet
.filter((x) => x.dstVolume === Backups.BACKUP)
.map((x) => x.dstPath)
.map((x) => x.replace(/\.\/([^]*)\//, "$1"));
const filteredItems = previousItems.filter((x) => backupPaths.indexOf(x) === -1);
for (const itemToRemove of filteredItems) {
effects.error(`Trying to remove ${itemToRemove}`);
await effects
.removeDir({
volumeId: Backups.BACKUP,
path: itemToRemove,
})
.catch(() =>
effects.removeFile({
volumeId: Backups.BACKUP,
path: itemToRemove,
})
)
.catch(() => {
effects.warn(`Failed to remove ${itemToRemove} from backup volume`);
});
}
for (const item of this.backupSet) { for (const item of this.backupSet) {
if (notEmptyPath(item.dstPath)) { if (notEmptyPath(item.dstPath)) {
await effects.createDir({ await effects.createDir({
@@ -92,17 +116,19 @@ export class Backups {
path: item.dstPath, path: item.dstPath,
}); });
} }
await effects.runRsync({ await effects
...item, .runRsync({
options: { ...item,
...this.options, options: {
...item.options, ...this.options,
}, ...item.options,
}).wait(); },
})
.wait();
} }
return ok; return ok;
}; };
const restoreBackup: T.ExpectedExports.restoreBackup = async (effects) => { const restoreBackup: T.ExpectedExports.restoreBackup = async ({ effects }) => {
for (const item of this.backupSet) { for (const item of this.backupSet) {
if (notEmptyPath(item.srcPath)) { if (notEmptyPath(item.srcPath)) {
await effects.createDir({ await effects.createDir({
@@ -110,16 +136,18 @@ export class Backups {
path: item.srcPath, path: item.srcPath,
}); });
} }
await effects.runRsync({ await effects
options: { .runRsync({
...this.options, options: {
...item.options, ...this.options,
}, ...item.options,
srcVolume: item.dstVolume, },
dstVolume: item.srcVolume, srcVolume: item.dstVolume,
srcPath: item.dstPath, dstVolume: item.srcVolume,
dstPath: item.srcPath, srcPath: item.dstPath,
}).wait(); dstPath: item.srcPath,
})
.wait();
} }
return ok; return ok;
}; };