I have a Mac mini running macOS Monterey, and it has InfluxDB running in a Docker container. I'm trying to set up a nightly cronjob via a shell script I'm calling to back the data up, zip it, and only keep the seven most recent backups. The backing up part works perfectly regardless of whether I'm running it manually or via cron, but the bit that's giving me grief is in cron for the shell expansion to get the current number of backup files so I can keep only the most recent ones.
I've stripped the whole thing down to just trying to figure out how to get shell expansion working at all under cron, with zero luck.
The simplest thing I've come up with is this, which behaves (correctly) as the following when running it manually:
$ /bin/bash -c 'BACKUPS=(/Users/virtualwolf/Documents/InfluxDB_Backups/*) && echo "Number of backups: ${#BACKUPS[*]}" && echo "Oldest backup: ${BACKUPS[0]}"'
Number of backups: 10
Oldest backup: /Users/virtualwolf/Documents/InfluxDB_Backups/2022-04-12_00-30.zip
If I put that exact same thing into cron, I get the following:
Number of backups: 1
Oldest backup: /Users/virtualwolf/Documents/InfluxDB_Backups/*
I know by default cron uses /bin/sh
and I've tried setting SHELL=/bin/bash
at the top of my crontab
as well, to no avail (though I wouldn't have thought that would have any effect if I'm either calling a script directly with a shebang of #!/bin/bash
?).
Am I missing something obvious here?
[EDIT]
To clarify things a bit, I'm currently testing solely with cron and have excluded running an actual script file just to get down to the minimum possible scenario, the relevant crontab
entry looks like this:
* * * * * /bin/bash -c 'BACKUPS=(/Users/virtualwolf/Documents/InfluxDB_Backups/*) && echo Number of backups: ${#BACKUPS[*]} && echo Oldest backup: ${BACKUPS[0]}'
If I copy that exact same line (/bin/bash -c 'BACKUPS=(/Users/virtualwolf/Documents/InfluxDB_Backups/*) && echo Number of backups: ${#BACKUPS[*]} && echo Oldest backup: ${BACKUPS[0]}'
) into my terminal, it works as expected and it lists ten files and shows the oldest backup. When it's executed by cron
, the shell expansion hasn't occurred and it's taken the BACKUPS
variable as the literal string /Users/virtualwolf/Documents/InfluxDB_Backups/*
.