Skip to content

Commit 859da21

Browse files
optimize systemctl commands (#531)
* optimize systemctl commands * don't need to call daemon-reload on setting up new files * update documentation * fix completion tests * add tests on reload flag * Add support for --reload flag in createSchedule and enhance systemd handler - Implemented the --reload flag in createSchedule to set the reload flag for jobs. - Updated the systemd handler to manage reload hooks and execute daemon-reload when necessary. - Improved documentation for keyfile generation and schedule commands for clarity. * Refactor command flags and enhance systemd handler tests for reload functionality
1 parent 63dee21 commit 859da21

9 files changed

Lines changed: 163 additions & 87 deletions

File tree

commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func getOwnCommands() []ownCommand {
106106
flags: map[string]string{
107107
"--no-start": "don't start the job after installing (systemd/launch only)",
108108
"--start": "start the job after installing (systemd/launch only)",
109+
"--reload": "force a systemctl daemon-reload after setting up the files (systemd only, available since v0.32.0)",
109110
"--all": "add all scheduled jobs of all profiles and groups",
110111
},
111112
},

commands_schedule.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ func createSchedule(_ io.Writer, ctx commandContext) error {
5656
jobs[id].SetFlag("no-start", "")
5757
}
5858
}
59+
if slices.Contains(args, "--reload") {
60+
for id := range jobs {
61+
jobs[id].SetFlag("reload", "")
62+
}
63+
}
5964

6065
allJobs = append(allJobs, profileJobs{schedulerConfig: scheduler, name: profileName, jobs: jobs})
6166
}

commands_schedule_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func TestCreateScheduleOverwriteExistingIntegrationUsingCrontab(t *testing.T) {
263263
config: cfg,
264264
global: global,
265265
request: Request{
266-
arguments: []string{"--all"},
266+
arguments: []string{"--all", "--reload", "--no-start"},
267267
},
268268
},
269269
}

complete_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,15 @@ func TestCompleter(t *testing.T) {
271271
{args: []string{"self-update", "-q"}, expected: nil},
272272

273273
// Can completion commands after flags
274-
{args: []string{"--verbose", "schedule", "-"}, expected: []string{"--all", "--no-start", "--start"}},
275-
{args: []string{"--log", "file", "schedule", "-"}, expected: []string{"--all", "--no-start", "--start"}},
274+
{args: []string{"--verbose", "schedule", "-"}, expected: []string{"--all", "--no-start", "--reload", "--start"}},
275+
{args: []string{"--log", "file", "schedule", "-"}, expected: []string{"--all", "--no-start", "--reload", "--start"}},
276276

277277
// Flags are returned only once
278278
{args: []string{"--verb"}, expected: []string{"--verbose"}},
279279
{args: []string{"--verb", "--verb"}, expected: []string{"--verbose"}},
280280
{args: []string{"--verbose", "--verb"}, expected: nil},
281-
{args: []string{"schedule", "-"}, expected: []string{"--all", "--no-start", "--start"}},
282-
{args: []string{"schedule", "--all", "-"}, expected: []string{"--no-start", "--start"}},
281+
{args: []string{"schedule", "-"}, expected: []string{"--all", "--no-start", "--reload", "--start"}},
282+
{args: []string{"schedule", "--all", "-"}, expected: []string{"--no-start", "--reload", "--start"}},
283283

284284
// Exact command match returns nothing (no duplication)
285285
{args: []string{"schedule"}, expected: nil},
@@ -295,9 +295,9 @@ func TestCompleter(t *testing.T) {
295295
{args: []string{"__POS:2", "--log", "out.log", "--verbose", "schedule", "-"}, expected: []string{RequestFileCompletion}},
296296
{args: []string{"__POS:4", "--log", "out.log", "--verbose", "schedule", "-"}, expected: nil},
297297
{args: []string{"__POS:4", "--log", "out.log", "--verbose", "schedule"}, expected: nil},
298-
{args: []string{"__POS:5", "--log", "out.log", "--verbose", "schedule", "-"}, expected: []string{"--all", "--no-start", "--start"}},
299-
{args: []string{"__POS:5", "--log", "out.log", "--verbose", "schedule"}, expected: []string{"--all", "--no-start", "--start"}},
300-
{args: []string{"__POS:INVALID", "--log", "out.log", "--verbose", "schedule", "-"}, expected: []string{"--all", "--no-start", "--start"}},
298+
{args: []string{"__POS:5", "--log", "out.log", "--verbose", "schedule", "-"}, expected: []string{"--all", "--no-start", "--reload", "--start"}},
299+
{args: []string{"__POS:5", "--log", "out.log", "--verbose", "schedule"}, expected: []string{"--all", "--no-start", "--reload", "--start"}},
300+
{args: []string{"__POS:INVALID", "--log", "out.log", "--verbose", "schedule", "-"}, expected: []string{"--all", "--no-start", "--reload", "--start"}},
301301
{args: []string{"__POS:INVALID", "--log", "out.log", "--verbose", "schedule"}, expected: nil},
302302

303303
// Unknown is delegated to restic

docs/content/schedules/commands.md

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@ title: "Schedule Commands"
33
weight: 20
44
---
55

6+
resticprofile supports the following commands:
7+
- **schedule**
8+
- **unschedule**
9+
- **status**
610

7-
resticprofile accepts these commands:
8-
- **schedule**
9-
- **unschedule**
10-
- **status**
11+
These commands apply to the profile or group specified by `--name`, or to all profiles when `--all` is used.
1112

12-
These commands operate on the profile or group selected by `--name`, or on all profiles when `--all` is passed.
13-
14-
{{% notice style="warning" %}}
15-
Before version `0.29.0`, the `--name` flag on a group selected all profiles in the group for scheduling, similar to running the schedule command on each profile individually.
16-
17-
Version `0.29.0` introduced group scheduling: The group schedule works at the group level and runs all profiles one by one when triggered.
18-
{{% /notice %}}
13+
{{% notice style="warning" %}}
14+
In versions prior to `0.29.0`, the `--name` flag for a group scheduled all profiles in the group individually, as if the schedule command was run on each profile.
1915

16+
Starting with version `0.29.0`, group scheduling was introduced. This feature schedules at the group level, executing all profiles sequentially when triggered.
17+
{{% /notice %}}
2018

2119
Examples:
2220
```shell
@@ -25,52 +23,53 @@ resticprofile --name group schedule
2523
resticprofile schedule --all
2624
```
2725

28-
Schedules are always independent, regardless of whether they are created with `--all` or from a single profile.
26+
Schedules are always independent, whether created with `--all` or a single profile.
2927

3028
### schedule command
3129

3230
Install all schedules defined in the selected profile(s).
3331

34-
Note: On systemd, you need to `start` the timer once to enable it. Otherwise, it will only be enabled on the next reboot. If you don't want to start (and enable) it now, pass the `--no-start` flag to the command.
32+
**Note:** On systemd, you must `start` the timer once to enable it. Otherwise, it will only activate after the next reboot. To skip starting (and enabling) it now, use the `--no-start` flag.
3533

36-
If you use the `--all` flag to schedule all profiles at once, use either `user` mode or `system` mode. Combining both will not schedule tasks properly:
37-
- If the user is not privileged, only `user` tasks will be scheduled.
38-
- If the user is privileged, all schedules will be `system` schedules.
34+
When using the `--all` flag to schedule all profiles at once, choose either `user` mode or `system` mode. Mixing both will cause scheduling issues:
35+
- Non-privileged users can only schedule `user` tasks.
36+
- Privileged users will schedule all tasks as `system` tasks.
37+
38+
Use the `--reload` flag to trigger a `systemctl daemon-reload` after setting up the schedule files. This is helpful if systemd fails to detect manually added dependencies in the service file. The flag is available starting from version 0.32.0.
3939

4040
{{% notice style=tip %}}
41-
Before version `v0.30.0`, resticprofile did not keep a state of the schedule and unschedule commands. If you needed to make many changes to your profiles (e.g., moving, renaming, deleting), it was recommended to unschedule everything using the `--all` flag before making changes. This is no longer necessary since version `v0.30.0`.
41+
Before version `v0.30.0`, resticprofile did not track the state of schedule and unschedule commands. If you needed to make significant changes to profiles (e.g., moving, renaming, deleting), it was recommended to unschedule everything using the `--all` flag first. This is no longer required as of version `v0.30.0`.
4242
{{% /notice %}}
4343

4444
### unschedule command
4545

46-
Remove all schedules defined on the selected profile, or all profiles using the `--all` flag.
46+
Remove all schedules from the selected profile or all profiles using the `--all` flag.
4747

48-
Before version `v0.30.0`, the `--all` flag didn't remove schedules on deleted or renamed profiles.
48+
Before `v0.30.0`, the `--all` flag did not remove schedules for deleted or renamed profiles.
4949

50-
> [!NOTE]
51-
> The behavior of the `unschedule` command changed in version `v0.30.0`:
52-
>
53-
> It now deletes any schedule associated with the profile name, or any profile in the configuration file with `--all` (including deleted profiles).
50+
> [!NOTE]
51+
> Starting with `v0.30.0`, the behavior of the `unschedule` command changed:
52+
> - Without the `--all` flag, it deletes schedules associated with the profile name.
53+
> - With the `--all` flag, it removes all profiles from the configuration file, including deleted and renamed ones.
5454
5555
### status command
5656

57-
Print the status of all installed schedules for the selected profile(s).
57+
Print the status of all installed schedules for the selected profiles.
5858

59-
The `status` command output depends on the OS. Refer to the [examples]({{% relref "/schedules/examples" %}}) for expected output.
59+
The `status` command output varies by OS. See the [examples]({{% relref "/schedules/examples" %}}) for details.
6060

6161
### run-schedule command
6262

63+
This command allows the scheduler to instruct resticprofile to run according to a schedule. It configures the appropriate log output (`schedule-log`) and other schedule-specific flags.
6364

64-
This command is used by the scheduler to tell resticprofile to execute within a schedule. It sets the proper log output (`schedule-log`) and other schedule-specific flags.
65-
66-
If you're scheduling resticprofile manually, use this command. It executes the profile with all `schedule-*` parameters defined in the profile.
65+
If you're manually scheduling resticprofile, use this command. It runs the profile with all `schedule-*` parameters defined in the profile.
6766

68-
This command takes one argument: the command name followed by the profile name, separated by an `@` sign.
67+
The command requires one argument: the command name followed by the profile name, separated by an `@` symbol.
6968

7069
```shell
7170
resticprofile run-schedule backup@profile
7271
```
7372

7473
{{% notice info %}}
75-
For the `run-schedule` command, you cannot specify the profile name using the `--name` flag.
76-
{{% /notice %}}
74+
The `--name` flag cannot be used to specify the profile name with the `run-schedule` command.
75+
{{% /notice %}}

docs/content/usage/keyfile.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,23 @@ weight: 10
55

66
## Generating random keys
77

8-
resticprofile has a handy tool to generate cryptographically secure random keys encoded in base64. You can simply put this key into a file and use it as a strong key for restic.
8+
Resticprofile includes a tool to generate cryptographically secure, base64-encoded random keys. Save the key to a file and use it as a strong key for Restic.
99

10-
- On Linux and FreeBSD, the generator uses `getrandom(2)` if available, `/dev/urandom` otherwise.
11-
- On OpenBSD and macOS, the generator uses `getentropy(2)`.
12-
- On other Unix-like systems, the generator reads from `/dev/urandom`.
13-
- On Windows systems, the generator uses the uses the RtlGenRandom API.
14-
- On JS/Wasm, the generator uses the Web Crypto API.
15-
- On WASIP1/Wasm, the generator uses `random_get` from `wasi_snapshot_preview1`.
10+
- On Linux, FreeBSD, Dragonfly, and Solaris, Reader uses `getrandom(2)`.
11+
- On legacy Linux (< 3.17), it uses `/dev/urandom`.
12+
- On macOS, and OpenBSD Reader, uses `arc4random_buf(3)`.
13+
- On NetBSD, Reader uses the kern.arandom sysctl.
14+
- On Windows, Reader uses the ProcessPrng API.
1615

17-
[Reference from the Go documentation](https://golang.org/pkg/crypto/rand/#pkg-variables)
16+
[Reference from the Go cryto library documentation](https://golang.org/pkg/crypto/rand/#pkg-variables)
1817

1918
```shell
2019
resticprofile generate --random-key
2120
```
2221

23-
generates a 1024 bytes random key (converted into 1368 base64 characters) and displays it on the console
22+
Generates a 1024-byte random key (converted to 1368 Base64 characters) and displays it in the console.
2423

25-
To generate a different size of key, you can specify the bytes length on the command line:
24+
To generate a key of a different size, specify the byte length in the command line.
2625

2726
```shell
2827
resticprofile generate --random-key 2048

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/creativeprojects/resticprofile
22

3-
go 1.24.3
3+
go 1.24.5
44

55
require (
66
github.com/Masterminds/semver/v3 v3.3.1

0 commit comments

Comments
 (0)