Remember that the OP already reported a file locked problem with the RM database that might have been caused by Time Machine. There are no other obvious candidates for the file being locked than than Time Machine. This file lock problem apparently did not cause any corruption of the RM database.
I confess that I have never totally understood how backing up a live RM database could alone cause file corruption, no matter if it’s Dropbox or OneDrive or Time Machine or whatever. It has always seemed to me that any read operations of the database by the backup software should just be queued up and executed along with any update operations that are being issued by the SQLite app (which is RM in this case). But SQLite file update operations involve multiple writes for what the RM user sees as a single update. So I’m guessing that maybe some of the file updates succeed and then some of them encounter that the file is locked and therefore fail. Again, I don’t understand why the rest of the file update operations don’t simply wait until the file becomes unlocked. But there have been persistent reports of RM database corruption occurring in this situation.
But there are really two concerns with backing up a live SQLite database. Concern #1 is the possibility that the backup process will interfere with the SQLite app as it is trying to update the database and therefore corrupt the database immediately. That’s the one I’ve been talking about. Concern #2 is that there is no immediate corruption of the SQLite database, but that the backup files itself is corrupted because the SQLite file is being changed as it’s being read. That is a variation on the theme of you can’t wade in the same river twice because it’s a different river the second time. Well, you can’t even wade in the same river once, because the river is changing verily as you wade in it. Similarly, you really can’t backup a file that’s being changed as you back it up. This type of corruption would only matter if ever the backup file itself needed to be used to do a restore. Otherwise, the fact that the backup file itself is corrupted wouldn’t matter. It’s just that you would think you have a good backup file when you don’t.
As for me, I would never trust any RM backup file except the RMBACKUP file made by RM itself, or a copy of an RMTREE file that I made myself when RM was not running. I would never trust a copy of an RMTREE file made by Dropbox or OneDrive or Time Machine unless I knew for sure that the copy was made when RM was not running. I would prefer that Dropbox and OneDrive and TimeMachine recognize that SQLIte files were in use and not back them up while they were in use, and in turn I would prefer that Dropbox and OneDrive and TimeMachine would actually backup SQLite databases only when they are not in use. But that doesn’t seem to be the way it works.
The following is from ChatGPT. The Google AI provides essentially the same advice.
While Time Machine can back up SQLite database files, relying solely on it is not a safe or reliable method for live, actively changing databases. If Time Machine copies the database file while a write transaction is in progress, the backup will be corrupted and unusable.
The problem lies in the way Time Machine works. It performs file-level backups and cannot ensure a database is in a consistent state during the process. An active SQLite database consists of more than just the .sqlite
or .db
file; it also uses a journal file (or Write-Ahead Log) for atomic transactions. If Time Machine backs up these files at different instants, the backup will be inconsistent.
Safe strategies for Time Machine backups
To safely back up SQLite databases on a Mac with Time Machine, you must first create a consistent copy of the database and then allow Time Machine to back up that copy.
- Use the
.backup
command
This is the most reliable method for creating a consistent backup of a live database. The .backup
command in the sqlite3
command-line tool locks the database, copies its contents to a new file, and ensures the backup file is in a valid state.
Steps:
-
Open the Terminal app on your Mac.
-
Run the following command, replacing mydatabase.db
with the path to your database and mydatabase_backup.db
with the desired backup file path:
sqlite3 mydatabase.db ".backup 'mydatabase_backup.db'"
-
Store the backup file in a location that Time Machine automatically includes in its backups.
-
Consider writing a shell script to automate this process.
-
Close the database before a backup
If you can stop all applications writing to the database, you can make a safe file-level copy.
Steps:
-
Close any applications that might be accessing the database.
-
Initiate a Time Machine backup manually by clicking “Back Up Now” from the Time Machine menu in the menu bar.
-
Use the SQLite Backup API
For developers, the built-in SQLite Backup API provides a programmatic way to create a consistent copy of a database. It works by establishing a shared lock on the database and then copying the file. A custom script or application can use this API to create a consistent backup file for Time Machine to pick up.
Recommended workflow
For robust data integrity, a mixed approach is best:
- Schedule a daily or hourly script (using
cron
or a tool like launchd
) to use the sqlite3 .backup
command to create a consistent copy of your database.
- Save the backup copy in a dedicated folder that is part of your normal Time Machine backups.
- Exclude the active, live database file from Time Machine. If you know the exact path to your live database, you can prevent Time Machine from creating potentially corrupt file-level backups of it. Go to System Settings > General > Time Machine > Options, and add the live
.db
file to the exclusion list.
- Test your backups. Regularly test a restore from your backup files to confirm they are uncorrupted and contain the correct data.