#205 Suggestions for more robust borgmatic systemd service

Open
opened 1 month ago by adatum · 6 comments
adatum commented 1 month ago

Would it make sense to use systemd-inhibit in the borgmatic.service file to prevent the system from being suspended or shut down while a scheduled backup is underway?

eg. modifying line 6 to something like

ExecStart=systemd-inhibit --who="borgmatic" --why="Prevent interrupting scheduled backup" /root/.local/bin/borgmatic

Also, borgmatic.timer specifies Persistent=true which should cause borgmatic to trigger on boot if the schedule was missed. Perhaps a delay should be incorporated so that borgmatic doesn’t start backing up immediately while many programs are loading. This might be bad both for system responsiveness and due to a greater chance of files being written to at that time.

Would it make sense to use `systemd-inhibit` in the [borgmatic.service](https://projects.torsion.org/witten/borgmatic/raw/branch/master/sample/systemd/borgmatic.service) file to prevent the system from being suspended or shut down while a scheduled backup is underway? eg. modifying line 6 to something like ```ExecStart=systemd-inhibit --who="borgmatic" --why="Prevent interrupting scheduled backup" /root/.local/bin/borgmatic``` Also, [borgmatic.timer](https://projects.torsion.org/witten/borgmatic/raw/branch/master/sample/systemd/borgmatic.timer) specifies `Persistent=true` which should cause borgmatic to trigger on boot if the schedule was missed. Perhaps a delay should be incorporated so that borgmatic doesn't start backing up immediately while many programs are loading. This might be bad both for system responsiveness and due to a greater chance of files being written to at that time.
witten commented 1 month ago
Owner

Both of these suggestions make sense to me. Thank you for filing them!

The one argument I could see against systemd-inhibit is that you may actually want the system to suspend while a backup is underway. For instance, let’s say you’re using a laptop and a backup is running, and you have somewhere to go, and you want to take your laptop (oops). You may not want to wait for the backup to finish before suspending. Given that Borg checkpoints every few minutes, an interrupted backup isn’t so bad. Especially since the checkpoints are automatically recoverable on the next backup. And you could in theory lower the checkpoint interval if this was a common thing.

Anyway, this is certainly not a deal breaker, but I wanted to think through the use cases a bit. Do you have thoughts on this? Can you talk about your use case for inhibiting suspend or shutdown when Borg is running? Thanks!

Both of these suggestions make sense to me. Thank you for filing them! The one argument I could see against `systemd-inhibit` is that you may actually want the system to suspend while a backup is underway. For instance, let's say you're using a laptop and a backup is running, and you have somewhere to go, and you want to take your laptop (oops). You may not want to wait for the backup to finish before suspending. Given that Borg checkpoints every few minutes, an interrupted backup isn't *so* bad. Especially since the checkpoints are automatically recoverable on the next backup. And you could in theory lower the checkpoint interval if this was a common thing. Anyway, this is certainly not a deal breaker, but I wanted to think through the use cases a bit. Do you have thoughts on this? Can you talk about your use case for inhibiting suspend or shutdown when Borg is running? Thanks!
witten commented 1 month ago
Owner

As for the suggested delay with Persistent=true, systemd.timer’s RandomizedDelaySec= may do the trick.

As for the suggested delay with `Persistent=true`, `systemd.timer`'s `RandomizedDelaySec=` may do the trick.
nicoulaj commented 1 month ago

@witten: I get your point, maybe that deserves a service level configuration option (eg: in /etc/default/borgmatic) ? For my own use case, I would prefer the change suggested by @adatam (especially as I use the after_backup hook to transfer data remotely).

On a related note (sorry if off topic), I also use a few additions compared to the sample service, some security hardening stuff:

CapabilityBoundingSet=CAP_SYS_RESOURCE CAP_SYS_ADMIN CAP_MKNOD CAP_DAC_READ_SEARCH CAP_SYS_CHROOT CAP_SETPCAP
NoNewPrivileges=yes
PrivateUsers=no
PrivateTmp=yes
PrivateDevices=yes
DevicePolicy=closed
ProtectSystem=strict
ProtectHome=read-only
ProtectControlGroups=yes
ProtectKernelModules=yes
ProtectKernelTunables=yes
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK
RestrictRealtime=yes
RestrictNamespaces=yes
MemoryDenyWriteExecute=yes
LockPersonality=true
SystemCallArchitectures=native
SystemCallFilter=@system-service
ReadWritePaths=-/root/.config -/root/.cache /mnt/backup

And some options to improve system responsiveness during backups:

IOSchedulingClass=idle
CPUSchedulingPolicy=idle
IOReadIOPSMax=/dev/nvme0n1 100

As you can see some options are host specific, but maybe it would be useful to add it commented out ?

@witten: I get your point, maybe that deserves a service level configuration option (eg: in `/etc/default/borgmatic`) ? For my own use case, I would prefer the change suggested by @adatam (especially as I use the `after_backup` hook to transfer data remotely). On a related note (sorry if off topic), I also use a few additions compared to the sample service, some security hardening stuff: ``` CapabilityBoundingSet=CAP_SYS_RESOURCE CAP_SYS_ADMIN CAP_MKNOD CAP_DAC_READ_SEARCH CAP_SYS_CHROOT CAP_SETPCAP NoNewPrivileges=yes PrivateUsers=no PrivateTmp=yes PrivateDevices=yes DevicePolicy=closed ProtectSystem=strict ProtectHome=read-only ProtectControlGroups=yes ProtectKernelModules=yes ProtectKernelTunables=yes RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK RestrictRealtime=yes RestrictNamespaces=yes MemoryDenyWriteExecute=yes LockPersonality=true SystemCallArchitectures=native SystemCallFilter=@system-service ReadWritePaths=-/root/.config -/root/.cache /mnt/backup ``` And some options to improve system responsiveness during backups: ``` IOSchedulingClass=idle CPUSchedulingPolicy=idle IOReadIOPSMax=/dev/nvme0n1 100 ``` As you can see some options are host specific, but maybe it would be useful to add it commented out ?
adatum commented 1 month ago
Poster

@witten The use case for these suggestions in my case is a desktop that is on 24/7. Backups would be scheduled for say 5AM daily, so it’s unlikely that it would be rebooted, or even in use at that time. On the odd chance it were necessary, delaying the reboot a little probably wouldn’t be much of an issue. Then again your point about borg’s checkpoints alleviate the concern about interrupted backups, but it still might be nice to avoid.


As for the laptop use case, your point is well taken. Is there a way to override the systemd-inhibit? I have additional concerns about scheduled backups on laptops:

  • Does it make sense to have scheduled backups for devices that may often be off, or used at unpredictable times?
  • Does it make sense to have backups run on boot if 1) schedules are likely to be missed, and 2) immediate responsiveness is usually desired in laptops?
  • For backups to local network shares, what if it is not mounted or available? (eg. laptop is not at home)
  • For backups using SSH, what if there is no internet connection?

How gracefully are such scenarios/failures handled?

For the second point, perhaps your suggestion of RandomizedDelaySec= could work. It isn’t quite what I had in mind, due to its random nature, but maybe it’s good enough.

I am undecided whether my laptop should have scheduled backups or if I should run borgmatic manually. The tradeoffs in the convenience of automation versus the complexity of a reasonably robust solution are not yet clear to me. This discussion is useful to that end.


@nicoulaj Thanks for the list of additional service options. Maybe the reasons and use case could be documented?

@witten The use case for these suggestions in my case is a desktop that is on 24/7. Backups would be scheduled for say 5AM daily, so it's unlikely that it would be rebooted, or even in use at that time. On the odd chance it were necessary, delaying the reboot a little probably wouldn't be much of an issue. Then again your point about borg's checkpoints alleviate the concern about interrupted backups, but it still might be nice to avoid. --- As for the laptop use case, your point is well taken. Is there a way to override the `systemd-inhibit`? I have additional concerns about scheduled backups on laptops: * Does it make sense to have scheduled backups for devices that may often be off, or used at unpredictable times? * Does it make sense to have backups run on boot if 1) schedules are likely to be missed, and 2) immediate responsiveness is usually desired in laptops? * For backups to local network shares, what if it is not mounted or available? (eg. laptop is not at home) * For backups using SSH, what if there is no internet connection? How gracefully are such scenarios/failures handled? For the second point, perhaps your suggestion of `RandomizedDelaySec=` could work. It isn't quite what I had in mind, due to its random nature, but maybe it's good enough. I am undecided whether my laptop should have scheduled backups or if I should run `borgmatic` manually. The tradeoffs in the convenience of automation versus the complexity of a reasonably robust solution are not yet clear to me. This discussion is useful to that end. --- @nicoulaj Thanks for the list of additional service options. Maybe the reasons and use case could be documented?
adatum commented 1 month ago
Poster

There is an interesting similar discussion about a systemd service and timer for Relax-and-Recover: https://github.com/rear/rear/issues/2139

The use cases for both ReaR and borgmatic have a lot of overlap, so the ideas proposed are likely to be relevant.

There is an interesting similar discussion about a systemd service and timer for Relax-and-Recover: https://github.com/rear/rear/issues/2139 The use cases for both ReaR and borgmatic have a lot of overlap, so the ideas proposed are likely to be relevant.
adatum commented 2 days ago
Poster

For the past couple of weeks, I’ve been running the following daily (prune, create) and weekly (prune, create, and the lengthier check) systemd service/timer pairs. I have not tested edge cases, though it has been working fine under “normal” use.

Most of the configuration is taken from suggestions from the ReaR issue referenced above. A couple of differences are:

  • launching with systemd-inhibit to prevent interrupting backups
  • combining a X minute sleep and setting the timer X minutes earlier as a workaround to avoiding starting a backup immediately upon system startup in case Persistent=true were triggered

Security hardening mentioned above is not included yet.

Here are the four files I have so far, all in /etc/systemd/system:

borgmatic-daily.service

[Unit]
Description=borgmatic daily backup
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
# lowest CPU priority
Nice=19
# lowest I/O priority among 'normal' processes
IOSchedulingClass=best-effort
IOSchedulingPriority=7
IOWeight=100
Restart=no
LogRateLimitIntervalSec=0
# delay start to prevent backups immediately upon system startup if Persistent=true is triggered
ExecStartPre=sleep 30m 
ExecStart=systemd-inhibit --who="borgmatic" --why="Prevent interrupting scheduled backup" /root/.local/bin/borgmatic prune create --syslog-verbosity 1

borgmatic-daily.timer

[Unit]
Description=Run borgmatic daily backup
ConditionACPower=true

[Timer]
# backup will run at time specified here + delay specified by ExecStartPre in .service
OnCalendar=Mon..Fri,Sun 04:30:00
Persistent=true

[Install]
WantedBy=timers.target

borgmatic-weekly.service

[Unit]
Description=borgmatic backup and weekly check
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
# lowest CPU priority
Nice=19
# lowest I/O priority among 'normal' processes
IOSchedulingClass=best-effort
IOSchedulingPriority=7
IOWeight=100
Restart=no
LogRateLimitIntervalSec=0
# delay start to prevent backups immediately upon system startup if Persistent=true is triggered
ExecStartPre=sleep 30m 
ExecStart=systemd-inhibit --who="borgmatic" --why="Prevent interrupting scheduled backup" /root/.local/bin/borgmatic --syslog-verbosity 1

borgmatic-weekly.timer

[Unit]
Description=Run borgmatic backup and weekly check
ConditionACPower=true

[Timer]
# backup will run at time specified here + delay specified by ExecStartPre in .service
OnCalendar=Sat 02:30:00
Persistent=true

[Install]
WantedBy=timers.target
For the past couple of weeks, I've been running the following daily (prune, create) and weekly (prune, create, and the lengthier check) systemd service/timer pairs. I have not tested edge cases, though it has been working fine under "normal" use. Most of the configuration is taken from suggestions from the ReaR issue referenced above. A couple of differences are: * launching with `systemd-inhibit` to prevent interrupting backups * combining a X minute sleep and setting the timer X minutes earlier as a workaround to avoiding starting a backup immediately upon system startup in case Persistent=true were triggered Security hardening mentioned above is not included yet. Here are the four files I have so far, all in `/etc/systemd/system`: ####borgmatic-daily.service ``` [Unit] Description=borgmatic daily backup Wants=network-online.target After=network-online.target [Service] Type=oneshot # lowest CPU priority Nice=19 # lowest I/O priority among 'normal' processes IOSchedulingClass=best-effort IOSchedulingPriority=7 IOWeight=100 Restart=no LogRateLimitIntervalSec=0 # delay start to prevent backups immediately upon system startup if Persistent=true is triggered ExecStartPre=sleep 30m ExecStart=systemd-inhibit --who="borgmatic" --why="Prevent interrupting scheduled backup" /root/.local/bin/borgmatic prune create --syslog-verbosity 1 ``` ####borgmatic-daily.timer ``` [Unit] Description=Run borgmatic daily backup ConditionACPower=true [Timer] # backup will run at time specified here + delay specified by ExecStartPre in .service OnCalendar=Mon..Fri,Sun 04:30:00 Persistent=true [Install] WantedBy=timers.target ``` ####borgmatic-weekly.service ``` [Unit] Description=borgmatic backup and weekly check Wants=network-online.target After=network-online.target [Service] Type=oneshot # lowest CPU priority Nice=19 # lowest I/O priority among 'normal' processes IOSchedulingClass=best-effort IOSchedulingPriority=7 IOWeight=100 Restart=no LogRateLimitIntervalSec=0 # delay start to prevent backups immediately upon system startup if Persistent=true is triggered ExecStartPre=sleep 30m ExecStart=systemd-inhibit --who="borgmatic" --why="Prevent interrupting scheduled backup" /root/.local/bin/borgmatic --syslog-verbosity 1 ``` ####borgmatic-weekly.timer ``` [Unit] Description=Run borgmatic backup and weekly check ConditionACPower=true [Timer] # backup will run at time specified here + delay specified by ExecStartPre in .service OnCalendar=Sat 02:30:00 Persistent=true [Install] WantedBy=timers.target ```
Sign in to join this conversation.
No Milestone
No Assignees
3 Participants
Due Date

No due date set.

Dependencies

This issue currently doesn't have any dependencies.

Loading…
Cancel
Save
There is no content yet.