Skip to content

Commit

Permalink
feat: add forget config support
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed Jan 7, 2021
1 parent e79766b commit 1469577
Show file tree
Hide file tree
Showing 5 changed files with 317 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Config profiles
- Repositories
- Backups
- Forgets
- File config JSON/YAML format
- Auto init repositories
- Path globs
Expand All @@ -29,7 +30,8 @@ Options:
Commands:
parse Parse config
backup [options] Create backups
backup [options] Create snapshots
forget [options] Forget snapshots
help [command] display help for command
```

Expand Down
100 changes: 100 additions & 0 deletions arestic.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,67 @@
}
}
},
"forget-options-cli": {
"type": "object",
"properties": {
"keep-last": {
"type": "integer",
"description": "keep the last n snapshots"
},
"keep-hourly": {
"type": "integer",
"description": "keep the last n hourly snapshots"
},
"keep-daily": {
"type": "integer",
"description": "keep the last n daily snapshots"
},
"keep-weekly": {
"type": "integer",
"description": "keep the last n weekly snapshots"
},
"keep-monthly": {
"type": "integer",
"description": "keep the last n monthly snapshots"
},
"keep-yearly": {
"type": "integer",
"description": "keep the last n yearly snapshots"
},
"keep-within": {
"type": "string",
"description": "keep snapshots that are newer than duration (eg. 1y5m7d2h) relative to the latest snapshot"
},
"keep-tag": {
"$ref": "#/definitions/stringList",
"description": "keep snapshots with this taglist (can be specified multiple times) (default [])"
},
"host": {
"type": "string",
"description": "only consider snapshots with the given host"
},
"tag": {
"$ref": "#/definitions/stringList",
"description": "only consider snapshots which include this taglist in the format `tag[,tag,...]` (can be specified multiple times) (default [])"
},
"compact": {
"type": "boolean",
"description": "use compact format"
},
"group-by": {
"type": "string",
"description": "string for grouping snapshots by host,paths,tags (default \"host,paths\")"
},
"dry-run": {
"type": "boolean",
"description": "do not delete anything, just print what would be done"
},
"prune": {
"type": "boolean",
"description": "automatically run the 'prune' command if snapshots have been removed"
}
}
},
"repository": {
"type": "object",
"additionalProperties": false,
Expand Down Expand Up @@ -464,6 +525,34 @@
"$ref": "#/definitions/backup-options-cli"
}
}
},
"forget": {
"title": "Forget",
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"type": "string"
},
"env": {
"$ref": "#/definitions/env"
},
"repositories": {
"$ref": "#/definitions/stringList"
},
"password": {
"type": "string"
},
"passwordPath": {
"type": "string"
},
"globalOptions": {
"$ref": "#/definitions/global-options-cli"
},
"options": {
"$ref": "#/definitions/forget-options-cli"
}
}
}
},
"type": "object",
Expand All @@ -489,6 +578,17 @@
"$ref": "#/definitions/backup"
}
}
},
"forgets": {
"type": ["object", "array"],
"items": {
"$ref": "#/definitions/forget"
},
"patternProperties": {
".+": {
"$ref": "#/definitions/forget"
}
}
}
}
}
35 changes: 35 additions & 0 deletions src/ARestic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export type ConfigType<TNormalized extends boolean = true> = {
backups: TNormalized extends true
? Schema.Backup[]
: Record<string, Schema.Backup> | Schema.Backup[]
forgets: TNormalized extends true
? Schema.Forget[]
: Record<string, Schema.Forget> | Schema.Forget[]
}

export class ARestic {
Expand Down Expand Up @@ -134,9 +137,22 @@ export class ARestic {
await fs.readFile(backup.passwordPath)
).toString()

const forgets: Schema.Forget[] = Array.isArray(config.forgets)
? config.forgets.slice(0).map((forget) => Object.assign({}, forget))
: isPlainObject(config.forgets)
? recordToArray(config.forgets, "name")
: []

for (const forget of forgets)
if (forget.passwordPath)
forget.password = (
await fs.readFile(forget.passwordPath)
).toString()

return {
repositories: repositories,
backups: backups,
forgets: forgets,
} as ConfigType
}

Expand Down Expand Up @@ -250,4 +266,23 @@ export class ARestic {
onExecData
)
}

async forget(
data: Schema.Forget,
repository: Schema.Repository,
onExecData: (data: Buffer) => void
) {
const args = ["forget"]
.concat(parseArgs(data.globalOptions || {}))
.concat(parseArgs(data.options || {}))

return await exec(
"restic",
args,
{
env: this.buildEnv(repository, data),
},
onExecData
)
}
}
77 changes: 77 additions & 0 deletions src/AResticSchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export interface ARestic {
[k: string]: Backup;
}
| Backup[];
forgets?:
| {
[k: string]: Forget;
}
| Forget[];
[k: string]: unknown;
}
/**
Expand Down Expand Up @@ -392,3 +397,75 @@ export interface BackupOptionsCli {
"with-atime"?: boolean;
[k: string]: unknown;
}
/**
* This interface was referenced by `undefined`'s JSON-Schema definition
* via the `patternProperty` ".+".
*/
export interface Forget {
name?: string;
env?: Env;
repositories?: StringList;
password?: string;
passwordPath?: string;
globalOptions?: GlobalOptionsCli;
options?: ForgetOptionsCli;
}
export interface ForgetOptionsCli {
/**
* keep the last n snapshots
*/
"keep-last"?: number;
/**
* keep the last n hourly snapshots
*/
"keep-hourly"?: number;
/**
* keep the last n daily snapshots
*/
"keep-daily"?: number;
/**
* keep the last n weekly snapshots
*/
"keep-weekly"?: number;
/**
* keep the last n monthly snapshots
*/
"keep-monthly"?: number;
/**
* keep the last n yearly snapshots
*/
"keep-yearly"?: number;
/**
* keep snapshots that are newer than duration (eg. 1y5m7d2h) relative to the latest snapshot
*/
"keep-within"?: string;
/**
* keep snapshots with this taglist (can be specified multiple times) (default [])
*/
"keep-tag"?: string[];
/**
* only consider snapshots with the given host
*/
host?: string;
/**
* only consider snapshots which include this taglist in the format `tag[,tag,...]` (can be specified multiple times) (default [])
*/
tag?: string[];
/**
* use compact format
*/
compact?: boolean;
/**
* string for grouping snapshots by host,paths,tags (default "host,paths")
*/
"group-by"?: string;
/**
* do not delete anything, just print what would be done
*/
"dry-run"?: boolean;
/**
* automatically run the 'prune' command if snapshots have been removed
*/
prune?: boolean;
[k: string]: unknown;
}
Loading

0 comments on commit 1469577

Please sign in to comment.