--exclude-if-present wildcards

Does --exclude-if-present support wildcards?

I’ve got a list of about 100 possible .lock files, the presence of which should cause the directory to be skipped. A bit of initial testing indicates that wildcards don’t seem to work, but I’m hoping that maybe there is a syntax I have missed.

I’ve never used it myself, hence can’t tell for sure, but the documentation indicates that wildcards should work.

I disagree. Aside from mentioning that this option exists, the documentation does not at all mention what argument type it expects or supports.

Don’t know if we see the same content because I see lots of text including some examples using wildcards.

The --excude-if-present does not support wildcards, I’ve clarified this in the documentation:

The other exclude options do support wildcards.

At least in bash you could do:

find /your/path -iname *.lock 2> /dev/null > filename0.txt

This will give you a list like this:

/home/user/dir1/lock1.lock
/home/user/dir2/lock2.lock

Now use sed to cut the *.lock words:

sed -e 's/\<[.a-zA-Z0-9.]*[lock]\>//g' filename0.txt > filename.txt

Result:

/home/user/dir1/
/home/user/dir2/

You can then use --exclude-file="/path/to/your/filename.txt" when performing a backup. In restic help says you can use this flag multiple times, so you don’t have to worry if you have a “static” exclusion list elsewhere. If this .lock files are changing, you can do the find thing every time you will run the backup. It is not ideal but you can easily script it and stop worrying about having to do the two steps every time you need to run a backup.

EDIT:
I made this script just to see if it works for me and it does. My test directory contains 6 sub-dirs and 3 of them have .lock files. This is the output of the latest snapshot when I use ls:

repository b9349ed1 opened successfully, password is correct
snapshot 87970e1c of [/home/dj0k3/testdir] filtered by [] at 2019-02-22 11:53:02.466103641 -0500 EST):
/home
/home/dj0k3
/home/dj0k3/testdir
/home/dj0k3/testdir/dir1
/home/dj0k3/testdir/dir3
/home/dj0k3/testdir/dir5

Sub-dirs 2, 4 and 6 contains those lock files. This is the little script used:

#!/bin/bash
export RESTIC_REPOSITORY="$HOME/Downloads/restic-repo"
export RESTIC_PASSWORD="a6sdf456sdafs"
BACKUP_DIR="$HOME/testdir"
tmplist=$HOME/.tmplist.txt
tmpexclude=$HOME/.tmpexclude.txt

find $BACKUP_DIR -iname *.lock 2> /dev/null > $tmplist
sed -e 's/\<[.a-zA-Z0-9.]*[lock]\>//g' $tmplist > $tmpexclude
echo "Excluding the following directories containing a '.lock' file:"
cat -n $tmpexclude
echo "Starting Backup..."
restic backup -v --exclude-file=$tmpexclude $BACKUP_DIR
rm $tmplist
rm $tmpexclude

With bash process substitution, there’s no reason you even need temp files.

#!/bin/bash
...
restic backup -v \
  --exclude-file=<(
    find $BACKUP_DIR -iname *.lock 2>/dev/null | \
      sed -e 's/\<[.a-zA-Z0-9.]*[lock]\>//g'
  ) \
  $BACKUP_DIR
1 Like

Nice! I don’t know a lot, it is nice to learn new ways to manipulate the awesome bash.