1
votes

I would like to make backup copies of my app's main sqlite DB while my app is running.

1) I have read that it is safe to just copy the sqlite file if the DB has been checkpointed (at that point the wal file contains no important data). Does [managedContext save:] do that checkpointing, or is there something else I have to do? (ref -shm and -wal files in SQLite DB)

2) Is there any way, short of tearing down the whole core data stack, to be sure that core data doesn't try to write to the sqlite file while I'm copying it? My app does save frequently after any user input, and it would be nice if there was some way to force that to block for a second.

2
While you're at it, can't you just implement replication? I could use that, too. :-) - ott--
Apparently there is something like a live backup API in the sqlite C library: sqlite.org/backup.html Has anyone used it with Core Data? - c roald
Actually, I suppose I could wrap @synchronized directives around the -save: and -backup: calls in my singleton DB handler. If I'm understanding this right, that should buy me a fraction of a second of blocking while the sqlite file is being copied, without having to mess around too much with already-debugged code. - c roald

2 Answers

1
votes

I have uploaded a sample app that provides backup and restore capabilities a number of different ways, including local backups, copy backups to and from iCloud, email backups, import from email, and file copy via iTunes. See link below for video demonstrating these capabilities and you can download the sample apps from the site.

http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/sample-apps-explanations/backup-files/

EDIT

It should be safe to create a new persistentStoreCoordinator with the same fileURL and to then use migratePersistentStore API without closing the App, save the main MOC first though. I always use JOURNAL=DELETE mode to ensure I just have to use a single file to deal with. If you are using WAL mode then you would need to backup all three files used by sqlite.

0
votes

Regarding 2) When I make backup, I close all mom's, moc's and persistent store. Then it's safe to make backup or restore. Also all views are waiting for events to release all coredata resources and bring them back when database is available again. It's good to have a singleton to manage coredata.