Too many backups all at once

I backup my /home every hour from a cron job which shows this -

00 06-22/1 * * * /usr/local/bin/restic -r /mnt/backb/boudiccas -p ~/bin/foo.txt backup /home/boudiccas --exclude-file /home/boudiccas/bin/excluded.txt

and there are currently 7 instances of it trying to complete their backups!

So how can I get to the point where a backup is not started until any previous ones have ended please?

This is on a fully up-to-date Debian 10.

1 Like

Restic itself doesn’t provide any tools for this but you can write a small bash script that creates and checks a lock file or use systemd timers.

You can use the flock command-line utility to add a lock guard. In particular, you will want to use the -n argument which will tell flock that it shouldn’t wait to acquire the lock – if it can’t acquire the lock it will instead immediately exit. You’d use it like:

flock -n /path/to/lockfile /usr/local/bin/restic -r ...

You can put the lock file wherever makes sense for you, such as /tmp or /run or pretty much anywhere else. It will be automatically created as an empty file on the first run.

That’s two great answers above. Personally in my backup script I just check if there’s a process with the name restic running and if there is the script exits.

1 Like

Great answers above. I’d just run it less often and avoid the problem altogether.

On my personal computer, I just run the backups manually. That’s for sure not gong to have several runs at the same time (to the same target repo - I do run two backups at the same time but they’re to different repos) :slight_smile: Do you really need it scheduled? :wink:

1 Like

I have made a restic shell script so it runs multiple commands in order. I use cron to run this script everyday.

#!/bin/bash
#Script to backup files using restic to external RAID
echo “…START RESTIC BACKUPS”
echo “…RESTIC REPO: /MNT/HERMES/ARKADI-ONE-BACKUP”
source /home/admin/.restic-arkadi
echo “…RESTIC BACKUP: /HOME/ADMIN”
restic backup /home/admin/ --tag home
echo “…RESTIC BACKUP: Nextcloud files”
restic backup /home/yunohost.app/nextcloud/data/johnny/files/ --tag nextcloud
echo “…RESTIC CHECK REPO”
restic check

I am doing this precisely because I don’t want to worry about how long a cron job is taking. This is on my server, not my personal computer.

Since you are using Debian 10, it means you have systemd at your disposal. So my recommendation would be to use a .timer and .service unit and let systemd do the scheduling for your backup task.
systemd will automatically take care to not run any new instances if there is already an instance running.

1 Like

Using a systemd service has the additional benefit, that you have a lot of knobs to e.g. restrict IO or CPU time.