write db to temp file, rename to final name at end