You are not logged in.

#1 2018-08-25 13:56:50

karcher
Member
Registered: 2018-01-09
Posts: 140

Automatic backup with systemd after shutdown initiation

Hi all,

I'm trying to make automatic backups with the help of systemd and borg to an external USB drive every time I initiate a system shutdown.
I have created and modified several times a systemd service (/etc/systemd/system/backup.service):

[Unit]
Description=backup
RequiresMountsFor=/home /mnt/backups
Before=shutdown.target reboot.target
Conflicts=reboot.target

[Service]
Type=simple
ExecStart=/bin/true
ExecStop=/usr/bin/borgmatic
RemainAfterExit=true

[Install]
WantedBy=multi-user.target

but it doesn't work! After I initiate a system shutdown the system powers off without making any backup!

Any ideas how can I make it work?

Offline

#2 2018-08-25 16:26:21

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 78,840

Re: Automatic backup with systemd after shutdown initiation

Is the service running before you shutdown?
Does "ExecStop=/usr/bin/echo foo > /home/bar" leave a trace?

Offline

#3 2018-08-27 15:01:39

karcher
Member
Registered: 2018-01-09
Posts: 140

Re: Automatic backup with systemd after shutdown initiation

@seth: Thanks. I was able to find out what is going on by using:

$ journalctl -u <myservice>

Now I have two other minor problems:
1. The backup runs even on reboot which is annoying since the backup process can take over 30 min. How can I force systemd to run myservice only on shutdown? (Before=shutdown.target doesn't help)
2. During shutdown, when tty1 appears there is no message that myservice has started. Is there a way to get some info about the progress or status of the service? (StandardOutput=tty and StandardError=tty do not help)

This is my current service configuration:

[Unit]
Description=backup
RequiresMountsFor=/home /mnt/backups
Before=shutdown.target

[Service]
TimeoutStartSec=0
Type=oneshot
ExecStart=/bin/true
ExecStop=/usr/bin/borgmatic --verbosity 2
RemainAfterExit=true
User=user
Group=user
StandardOutput=tty
StandardError=tty
TimeoutSec=infinity
KillMode=none
SendSIGKILL=no

[Install]
WantedBy=multi-user.target

Offline

#4 2018-08-27 15:48:57

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 78,840

Re: Automatic backup with systemd after shutdown initiation

"Conflicts=reboot.target"?
Try kmesg as output.

Offline

#5 2018-09-21 06:24:42

karcher
Member
Registered: 2018-01-09
Posts: 140

Re: Automatic backup with systemd after shutdown initiation

Sorry for the late response.

The option:

Conflicts=reboot.target

didn't help. It was just throwing me to a tty before shutting down without any message and after at least half an hour the system was restarting without making a backup.
I have tried some other options but I was in the same situation, namely either restart and backup or restart and wait for nothing.
Finally I gave up making experiments because I couldn't work.
If someone has a solution that works for sure I would be glad to hear it.

The option:

StandardOutput=kmsg

seem to be working fine.

Offline

Board footer

Powered by FluxBB