Detecting stale locks

I use restic 0.9.3 in a bash script to back up a Debian 9 host to a repository used exclusively by the host.
Is there any restic command I can add to the script to detect stale locks before performing backups?

Thanks for your time.

restic unlock does exactly this.

Before deleting stale locks, is there a way to find out if they exist?


Go to your repo and look in the “locks” folder. Anything in there is a lock. :slight_smile:

There isn’t a restic locks command or anything… but ls /path/to/repo/locks will basically give you that.

Use restic list locks - thanks @cdhowie lol

For a bash script you can use an if statement to check on the repository directory if there is any lock. For example:

if [[ $(ls -A /path/to/repository/locks) ]] ; then
  restic unlock

If the directory is not empty then it will execute unlock. For other backends as sftp, for example, the command will change. For sftp you will need to replace the command for ssh user@ip_addr ls -A /path/to/repository/locks.

1 Like

For a Windows batch file, in case someone else finds this thread and wants one (works with an SMB share):

IF EXIST \\path\to\repository\locks\* (
	ECHO Unlocking database, please wait...
	restic unlock

This one will loop every 5 minutes and quit once nothing exists in the locks folder.

1 Like

You mean like restic list locks? :wink:

1 Like

Yeah, but when I use this command it always displays a lock. For example:

# Using ls in the locks directory
 ~$ ls ~/Downloads/restic-test/locks
 # Output nothing, but using list locks...
 ~$ restic -r ~/Downloads/restic-test list locks

After that I have ls again and the locks directory is empty. I think when you use list locks restic creates a lock and therefore, will have a “lock” to list. Is restic supposed to create a lock for list command? This is only a testing repository, using restic 0.9.5 and there is no other machine backing up in this repository.

Whoops! I stand corrected lol. I actually went looking for a restic show locks and list never crossed my mind. That’s what I get for just skimming the list of commands haha

And actually I’m showing something similar to what @sulfuror just mentioned. Out of morbid curiosity, I started a test backup. I then ran restic unlock while it was running. Everything in /repo/locks was cleared out. Everything. Stale locks, and also a lock that had a modified date within ~4m of the current time (my test backup). It was at this point I saw your reply, ran restic list locks and it actually listed a lock - even though /repo/locks was empty. I waited a few minutes, and something popped in to the locks folder. I ran ‘restic list locks’ again and there were TWO locks still listed.

EDIT: Moved the “restic removed an active lock” bit to my own thread.

Is this normal behavior or a bug? Does restic list locks use a lock of it’s own or something? :thinking:

1 Like

I have just remembered the --no-lock option and when I used restic list locks --no-lock it doesn’t display anything. But still, it is kind of weird that list creates a lock (IMHO) and I was wondering the same thing, if it was a bug, but with the --no-lock option it makes more sense.

1 Like

Ahhhh okay, it uses a lock of it’s own. Makes sense. Kind of lol

1 Like

That seems to be the case. This is how I tested.

In one terminal I ran watch -n .1 ls -lh.
In a second terminal sleep 10; restic list locks.
I went back to the first terminal and saw the locks directory for a brief moment when restic command executed.

Thanks to you guys, I now feel a bit wiser!

1 Like