Reputation: 181
Can someone who knows systemd please help me figure out what I am doing wrong?
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. 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)
I am running Linux Mint 18 - Ubuntu 16.04 based - if that makes a difference to the answers.
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.
I have tested the bash script and when it is run, it does what I want.
I have manually stopped the mardibackup.service by running systemctl stop mardibackup.service and it does what I want.
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? Why not just use the shutdown.target and call the service when the ExecStart is triggered?
2) What is ExecStart=/bin/true supposed to do? 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)
#!/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
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)
[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
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)
Anyone with some ideas?
Upvotes: 0
Views: 3299
Reputation: 181
Thank you to @le_me and his submission to the question at
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
So, it is NOW working with the service at /etc/systemd/system/mardibackup.service
defined as
[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.
Upvotes: 1