digint / btrbk

Tool for creating snapshots and remote backups of btrfs subvolumes
https://digint.ch/btrbk/
GNU General Public License v3.0
1.64k stars 120 forks source link

btrbk.timer runs too soon during boot process #486

Open wolfgangrupprecht opened 2 years ago

wolfgangrupprecht commented 2 years ago

As is, btrbk.timer runs so early during the boot process that the hostname hasn't even been assigned yet. This also happens long before vital services are started.

To demonstrate, shut the backup server down before midnight and boot after midnight. Then run:

journalctl --since 0:0:0

Notice that btrbk runs in the midst of everything slowly being started up. On my fedora-36 system the hostname was still the temporary boot placeholder name of "fedora" instead of its actual hostname. The network wasn't up and dns wouldn't have been working either. I'm not sure what one is supposed to wait for boot to finish under systemd.

edit: I had thought OnActiveSec=2min in addition to OnCalendar=daily would take the union of the two constraints, but that doesn't appear to be the case.

digint commented 2 years ago

I'm afraid I'm no expert on systemd (I have strong opinions on it and therefore don't use it whenever possible).

That said, I think you would need to add something like this in btrbk.service:

After=network.target

I'm not sure if it is wise to add something like this as default, as in many setups btrbk runs without network (and what happens if there is no network.target? Is there always a network.target?)

Please report back if that works, in the meantime I will add a "help wanted" flag on this issue.

wolfgangrupprecht commented 2 years ago

I'm afraid I never agreed with the direction systemd was taking things so I resisted leaning about it until now. Hopefully someone that understands this in their gut will have a better feel for how to solve this dilemma.

network.target seemed to soon anyway since there was potentially still lots of setup that could happen after it. There is another very late stage target called default.target which is mentioned in "man systemd.special". That's what I'm using locally in both the .timer and .service files. I may not need it in both.

[Unit] Description=... After=default.target

PsyBlade42 commented 2 years ago

Another aspect of this is that on my system the timer runs before the filesystems it tries to snapshot are actually mounted. Failing for obvious reasons.

I probably won't be looking into this since I want to keep my cronjobs for the time being. But afaik After=foo only does something if foo is going to be started and ignored otherwise.

sidju commented 1 year ago

For those looking into this in the future, here is my config:

[Unit]
Description=btrbk daily backup
Requires=network-online.target
After=network-online.target
RequiresMountsFor=/btrfs

[Timer]
OnCalendar=daily
AccuracySec=10min
Persistent=true

[Install]
WantedBy=timers.target

network-online.target means it waits until your network manager declares networking to have been configured (exists by default in systemd, but probably requires your network manager to support setting it). /btrfs is my btrfs mountpoint.

(Word of warning, RequiresMountsFor ignores filesystems configured with noauto (don't auto mount at boot))