adds backup utilities

This commit is contained in:
Lucy Cifferello
2023-01-16 14:08:07 -07:00
parent efbdffcf4f
commit 7727df7a0b
3 changed files with 117 additions and 0 deletions

98
backups.ts Normal file
View File

@@ -0,0 +1,98 @@
import { ok } from "./util.ts";
import * as T from "./types.ts";
/**
* This utility simplifies the volume backup process.
* ```ts
* export const { createBackup, restoreBackup } = Backups.volumes("main").build();
* ```
*/
export class Backups {
static BACKUP = "BACKUP" as const;
public backupSet = [] as {
srcPath: string;
srcVolume: string;
dstPath: string;
dstVolume: string;
}[];
constructor() {
}
static volumes(...volumeNames: string[]) {
return new Backups().addSets(...volumeNames.map((srcVolume) => ({
srcVolume,
srcPath: "./",
dstPath: `./${srcVolume}/`,
dstVolume: Backups.BACKUP,
})));
}
static addSets(
...options: {
srcPath: string;
srcVolume: string;
dstPath: string;
dstVolume: string;
}[]
) {
return new Backups().addSets(...options);
}
addSets(
...options: {
srcPath: string;
srcVolume: string;
dstPath: string;
dstVolume: string;
}[]
) {
options.forEach((x) => this.backupSet.push(x));
return this;
}
build() {
const createBackup: T.ExpectedExports.createBackup = async (effects) => {
for (const item of this.backupSet) {
if (notEmptyPath(item.dstPath)) {
await effects.createDir({
volumeId: item.dstVolume,
path: item.dstPath,
});
}
await effects.runRsync({
...item,
options: {
delete: true,
force: true,
ignoreExisting: false,
exclude: [],
},
}).wait();
}
return ok;
};
const restoreBackup: T.ExpectedExports.restoreBackup = async (effects) => {
for (const item of this.backupSet) {
if (notEmptyPath(item.srcPath)) {
await effects.createDir({
volumeId: item.srcVolume,
path: item.srcPath,
});
}
await effects.runRsync({
options: {
delete: true,
force: true,
ignoreExisting: false,
exclude: [],
},
srcVolume: item.dstVolume,
dstVolume: item.srcVolume,
srcPath: item.dstPath,
dstPath: item.srcPath,
}).wait();
}
return ok;
};
return { createBackup, restoreBackup };
}
}
function notEmptyPath(file: string) {
return ["", ".", "./"].indexOf(file) === -1;
}

1
mod.ts
View File

@@ -4,3 +4,4 @@ export * as compat from "./compat/mod.ts";
export * as migrations from "./migrations.ts";
export * as healthUtil from "./healthUtil.ts";
export * as util from "./util.ts";
export { Backups } from "./backups.ts";

View File

@@ -6,6 +6,10 @@ export namespace ExpectedExports {
export type getConfig = (effects: Effects) => Promise<ResultType<ConfigRes>>;
/** These are how we make sure the our dependency configurations are valid and if not how to fix them. */
export type dependencies = Dependencies;
/** For backing up service data though the embassyOS UI */
export type createBackup = (effects: Effects) => Promise<ResultType<unknown>>;
/** For restoring service data that was previously backed up using the embassyOS UI create backup flow. Backup restores are also triggered via the embassyOS UI, or doing a system restore flow during setup. */
export type restoreBackup = (effects: Effects) => Promise<ResultType<unknown>>;
/** Properties are used to get values from the docker, like a username + password, what ports we are hosting from */
export type properties = (effects: Effects) => Promise<ResultType<Properties>>;
@@ -85,6 +89,20 @@ export type Effects = {
/// Returns the body as a json
json(): Promise<unknown>;
}>;
runRsync(options: {
srcVolume: string,
dstVolume: string,
srcPath: string,
dstPath: string,
// rsync options: https://linux.die.net/man/1/rsync
options: {
delete: boolean,
force: boolean,
ignoreExisting: boolean,
exclude: string[]
}
}): {id: () => Promise<string>, wait: () => Promise<null>, progress: () => Promise<number>}
};
export type Metadata = {
fileType: string;