3 ms·
Taking an LVM snapshot and then copying the sqlite database from that is sufficient to keep it from being corrupted, but you can have incomplete transactions th
by HumanOstrich 1y ago
Taking an LVM snapshot and then copying the sqlite database from that is sufficient to keep it from being corrupted, but you can have incomplete transactions that will be rolled back during crash recovery.
The problem is that LVM snapshots operate at the block device level and only ensure there are no torn or half-written blocks. It doesn't know about the filesystem's journal or metadata.
To get a consistent point-in-time snapshot without triggering crash-recovery and losing transactions, you also need to lock the sqlite database or filesystem from writes during the snapshot.
PRAGMA wal_checkpoint(FULL);
BEGIN IMMEDIATE; -- locks out writers
. /* trigger your LVM snapshot here */
COMMIT;
You can also use fsfreeze to get the same level of safety:
sudo fsfreeze -f /mnt/data # (A) flush dirty pages & block writes
lvcreate -L1G -s -n snap0 /dev/vg0/data
sudo fsfreeze -u /mnt/data # (B) thaw, resume writes
Bonus - validate the snapshotted db file with:
sqlite3 mydb-snapshot.sqlite "PRAGMA integrity_check;"
- remram 1y agoWhat does "losing transactions" mean? Some transactions will have committed before your backup and some will have committed after and therefore won't be included. I don't see the problem you are trying to solve? Whether a transaction had started and gets transparently rolled back, or you had prevented from starting, what is the difference to you? Either way, you have a point-in-time snapshot, that time is the latest commit before the LVM snapshot. You're discussing this in terms of "safety" and that doesn't seem right to me.
- HumanOstrich 1y agoThis isn't really a personal or controversial take on the issue. There are easier ways to back up a sqlite database, but if you want to use LVM snapshots you need to understand how to do it correctly if you want to have useful backups. Here's a scenario for a note-taking app backed by sqlite: 1. User A is editing a note and the app writes the changes to the database. 2. SQLite is in WAL mode, meaning changes go to a -wal file first. 3. You take the LVM snapshot while SQLite is: - midway through a write (to the WAL or the main db file) - or the WAL hasn’t been checkpointed back into the main DB file 4. The snapshot includes: - a partial write to notes.db - or a notes.db and notes.db-wal that are out of sync Result: The backup is inconsistent. Restoring this snapshot later might: - Cause sqlite3 to throw errors like database disk image is malformed - Lose recent edits - Require manual recovery or loss of WAL contents In order to get a consistent, point-in-time recovery where your database is left in state A and your backup from the LVM snapshot is in state B with _no_ intermediate states (like rolled back transactions or partial writes to the db file), you have to first either: - Tell SQLite to create checkpoint (write the WAL contents to the main DB) and suspend writes - Or, flush and block all writes to the filesystem using fsfreeze Then take the LVM snapshot.
- remram 1y agoYou are wrong, the whole point of the WAL is to make SQLite crash-consistent. Same as the rollback journal. SQLite will safely rollback partial writes. I don't know where you got the idea that in WAL mode, SQLite ditches its consistency guarantees somehow. If you can corrupt a SQLite database by pulling the power or simulating it by taking a block-device snapshot, this is a serious SQLite bug and you should report it. https://sqlite.org/transactional.html https://sqlite.org/transactional.html & https://sqlite.org/atomiccommit.html https://sqlite.org/atomiccommit.html have more details
- HumanOstrich 1y agoIf you want to keep being aggressively ignorant, you can go argue with ChatGPT about it. I'm not wasting any more effort on you.
- dathery 1y agoSorry but your point is just completely wrong and I am not sure why you have this belief. It is extremely normal and safe to backup SQLite via block device or filesystem snapshots. Are you under the impression that SQLite cannot recover from a power loss...? The whole point of using a log is that you can recover from crashes mid-way through updating the database file by replaying the writes from the log. If it will convince you, I went and asked ChatGPT like you recommended and it agrees: > If you want to use snapshots: > Use a filesystem or block-level snapshot tool that guarantees a point-in-time, atomic snapshot (e.g., LVM snapshots, ZFS snapshots, Btrfs snapshots, or VSS on Windows). If you were going to "Tell SQLite to create checkpoint (write the WAL contents to the main DB) and suspend writes" as you suggest is necessary, why even bother with a snapshot at that point?
- remram 1y agoI'm sorry you took "you're wrong" as aggressive. I am providing sources though and other users are agreeing with me, will you please look into it? Or ask ChatGPT if SQLite is crash-consistent? Please consider the possibility you are spreading misinformation...