I'm not sure whether you're asking why preremove isn't running, or whether your script looks OK.
Preremove, and how to test
I don't believe that logrotate will ever run a preremove
script if it's not removing files (per -d
). I set up a test case and ran it under strace
. The logrotate -d
found the files that needed to be removed and wrote that it would have removed them, but did not run the preremove
code. Honestly this makes sense to me, and is what I expect, since the preremove
could do things as damaging as the deletion itself.
To perform a test, I think you need to reproduce your live environment in another directory and just actually run it. Don't use full size files, just use dummy files with a few lines. Create a script to build your test folder, so you can reproduce the test easily. Use touch
to set timestamps. For example:
for n in $(seq 1 30); do
cp test.log test.log.$n
gzip test.log.$n
touch -d "now - $n day" test.log.$n.gz
done
The script itself
grep -q gzip$
is wrong. The $
will disappear in the shell parse. You need to enclose this in single quotes.
- As someone else wrote, I would not expect
$_
to work. Repeat the directory name, or use a variable in the script.
- I don't think you need the
\
escapes at EOL, as suggested.
- You probably want
cp -p
, as someone suggested.
- If your files are large, the
cp
will be slow. If your archive is on the same filesystem, consider using ln
(not ln -s
!) instead. This will be instant.
- Using
file --mime-type
probably works fine, but frankly I'd just base it on the file name in this case, since logrotate will reliably append .gz
on any file for which file
would return gzip
. expr "$1" : '.*gz$'
will give a nonzero number as standard output for any $1
that ends in .gz
. It will give exactly the number 0 as standard output for a filename that does not end in .gz
.