Score:0

Mount/umount remote share before/after running a systemd service

hm flag

I'd like to use systemd to run a script that does a backup by saving the data to remote storage, so:

  • mount remote storage (e.g. NFS share)
  • run the script
  • umount remote storage

This is what I tried to do:

cat /etc/systemd/system/mnt-server-share.mount

[Unit]
Description=Remote storage test
After=network.target

[Mount]
What=server:/share
Where=/mnt/server/share
Type=nfs
Options=defaults,noauto
TimeoutSec=30

[Install]
WantedBy=multi-user.target

cat /etc/systemd/system/backuptest.service

[Unit]
Description=Remote backup test
RequiresMountsFor=/mnt/server/share

[Service]
Type=simple
ExecStart=/usr/local/bin/backup.sh
ExecStop= systemctl stop mnt-server-share.mount

[Install]
WantedBy=multi-user.target

But after script end/fail the remote stogare is not umount. I could put mount/umount in the script, but I wish I could do it with systemd ... Can anyone help me do this?

Score:0
lt flag

In your automount file /etc/systemd/system/mnt-server-share.mount specify an idle timeout value.

TimeoutIdleSec=

      Configures an idle timeout. Once the mount has been idle for the specified time, systemd will attempt to unmount. Takes a unit-less value in seconds, or a time span value such as "5min 20s". Pass 0 to disable the timeout logic. The timeout is disabled by default.

From your example:

[Unit]
Description=Remote storage test
After=network.target

[Mount]
What=server:/share
Where=/mnt/server/share
Type=nfs
Options=defaults,noauto
TimeoutSec=30
TimeoutIdleSec=1m

[Install]
WantedBy=multi-user.target
orsomannaro avatar
hm flag
Yes, it works, thanks! I thought I could eliminate `ExecStop` instead, doing some tests, it seems to be necessary: why?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.