0

Is it possible to configure systemd service that will delay the boot process (by about 10-20 minutes) to run a local backup script (something like dd if=/dev/sdb of=/dev/sdc) while filesystem is still read-only? Or maybe it's possible to execute such task just before shutdown?

I have tried several configurations, but still cannot achieve this behavior in my Ubuntu 22 box.

This variant causes timeout and boot process failed - system goes into maintenance mode:

[Unit]
Description=Backup on demand
DefaultDependencies=no
After=systemd-fsck-root.service
Before=systemd-remount-fs.service -.mount local-fs-pre.target local-fs.target

[Service]
Type=oneshot
TimeoutSec=0
ExecStart=/root/backup-on-boot.sh

[Install]
WantedBy=local-fs.target

This variant doesn't block boot process and run in background (which is undesired behavior)

[Unit]
Description=Backup on demand
After=local-fs.target
Wants=local-fs.target

[Service]
Type=oneshot
TimeoutSec=0
ExecStart=/root/backup-on-boot.sh

[Install]
WantedBy=multi-user.target
5
  • Have you considered using snapshots rather than a full copy? (requires LVM, or BTRFS, or ZFS, or a SAN)
    – symcbean
    Commented Jun 8 at 11:05
  • This system is intended to run without kb/screen and boot from USB stick. The purpose of this backup is to have a bootable copy of the system on another flash drive, which can easily replace the system drive in case of a failure. (Of course dd is overkill, especially for flash drives, so I already switched to blocksync). Commented Jun 8 at 12:02
  • Yes - so you create a snapshot, continue with boot, while writing the snapshot to the backup media in the background.
    – symcbean
    Commented Jun 8 at 12:05
  • Ah, I see. Yes, this could be a solution. Unfortunately I have used F2FS without LVM for system drive. Commented Jun 8 at 12:16
  • Just a thought but you might want to make a custom target and make that the default; this target could have a unit that backs up the system and then switches to the normal target state (possibly via systemctl isolate). No idea if this would work, but it's something to think about! Commented Jun 8 at 12:43

0

You must log in to answer this question.

Browse other questions tagged .