Database Backup #288

Closed
opened 2025-12-29 15:28:13 +01:00 by adam · 2 comments
Owner

Originally created by @OlionCompany on GitHub (Aug 19, 2019).

How we can create fully database copy with all last changes?
Right now, if we copy database from SQLiteStore.fileURL, this file don't include "-shm" and "-wal" files with last changes (certainly).

I found solution for Core Data:
persistentStore.migratePersistentStore(oldStore, to: newStoreURL, options: nil, withType: NSSQLiteStoreType)

Can we create backup using CoreStore? Thanks.

Originally created by @OlionCompany on GitHub (Aug 19, 2019). How we can create fully database copy with all last changes? Right now, if we copy database from SQLiteStore.fileURL, this file don't include "-shm" and "-wal" files with last changes (certainly). I found solution for Core Data: persistentStore.migratePersistentStore(oldStore, to: newStoreURL, options: nil, withType: NSSQLiteStoreType) Can we create backup using CoreStore? Thanks.
adam closed this issue 2025-12-29 15:28:13 +01:00
Author
Owner

@OlionCompany commented on GitHub (Aug 26, 2019):

Maybe someone has ideas?
Question still actual.

@OlionCompany commented on GitHub (Aug 26, 2019): Maybe someone has ideas? Question still actual.
Author
Owner

@OlionCompany commented on GitHub (Sep 3, 2019):

Our custom implementation of backups creation:

    let storage: SQLiteStore
    let dataStack: DataStack
​
    public func createBackup() throws {
        try withExtendedLifetime(NSPersistentStoreCoordinator(managedObjectModel: self.dataStack.modelSchema.rawModel())) { coordinator in
            let storeType = type(of: self.storage).storeType
            let store = try coordinator.addPersistentStore(ofType: storeType,
                                                           configurationName: self.storage.configuration,
                                                           at: self.storage.fileURL,
                                                           options: self.storage.dictionary(forOptions: .allowSynchronousLightweightMigration))
​
            let backupUrl = CoreDataStack.defaultBackupsDirectory.url.appendingPathComponent(Date().timeIntervalSince1970.description).appendingPathExtension(CoreDataStack.storeExtension)
​
            var backupStoreOptions = self.storage.storeOptions ?? [:]
            backupStoreOptions[NSSQLitePragmasOption] = ["journal_mode": "DELETE"]
​
            try coordinator.migratePersistentStore(store,
                                                   to: backupUrl,
                                                   options: backupStoreOptions, withType: storeType)
​
             _ = try? FileManager.default.removeItem(atPath: "\(backupUrl.path)-shm")
        }
    }`
@OlionCompany commented on GitHub (Sep 3, 2019): Our custom implementation of backups creation: ``` let storage: SQLiteStore let dataStack: DataStack ​ public func createBackup() throws { try withExtendedLifetime(NSPersistentStoreCoordinator(managedObjectModel: self.dataStack.modelSchema.rawModel())) { coordinator in let storeType = type(of: self.storage).storeType let store = try coordinator.addPersistentStore(ofType: storeType, configurationName: self.storage.configuration, at: self.storage.fileURL, options: self.storage.dictionary(forOptions: .allowSynchronousLightweightMigration)) ​ let backupUrl = CoreDataStack.defaultBackupsDirectory.url.appendingPathComponent(Date().timeIntervalSince1970.description).appendingPathExtension(CoreDataStack.storeExtension) ​ var backupStoreOptions = self.storage.storeOptions ?? [:] backupStoreOptions[NSSQLitePragmasOption] = ["journal_mode": "DELETE"] ​ try coordinator.migratePersistentStore(store, to: backupUrl, options: backupStoreOptions, withType: storeType) ​ _ = try? FileManager.default.removeItem(atPath: "\(backupUrl.path)-shm") } }` ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#288