You might find these shell script fragments helpful. If you call retry_backup_copy With arguments of the database to backup (weewx.sdb) and a destination file to backup to, the code will check to see if the cksum changed during the copy and also run an integrity check on the copy. If the cksum changed or the integrity check fails, the copy is retried.
make_backup_copy() {
db="$1"
tmp_db_copy="$2"
check_sum="`/usr/bin/cksum $db | cut -d ' ' -f 1`"
cp "$db" $tmp_db_copy
check_sum_after="`/usr/bin/cksum $db | cut -d ' ' -f 1`"
if [ "$check_sum" != "$check_sum_after" ]; then
echo "$db changed during cp!"
return 1
fi
integrity_check="`echo "pragma integrity_check;" | sqlite3 "$tmp_db_copy"`"
if [ "$integrity_check" != "ok" ]; then
echo "$tmp_db_copy failed integrity check!"
return 2
fi
return 0
}
retry_backup_copy() {
db="$1"
tmp_db_copy="$2"
retval=99
attempts=0
while [ "$retval" -ne 0 ]; do
attempts="`/usr/bin/expr $attempts + 1`"
if [ "$retval" -ne 99 ]; then
echo "make_backup_copy attempt $attempts"
fi
make_backup_copy "$db" "$tmp_db_copy"
retval="$?"
done
}
I have an archive_interval of 300s, the database is usually updated within the first 20s after each 5th minute. The cron jobs that backup my sdb files run at minute 1 of every hour/day/week. I've never came across any problems, which doesn't mean this way to work will work always that smoothly. But: having an hourly backup, a daily backup, and a weekly backup done this way, chances are, I'll find a working copy anywhere :)