6

I am using some templated timer units to run sets of templated services. There are backup jobs and associated maintenance tasks that require an exclusive lock on the backup repository and cannot run at the same time as the backup jobs. I am trying to figure out how to set up the units so that the jobs are sequenced correctly.

For example, I have the following service templates:

I have the following timer templates:

Where the [email protected] unit starts the corresponding [email protected] instance and might look something like:

[Unit] Description=daily backup of %i [Timer] OnCalendar=daily Unit=backup@%i.service [Install] WantedBy=timers.target 

If I run...

systemctl enable --now [email protected] [email protected] 

...I need to ensure that the clean@foo service does not run until after the backup@foo service has completed.

The only solution I've come up with so far is to drop OnCalendar=daily and instead use explicit start times so that I can guarantee the backup jobs start first (e.g., start the backup jobs at 1AM and the maintenance jobs at 2AM), and then utilize some sort of locking (e.g., the flock) command to ensure that the maintenance jobs don't start until after the backup jobs have completed.

That works but it's a little hacky. If there's a bettery way to solve this using systemd I would like to figure that out.

2 Answers 2

1

After thinking that question through a bit, there's an obvious answer: In the [email protected] template, add a dependency on the corresponding [email protected] instance, like this:

[Unit] After=backup@%i.service 

This works fine as long as the services are started in parallel. I don't know right now if services with the same OnCalendar setting are started in parallel are not; I'll update this answer once I've figured that one out.

1

I solved a similar problem by putting a Requires to the backup job on the clean job. My timer runs the clean job every 6 hours - and due to the dependency, the clean job kicks off the backup, and when the backup is done, it cleans any snapshots that are more than 60 days old. By the way, both services are Type=oneshot.

I also considered having a single backup-and-clean job, which would have also worked in a similar manner.

The downside is that I cannot run a clean job without starting a backup, though this is not an actual problem.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.