Directions to modify cron script

TrueNAS Scale version: ElectricEel-24.10.2

I have had some cron scripts running for ages, but at some point, I suspect after one of the recent upgrades, the don’t work completely. I know there are a lot of a number of scripts that do what I want to do and a lot more, but I’ve always tried to keep things simple. (try is the operative word!)

I’m looking for some directions to pursue in trouble shooting this issue. Places to look, commands to perform to narrow the problem down. Searching this forum was my first step, but most hits say “Oh, I found the problem and fixed it.” without explaining what they did or how they fixed it.

So here’s what my script does:

  1. Copies the TrueNAS sqlite3 configuration file to a specific location - Works
  2. Validates the integrity of the copied file using sqlite3 - Works
  3. tars the copied file, encrypts it with a passphrase - Problem
  4. Encrypts the tared file with a passphrase - never gets here
  5. Emails the encrypted tar file to me - never gets here

So I setup a Pool named Home, located at /mnt/Home.
I then created the subdirectory /mnt/Home/sysfiles
Within that directory I created the subdirectory cBackups

Visually it looks like this:

 --> mnt
      |
      +--> Home
                 |
                 +-- sysfiles
                 |        |
                 |        +-- cBackups
                 |        |     +-- files
                 |        :
                 |        +-- (other subdirectories)                 
                 +-- (other TrueNAS datasets)
                 |        :
                 :        :

Because they were setup before the conversion to Scale, the owner and groups for /mnt, /mnt/Home, /mnt/Home/sysfiles, /mnt/Home/sysfiles/cBackups and /mnt/Home/sysfiles/cBackups/files are ‘root:root’.

The script is located in /mnt/Home/sysfiles/ and is scheduled via the GUI using cron with the user ‘root’.

Step one works fine. The copying of the file: /data/freenas-v1.db to /mnt/Home/sysfiles/cBackups works without any issues. Copy command:
cp /data/freenas-v1.db /mnt/Home/sysfiles/cBackups/files/config.db

Step two works fine. The validation of the integrity of the copied file using sqlite3 works without any issues.Validation command:
sqlite3 "/mnt/Home/sysfiles/cBackups/files/config.db" "pragma integrity_check;"

But step three fails. The command is:
tar -czvf "config.tar.gz" -C "/mnt/Home/sysfiles/cBackups" "freenas-v1.db"
The error is:

config.tar.gz Cannot open. Read-only file system
tar(child):Error is not recoverable exiting now

which is interesting because the copy just worked!

Background:
When I upgraded the OS I also moved the pools to new drives. I exported and imported them. At that time the pools were set to “Read Only” but with the help of this group I was able to reset them and have been happily reading and writing to all my pools with no issues… except this one.

I hope I have given enough information here, but if not, ask me and I’ll provide it.
Thanks so much in advance for your help.

Try to run your script like this:

cd /mnt/Home/sysfiles && ./script_name.sh

Edit: Look at the last link in my signature. It emails you what you backing up.

1 Like

Provided that IMHO too you can rely on the script suggested by Joe, is also worth to mention that on EEL old sendemail has been removed (and you are probably using it), so it explain why the email is not working.
Instead there is the midclt mail.send (but is quite trivial to use with attachments, some example in this long thread), or grab from my repositories the sendemail that i have built in python

The switch -C /to/path should usually be first in the line. It will also only change the directory for the tar command and not the output -f foo.tar so that probably failed as a cron job will be run from / by default and tried to create /config.tar.gz

You can do this with just one command and using .backup is considered to be a safer option than cp.

sqlite3 -readonly -cmd "pragma integrity_check" /data/freenas-v1.db ".backup $HOME/freenas-v1.$(date -I).db"

so i would probably do something like this

#!/usr/bin/env
set -e
BACKUPDIR=/mnt/p0/tmp
sqlite3 -readonly -cmd "pragma integrity_check" /data/freenas-v1.db ".backup $BACKUPDIR/freenas-v1.$(date -I).db"
tar -czf $BACKUPDIR/config.tgz -C $BACKUPDIR freenas-v1.$(date -I).db
....
# whatever that mailing stuff needs ^_^ 

an alternative to .backup would be VACUUM INTO (which would clean up a bit but there isn’t much happening that database so not really worth it IMHO)

sqlite3 -readonly -echo -cmd "pragma integrity_check" /data/freenas-v1.db "VACUUM main INTO '$BACKUPDIR/freenas-v1.$(date -I).db'"

sqlite: How To Corrupt An SQLite Database File