Backup a SQLite database
Don't use the obsolete syntax `...`

Use the modern syntax $(...):

today=$(date "+%Y-%m-%d")


Simplify file deletion

Instead of deleting files with -exec rm {} \;, a simpler way is using -delete:

find "$BACKUP_DIR" -type f -mtime +30 -delete


Don't use the obsolete syntax head -N

head -1 is obsolete syntax. Use head -n 1 instead.

Don't repeat yourself

On these lines, $(ls -t "$BACKUP_DIR" | head -1) is repeated:


last_backup=$(ls -t "$BACKUP_DIR" | head -1)
if [ -n "$last_backup" ] &amp;&amp; diff "$BACKUP_FILE" "$BACKUP_DIR/$(ls -t "$BACKUP_DIR" | head -1)" &gt;/dev/null



The second use should be replaced with $last_backup.

Compare files using cmp

Instead of diff, it's more efficient to compare files using cmp:

if [ -n "$last_backup" ] &amp;&amp; cmp -s "$BACKUP_FILE" "$BACKUP_DIR/$last_backup"


Avoid empty statements

Instead of the empty then clause of the final condition,
it's better to invert it (using DeMorgan's law):

if [ ! "$last_backup" ] || ! cmp -s "$BACKUP_FILE" "$BACKUP_DIR/$last_backup"
then
        cp "$BACKUP_FILE" "$BACKUP_DIR/$today.sqlite3"
fi


Delay operations until really needed

The today variable is only used once at the end,
when certain conditions are fulfilled.
It would be better to create it right before it's needed.