mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-30 20:24:47 +00:00
chore: including options
This commit is contained in:
93
backups.ts
93
backups.ts
@@ -1,21 +1,48 @@
|
||||
import { ok } from "./util.ts";
|
||||
import * as T from "./types.ts";
|
||||
|
||||
/**
|
||||
export const DEFAULT_OPTIONS: T.BackupOptions = {
|
||||
delete: true,
|
||||
force: true,
|
||||
ignoreExisting: false,
|
||||
exclude: [],
|
||||
};
|
||||
type BackupSet = {
|
||||
srcPath: string;
|
||||
srcVolume: string;
|
||||
dstPath: string;
|
||||
dstVolume: string;
|
||||
options?: Partial<T.BackupOptions>;
|
||||
};
|
||||
/**
|
||||
* This utility simplifies the volume backup process.
|
||||
* ```ts
|
||||
* export const { createBackup, restoreBackup } = Backups.volumes("main").build();
|
||||
* ```
|
||||
*/
|
||||
*
|
||||
* Changing the options of the rsync, (ie exludes) use either
|
||||
* ```ts
|
||||
* Backups.volumes("main").set_options({exclude: ['bigdata/']}).volumes('excludedVolume').build()
|
||||
* // or
|
||||
* Backups.with_options({exclude: ['bigdata/']}).volumes('excludedVolume').build()
|
||||
* ```
|
||||
*
|
||||
* Using the more fine control, using the addSets for more control
|
||||
* ```ts
|
||||
* Backups.addSets({
|
||||
* srcVolume: 'main', srcPath:'smallData/', dstPath: 'main/smallData/', dstVolume: : Backups.BACKUP
|
||||
* }, {
|
||||
* srcVolume: 'main', srcPath:'bigData/', dstPath: 'main/bigData/', dstVolume: : Backups.BACKUP, options: {exclude:['bigData/excludeThis']}}
|
||||
* ).build()
|
||||
* ```
|
||||
*/
|
||||
export class Backups {
|
||||
static BACKUP = "BACKUP" as const;
|
||||
public backupSet = [] as {
|
||||
srcPath: string;
|
||||
srcVolume: string;
|
||||
dstPath: string;
|
||||
dstVolume: string;
|
||||
}[];
|
||||
constructor() {
|
||||
|
||||
constructor(
|
||||
private options = DEFAULT_OPTIONS,
|
||||
private backupSet = [] as BackupSet[],
|
||||
) {
|
||||
}
|
||||
static volumes(...volumeNames: string[]) {
|
||||
return new Backups().addSets(...volumeNames.map((srcVolume) => ({
|
||||
@@ -26,24 +53,34 @@ export class Backups {
|
||||
})));
|
||||
}
|
||||
static addSets(
|
||||
...options: {
|
||||
srcPath: string;
|
||||
srcVolume: string;
|
||||
dstPath: string;
|
||||
dstVolume: string;
|
||||
}[]
|
||||
...options: BackupSet[]
|
||||
) {
|
||||
return new Backups().addSets(...options);
|
||||
}
|
||||
static with_options(options?: Partial<T.BackupOptions>) {
|
||||
return new Backups({ ...DEFAULT_OPTIONS, ...options });
|
||||
}
|
||||
set_options(options?: Partial<T.BackupOptions>) {
|
||||
this.options = {
|
||||
...this.options,
|
||||
...options,
|
||||
};
|
||||
return this;
|
||||
}
|
||||
volumes(...volumeNames: string[]) {
|
||||
return this.addSets(...volumeNames.map((srcVolume) => ({
|
||||
srcVolume,
|
||||
srcPath: "./",
|
||||
dstPath: `./${srcVolume}/`,
|
||||
dstVolume: Backups.BACKUP,
|
||||
})));
|
||||
}
|
||||
addSets(
|
||||
...options: {
|
||||
srcPath: string;
|
||||
srcVolume: string;
|
||||
dstPath: string;
|
||||
dstVolume: string;
|
||||
}[]
|
||||
...options: BackupSet[]
|
||||
) {
|
||||
options.forEach((x) => this.backupSet.push(x));
|
||||
options.forEach((x) =>
|
||||
this.backupSet.push({ ...x, options: { ...this.options, ...x.options } })
|
||||
);
|
||||
return this;
|
||||
}
|
||||
build() {
|
||||
@@ -58,10 +95,8 @@ export class Backups {
|
||||
await effects.runRsync({
|
||||
...item,
|
||||
options: {
|
||||
delete: true,
|
||||
force: true,
|
||||
ignoreExisting: false,
|
||||
exclude: [],
|
||||
...this.options,
|
||||
...item.options,
|
||||
},
|
||||
}).wait();
|
||||
}
|
||||
@@ -77,10 +112,8 @@ export class Backups {
|
||||
}
|
||||
await effects.runRsync({
|
||||
options: {
|
||||
delete: true,
|
||||
force: true,
|
||||
ignoreExisting: false,
|
||||
exclude: [],
|
||||
...this.options,
|
||||
...item.options,
|
||||
},
|
||||
srcVolume: item.dstVolume,
|
||||
dstVolume: item.srcVolume,
|
||||
|
||||
Reference in New Issue
Block a user