Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

>Do not use `cp` to back up SQLite databases. It is not transactionally safe.

I've read that this is true, but it has always confused me because I would expect that using cp would be equivalent to if the application had crashed / you lost power.



`cp` is not instantaneous. Unless you lock the entire file, one block can change while you're reading another block. You end up with a Frankenstein of a file, compared with a consistent snapshot if you had simply lost power.

If your filesystem supports snapshotting, it would be safe to create a snapshot and `cp` the database off of that snapshot.


No, because cp takes a non-zero amount of time and so by the time it has completed the first blocks may have been rewritten, thus the backup is inconsistent. If you take something like a ZFS snapshot, then, yes, that would be a consistent backup.


Would cp --reflink=always work too?


Not an expert but the transaction log is in another file and you’d need both to be in line with each other and backed up, so perhaps that’s a factor


I wrote up a bit longer explanation of the failure modes (which depend on your journaling mode) in this comment: https://news.ycombinator.com/item?id=31387447




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: