在运行 systemd 服务之前/之后挂载/卸载远程共享

在运行 systemd 服务之前/之后挂载/卸载远程共享

我想使用systemd运行一个脚本,通过将数据保存到远程存储来进行备份,因此:

  • 挂载远程存储(例如 NFS 共享)
  • 运行脚本
  • 卸载远程存储

这是我尝试做的:

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

但脚本结束/失败后,远程存储不存在umount。我可以在脚本中输入mount/ umount,但我希望我可以使用systemd... 有人能帮我做到这一点吗?

答案1

在自动挂载文件中/etc/systemd/system/mnt-server-share.mount指定空闲超时值。

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.

从你的例子来看:

[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

相关内容