简体   繁体   English

如何使用systemd激活脚本以在关机时备份文件

[英]How use systemd to activate a script to backup files on shutdown

Can someone who knows systemd please help me figure out what I am doing wrong? 知道systemd的人可以帮助我找出我做错了什么吗?

I am trying to use systemd to trigger a script that will sync (backup) files from the home directory of a user (mardi) to a second drive when the user shuts down her computer. 我正在尝试使用systemd触发一个脚本,该脚本将在用户关闭计算机时将用户(mardi)主目录中的文件同步(备份)到第二个驱动器。 I would prefer to have it only happen on shutdown but if that gets too complicated, then it can happen on shutdown or reboot; 我宁愿让它仅在关机时发生,但是如果操作太复杂,则可能在关机或重新启动时发生; I am OK with that. 我可以。 (I know this is not a particularly good back-up strategy but it is step one in an overall process so please do not provide feedback on the inadvisability of this ; I want to do this and I want to learn how to properly use systemd so please provide feedback that helps) (我知道这不是一个特别好的备份策略,但它是整个过程的第一步,因此请不要提供对此不建议的反馈;我要这样做,我想学习如何正确使用systemd,因此请提供有助于您的反馈)

I am running Linux Mint 18 - Ubuntu 16.04 based - if that makes a difference to the answers. 我正在运行Linux Mint 18-基于Ubuntu 16.04-如果这对答案有所不同。

I have included what I have done below, which is s result of reading the man pages for systemd and the multiple (often conflicting) forum pages here and elsewhere, but I am not getting it to work. 我在下面包括了我所做的事情,这是由于阅读systemd的手册页以及此处和其他地方的多个(通常是冲突的)论坛页面而导致的,但是我无法使其正常工作。

I have tested the bash script and when it is run, it does what I want. 我已经测试了bash脚本,当它运行时,它可以执行我想要的操作。

I have manually stopped the mardibackup.service by running systemctl stop mardibackup.service and it does what I want. 我已经通过运行systemctl stop mardibackup.service手动停止了mardibackup.service,它可以执行我想要的操作。

So I expect I am not defining the unit properly or calling the service properly, but I cannot figure out what to do not. 因此,我希望我没有正确定义该单元或正确调用该服务,但是我不知道该怎么做。

Specific questions: 具体问题:

1) Why use multi-user.target and call the service when the ExecStop is triggered stops? 1)为什么在ExecStop触发停止时使用multi-user.target并调用服务? Why not just use the shutdown.target and call the service when the ExecStart is triggered? 为什么不只使用shutdown.target并在ExecStart触发时调用服务?

2) What is ExecStart=/bin/true supposed to do? 2)ExecStart = / bin / true应该做什么? It does not seem to do anything but it is in many recommended answers. 它似乎没有任何作用,但是在很多建议的答案中都有。

Help?? 救命?? And thank you. 谢谢你

So, here is what I have done: 所以,这是我所做的:

1) Create the following script, using Lucky Backup to generate the rsync command and put it into /home/mardi/Templates/backupmardi.sh (this is working when I execute it manually from the command line) 1)创建以下脚本,使用Lucky Backup生成rsync命令并将其放入/home/mardi/Templates/backupmardi.sh(当我从命令行手动执行该命令时,此命令有效)

#!/bin/bash

# This program synchronizes the contents of mardi home directory
# to the archives disk in /backups/mardi
#
# It checks if a current version of a file is already copied and if so, does not change anything
# If a file is deleted in the home directory, it is NOT deleted from the archives

rsync -h --progress --stats -r -tgo -p -l -D --update --delete-after /home/mardi /mnt/7b2152a9-af3c-4f63-9287-98aacd9cb8ff/backups/

2) Change the permissions of the script file to make it executable 2)更改脚本文件的权限以使其可执行

chmod +x /home/mardi/Templates/backupmardi.sh

3) Create the following shutdown/restart service to call the backup script and put into /etc/systemd/system/mardibackup.service (this does what I want if I manually "Stop" it via systemctl stop mardibackup.service) 3)创建以下关闭/重新启动服务,以调用备份脚本并将其放入/etc/systemd/system/mardibackup.service(如果我通过systemctl手动“停止”它,则可以执行此操作stop mardibackup.service)

[Unit]
Description=Backup Mardi home dir to archives

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/bin/bash /home/mardi/Templates/backupmardi.sh

[Install]
WantedBy=multi-user.target

4) create the symlink that will call the shutdown/restart script when the system is shutdown/restarted 4)创建符号链接,该符号链接将在系统关闭/重新启动时调用关闭/重新启动脚本

systemctl enable /etc/systemd/system/mardibackup.service
    (Note if you ever want to remove the symlink, enter: systemctl disable mardibackup.service)
systemctl start mardibackup.service

5) Shutdown the system (I tried multiple shutdowns but it does not call the backup script backupmardi.sh) 5)关闭系统(我尝试了多次关闭,但未调用备份脚本backupmardi.sh)

Anyone with some ideas? 任何有想法的人吗?

Thank you to @le_me and his submission to the question at 感谢@le_me及其对以下问题的投稿

https://superuser.com/questions/1016827/how-do-i-run-a-script-before-everything-else-on-shutdown-with-systemd https://superuser.com/questions/1016827/how-do-i-run-a-script-before-everything-else-on-shutdown-with-systemd

I was missing one line in my [Unit] definiiton 我在[单位]定义中缺少一行

RequiresMountsFor=/home /mnt/7b2152a9-af3c-4f63-9287-98aacd9cb8ff

/home is the main drive home directory /mnt/7b2152a9-af3c-4f63-9287-98aacd9cb8ff is the secondary drive to which I am sending backup files /home是主驱动器主目录/mnt/7b2152a9-af3c-4f63-9287-98aacd9cb8ff是要向其发送备份文件的辅助驱动器

So, it is NOW working with the service at /etc/systemd/system/mardibackup.service defined as 因此,现在可以将/etc/systemd/system/mardibackup.service的服务定义为

[Unit]
Description=Backup Mardi home dir to archives
RequiresMountsFor=/home /mnt/7b2152a9-af3c-4f63-9287-98aacd9cb8ff

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/bin/bash /home/mardi/Templates/backupmardi.sh

[Install]
WantedBy=multi-user.target

The rest of the instructions above are still good. 上面的其余说明仍然很好。

I guess the drives were being unmounted before the service was being called so the service could not do what it was supposed to do. 我猜想在调用服务之前要先卸下驱动器,以使该服务无法执行应有的功能。

If anyone sees a flaw in this, feel free to comment. 如果有人发现此缺陷,请随时发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM