Skip to content

Commit 0ce3aea

Browse files
authored
Merge pull request #1460 from Altinity/feature_watch_crontab
add --schedule to watch command and related server.go REST API
2 parents c8d0d88 + 7e2a85a commit 0ce3aea

15 files changed

Lines changed: 1046 additions & 239 deletions

File tree

Manual.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,17 +360,22 @@ NAME:
360360
clickhouse-backup watch - Run infinite loop which create full + incremental backup sequence to allow efficient backup sequences
361361
362362
USAGE:
363-
clickhouse-backup watch [--watch-interval=1h] [--full-interval=24h] [--watch-backup-name-template=shard{shard}-{type}-{time:20060102150405}] [-t, --tables=<db>.<table>] [--partitions=<partitions_names>] [--schema] [--rbac] [--configs] [--skip-check-parts-columns]
363+
clickhouse-backup watch [--watch-interval=1h] [--full-interval=24h] [--watch-backup-name-template=shard{shard}-{type}-{time:20060102150405}] [--schedule=name=<name>,full=<cron>,increment=<cron>] [-t, --tables=<db>.<table>] [--partitions=<partitions_names>] [--schema] [--rbac] [--configs] [--skip-check-parts-columns]
364364
365365
DESCRIPTION:
366-
Execute create_remote + delete local, create full backup every `--full-interval`, create and upload incremental backup every `--watch-interval` use previous backup as base with `--diff-from-remote` option, use `backups_to_keep_remote` config option for properly deletion remote backups, will delete old backups which not have references from other backups
366+
Execute create_remote + delete local, create full backup every `--full-interval`, create and upload incremental backup every `--watch-interval` use previous backup as base with `--diff-from-remote` option, use `backups_to_keep_remote` config option for properly deletion remote backups, will delete old backups which not have references from other backups. Use `--schedule` instead of intervals to run backups on cron expressions
367367
368368
OPTIONS:
369369
--config value, -c value Config 'FILE' name. (default: "/etc/clickhouse-backup/config.yml") [$CLICKHOUSE_BACKUP_CONFIG]
370370
--environment-override value, --env value override any environment variable via CLI parameter
371371
--watch-interval value Interval for run 'create_remote' + 'delete local' for incremental backup, look format https://pkg.go.dev/time#ParseDuration
372372
--full-interval value Interval for run 'create_remote'+'delete local' when stop create incremental backup sequence and create full backup, look format https://pkg.go.dev/time#ParseDuration
373373
--watch-backup-name-template value Template for new backup name, could contain names from system.macros, {type} - full or incremental and {time:LAYOUT}, look to https://go.dev/src/time/format.go for layout examples
374+
--schedule value Named cron driven backup chain in name=<name>,full=<cron>[,increment=<cron>][,full_type=create|rebase][,delete_previous_cycle=true|false] format, can be specified multiple times, mutually exclusive with --watch-interval and --full-interval
375+
cron expression contains standard 5 fields, optional leading seconds field and @every/@daily descriptors, see https://pkg.go.dev/github.com/robfig/cron/v3#hdr-CRON_Expression_Format
376+
name added as prefix to --watch-backup-name-template to isolate backup chains
377+
full_type=rebase creates scheduled full backup as increment + rebase command, server-side copy of previous chain instead of full re-upload
378+
delete_previous_cycle=true deletes all older backups of the chain after successful full backup
374379
--table value, --tables value, -t value Create and upload only objects which matched with table name patterns, separated by comma, allow ? and * as wildcard
375380
--partitions partition_id Partitions names, separated by comma
376381
If PARTITION BY clause returns numeric not hashed values for partition_id field in system.parts table, then use --partitions=partition_id1,partition_id2 format
@@ -411,6 +416,7 @@ OPTIONS:
411416
--watch-interval value Interval for run 'create_remote' + 'delete local' for incremental backup, look format https://pkg.go.dev/time#ParseDuration
412417
--full-interval value Interval for run 'create_remote'+'delete local' when stop create incremental backup sequence and create full backup, look format https://pkg.go.dev/time#ParseDuration
413418
--watch-backup-name-template value Template for new backup name, could contain names from system.macros, {type} - full or incremental and {time:LAYOUT}, look to https://go.dev/src/time/format.go for layout examples
419+
--schedule value Named cron driven backup chain for watch in name=<name>,full=<cron>[,increment=<cron>][,full_type=create|rebase][,delete_previous_cycle=true|false] format, can be specified multiple times, mutually exclusive with --watch-interval and --full-interval
414420
--watch-delete-source, --watch-delete-local explicitly delete local backup during upload in watch
415421
416422
```

ReadMe.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,20 @@ general:
168168
watch_interval: 1h # WATCH_INTERVAL, use only for `watch` command, backup will create every 1h
169169
full_interval: 24h # FULL_INTERVAL, use only for `watch` command, full backup will create every 24h
170170
watch_backup_name_template: "shard{shard}-{type}-{time:20060102150405}" # WATCH_BACKUP_NAME_TEMPLATE, used only for `watch` command, macros values will apply from `system.macros` for time:XXX, look format in https://go.dev/src/time/format.go
171-
171+
172+
# watch_schedules - WATCH_SCHEDULES, use only for `watch` command, named cron driven backup chains, mutually exclusive with watch_interval/full_interval
173+
# `name` added as prefix to watch_backup_name_template to isolate backup chains, `full` and `increment` are cron expressions (standard 5 fields, optional leading seconds field, @every/@daily descriptors)
174+
# `full_type: rebase` creates scheduled full backup as increment + `rebase` command (server-side copy of previous chain instead of full re-upload)
175+
# `delete_previous_cycle: true` deletes all older backups of the chain after successful full backup
176+
# in WATCH_SCHEDULES env variable use `name=daily,full=0 0 * * *,increment=0 * * * *;name=weekly,full=@weekly` format, `;` as separator between schedules
177+
# watch_schedules:
178+
# - name: daily
179+
# full: "0 0 * * *"
180+
# increment: "0 * * * *"
181+
# full_type: create
182+
# delete_previous_cycle: false
183+
watch_schedules: []
184+
172185
sharded_operation_mode: none # SHARDED_OPERATION_MODE, how different replicas will shard backing up data for tables. Options are: none (no sharding), table (table granularity), database (database granularity), first-replica (on the lexicographically sorted first active replica). If left empty, then the "none" option will be set as default.
173186

174187
cpu_nice_priority: 15 # CPU niceness priority, to allow throttling CPU intensive operation, more details https://manpages.ubuntu.com/manpages/xenial/man1/nice.1.html
@@ -549,6 +562,7 @@ You can't run watch twice with the same parameters even when `allow_parallel: tr
549562
- Optional string query argument `watch_interval` or `watch-interval` works the same as the `--watch-interval value` CLI argument.
550563
- Optional string query argument `full_interval` or `full-interval` works the same as the `--full-interval value` CLI argument.
551564
- Optional string query argument `watch_backup_name_template` or `watch-backup-name-template` works the same as the `--watch-backup-name-template value` CLI argument.
565+
- Optional string query argument `schedule` works the same as the `--schedule value` CLI argument (named cron driven backup chain in `name=<name>,full=<cron>[,increment=<cron>][,full_type=create|rebase][,delete_previous_cycle=true|false]` format, can be specified multiple times, mutually exclusive with `watch_interval`/`full_interval`).
552566
- Optional string query argument `table` works the same as the `--table value` CLI argument (backup only selected tables).
553567
- Optional string query argument `partitions` works the same as the `--partitions value` CLI argument (backup only selected partitions).
554568
- Optional boolean query argument `schema` works the same as the `--schema` CLI argument (backup schema only).
@@ -1091,17 +1105,22 @@ NAME:
10911105
clickhouse-backup watch - Run infinite loop which create full + incremental backup sequence to allow efficient backup sequences
10921106
10931107
USAGE:
1094-
clickhouse-backup watch [--watch-interval=1h] [--full-interval=24h] [--watch-backup-name-template=shard{shard}-{type}-{time:20060102150405}] [-t, --tables=<db>.<table>] [--partitions=<partitions_names>] [--schema] [--rbac] [--configs] [--skip-check-parts-columns]
1108+
clickhouse-backup watch [--watch-interval=1h] [--full-interval=24h] [--watch-backup-name-template=shard{shard}-{type}-{time:20060102150405}] [--schedule=name=<name>,full=<cron>,increment=<cron>] [-t, --tables=<db>.<table>] [--partitions=<partitions_names>] [--schema] [--rbac] [--configs] [--skip-check-parts-columns]
10951109
10961110
DESCRIPTION:
1097-
Execute create_remote + delete local, create full backup every `--full-interval`, create and upload incremental backup every `--watch-interval` use previous backup as base with `--diff-from-remote` option, use `backups_to_keep_remote` config option for properly deletion remote backups, will delete old backups which not have references from other backups
1111+
Execute create_remote + delete local, create full backup every `--full-interval`, create and upload incremental backup every `--watch-interval` use previous backup as base with `--diff-from-remote` option, use `backups_to_keep_remote` config option for properly deletion remote backups, will delete old backups which not have references from other backups. Use `--schedule` instead of intervals to run backups on cron expressions
10981112
10991113
OPTIONS:
11001114
--config value, -c value Config 'FILE' name. (default: "/etc/clickhouse-backup/config.yml") [$CLICKHOUSE_BACKUP_CONFIG]
11011115
--environment-override value, --env value override any environment variable via CLI parameter
11021116
--watch-interval value Interval for run 'create_remote' + 'delete local' for incremental backup, look format https://pkg.go.dev/time#ParseDuration
11031117
--full-interval value Interval for run 'create_remote'+'delete local' when stop create incremental backup sequence and create full backup, look format https://pkg.go.dev/time#ParseDuration
11041118
--watch-backup-name-template value Template for new backup name, could contain names from system.macros, {type} - full or incremental and {time:LAYOUT}, look to https://go.dev/src/time/format.go for layout examples
1119+
--schedule value Named cron driven backup chain in name=<name>,full=<cron>[,increment=<cron>][,full_type=create|rebase][,delete_previous_cycle=true|false] format, can be specified multiple times, mutually exclusive with --watch-interval and --full-interval
1120+
cron expression contains standard 5 fields, optional leading seconds field and @every/@daily descriptors, see https://pkg.go.dev/github.com/robfig/cron/v3#hdr-CRON_Expression_Format
1121+
name added as prefix to --watch-backup-name-template to isolate backup chains
1122+
full_type=rebase creates scheduled full backup as increment + rebase command, server-side copy of previous chain instead of full re-upload
1123+
delete_previous_cycle=true deletes all older backups of the chain after successful full backup
11051124
--table value, --tables value, -t value Create and upload only objects which matched with table name patterns, separated by comma, allow ? and * as wildcard
11061125
--partitions partition_id Partitions names, separated by comma
11071126
If PARTITION BY clause returns numeric not hashed values for partition_id field in system.parts table, then use --partitions=partition_id1,partition_id2 format
@@ -1142,6 +1161,7 @@ OPTIONS:
11421161
--watch-interval value Interval for run 'create_remote' + 'delete local' for incremental backup, look format https://pkg.go.dev/time#ParseDuration
11431162
--full-interval value Interval for run 'create_remote'+'delete local' when stop create incremental backup sequence and create full backup, look format https://pkg.go.dev/time#ParseDuration
11441163
--watch-backup-name-template value Template for new backup name, could contain names from system.macros, {type} - full or incremental and {time:LAYOUT}, look to https://go.dev/src/time/format.go for layout examples
1164+
--schedule value Named cron driven backup chain for watch in name=<name>,full=<cron>[,increment=<cron>][,full_type=create|rebase][,delete_previous_cycle=true|false] format, can be specified multiple times, mutually exclusive with --watch-interval and --full-interval
11451165
--watch-delete-source, --watch-delete-local explicitly delete local backup during upload in watch
11461166
11471167
```

cmd/clickhouse-backup/main.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -800,11 +800,11 @@ func main() {
800800
{
801801
Name: "watch",
802802
Usage: "Run infinite loop which create full + incremental backup sequence to allow efficient backup sequences",
803-
UsageText: "clickhouse-backup watch [--watch-interval=1h] [--full-interval=24h] [--watch-backup-name-template=shard{shard}-{type}-{time:20060102150405}] [-t, --tables=<db>.<table>] [--partitions=<partitions_names>] [--schema] [--rbac] [--configs] [--skip-check-parts-columns]",
804-
Description: "Execute create_remote + delete local, create full backup every `--full-interval`, create and upload incremental backup every `--watch-interval` use previous backup as base with `--diff-from-remote` option, use `backups_to_keep_remote` config option for properly deletion remote backups, will delete old backups which not have references from other backups",
803+
UsageText: "clickhouse-backup watch [--watch-interval=1h] [--full-interval=24h] [--watch-backup-name-template=shard{shard}-{type}-{time:20060102150405}] [--schedule=name=<name>,full=<cron>,increment=<cron>] [-t, --tables=<db>.<table>] [--partitions=<partitions_names>] [--schema] [--rbac] [--configs] [--skip-check-parts-columns]",
804+
Description: "Execute create_remote + delete local, create full backup every `--full-interval`, create and upload incremental backup every `--watch-interval` use previous backup as base with `--diff-from-remote` option, use `backups_to_keep_remote` config option for properly deletion remote backups, will delete old backups which not have references from other backups. Use `--schedule` instead of intervals to run backups on cron expressions",
805805
Action: func(c *cli.Context) error {
806806
b := backup.NewBackuper(config.GetConfigFromCli(c))
807-
return b.Watch(c.String("watch-interval"), c.String("full-interval"), c.String("watch-backup-name-template"), c.String("tables"), c.StringSlice("partitions"), c.StringSlice("skip-projections"), c.Bool("schema"), c.Bool("rbac"), c.Bool("configs"), c.Bool("named-collections"), c.Bool("skip-check-parts-columns"), c.Bool("delete-source"), version, c.Int("command-id"), nil, c)
807+
return b.Watch(c.String("watch-interval"), c.String("full-interval"), c.String("watch-backup-name-template"), c.StringSlice("schedule"), c.String("tables"), c.StringSlice("partitions"), c.StringSlice("skip-projections"), c.Bool("schema"), c.Bool("rbac"), c.Bool("configs"), c.Bool("named-collections"), c.Bool("skip-check-parts-columns"), c.Bool("delete-source"), version, c.Int("command-id"), nil, c)
808808
},
809809
Flags: append(cliapp.Flags,
810810
cli.StringFlag{
@@ -822,6 +822,15 @@ func main() {
822822
Usage: "Template for new backup name, could contain names from system.macros, {type} - full or incremental and {time:LAYOUT}, look to https://go.dev/src/time/format.go for layout examples",
823823
Hidden: false,
824824
},
825+
cli.StringSliceFlag{
826+
Name: "schedule",
827+
Usage: "Named cron driven backup chain in name=<name>,full=<cron>[,increment=<cron>][,full_type=create|rebase][,delete_previous_cycle=true|false] format, can be specified multiple times, mutually exclusive with --watch-interval and --full-interval\n" +
828+
" cron expression contains standard 5 fields, optional leading seconds field and @every/@daily descriptors, see https://pkg.go.dev/github.com/robfig/cron/v3#hdr-CRON_Expression_Format\n" +
829+
" name added as prefix to --watch-backup-name-template to isolate backup chains\n" +
830+
" full_type=rebase creates scheduled full backup as increment + rebase command, server-side copy of previous chain instead of full re-upload\n" +
831+
" delete_previous_cycle=true deletes all older backups of the chain after successful full backup",
832+
Hidden: false,
833+
},
825834
cli.StringFlag{
826835
Name: "table, tables, t",
827836
Usage: "Create and upload only objects which matched with table name patterns, separated by comma, allow ? and * as wildcard",
@@ -910,6 +919,11 @@ func main() {
910919
Usage: "Template for new backup name, could contain names from system.macros, {type} - full or incremental and {time:LAYOUT}, look to https://go.dev/src/time/format.go for layout examples",
911920
Hidden: false,
912921
},
922+
cli.StringSliceFlag{
923+
Name: "schedule",
924+
Usage: "Named cron driven backup chain for watch in name=<name>,full=<cron>[,increment=<cron>][,full_type=create|rebase][,delete_previous_cycle=true|false] format, can be specified multiple times, mutually exclusive with --watch-interval and --full-interval",
925+
Hidden: false,
926+
},
913927
cli.BoolFlag{
914928
Name: "watch-delete-source, watch-delete-local",
915929
Hidden: false,

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ require (
3939
github.com/prometheus/client_golang v1.23.2
4040
github.com/puzpuzpuz/xsync v1.5.2
4141
github.com/ricochet2200/go-disk-usage/du v0.0.0-20210707232629-ac9918953285
42+
github.com/robfig/cron/v3 v3.0.1
4243
github.com/rs/zerolog v1.35.1
4344
github.com/shirou/gopsutil/v3 v3.24.5
4445
github.com/stretchr/testify v1.11.1
@@ -124,6 +125,7 @@ require (
124125
github.com/mitchellh/mapstructure v1.5.0 // indirect
125126
github.com/moby/docker-image-spec v1.3.1 // indirect
126127
github.com/moby/sys/atomicwriter v0.1.0 // indirect
128+
github.com/morikuni/aec v1.1.0 // indirect
127129
github.com/mozillazg/go-httpheader v0.4.0 // indirect
128130
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
129131
github.com/nwaples/rardecode/v2 v2.2.5 // indirect

0 commit comments

Comments
 (0)