iOS: How can I create a backup copy of my core data base? And how to export/import that copy?

StackOverflow https://stackoverflow.com/questions/22151650

  •  19-10-2022
  •  | 
  •  

Question

I want to offer the users of my app the possibility to create a backup of the core data database, especially in case he switches to a new device etc.

How would I do that? Especially how can I reimport that file? I mean let's say he makes a backup copy of the database, then changes a ton of stuff and wants to reset to the previous saved backup copy. How would I do that?

Thx!

No correct solution

OTHER TIPS

Take a look at this sample app, it includes functions for making backups, copying backups to and from iCloud, emailing backups and importing backups from email. http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/

BTW it's much safer to use migratePersistentStore API to make/import backups if your are doing so to and from ICloud. Also be aware that the sample app assumes you are not using WAL mode which is the default mode for iOS 7. WAL mode uses multiple files which all need to be backed up or copied.

Here is a link to a video demonstrating the sample Apps backup and restore capabilities.

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

Here are the methods used to create copies for backup. Note that it is possible to open the store with multiple persistentStoreCoordinators so no need to close it down while you make a backup. Restoring it does obviously require the existing store to be removed first. Note that there is little difference between the two methods below except that the source store is opened with or without iCloud options.

    /*! Creates a backup of the ICloud store

     @return Returns YES of file was migrated or NO if not.
     */
    - (bool)backupICloudStore {
        FLOG(@"backupICloudStore called");


        // Lets use the existing PSC
        NSPersistentStoreCoordinator *migrationPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];

        // Open the store
        id sourceStore = [migrationPSC addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[self icloudStoreURL] options:[self icloudStoreOptions] error:nil];

        if (!sourceStore) {

            FLOG(@" failed to add old store");
            migrationPSC = nil;
            return FALSE;
        } else {
            FLOG(@" Successfully added store to migrate");

            NSError *error;

            FLOG(@" About to migrate the store...");
            id migrationSuccess = [migrationPSC migratePersistentStore:sourceStore toURL:[self backupStoreURL] options:[self localStoreOptions] withType:NSSQLiteStoreType error:&error];

            if (migrationSuccess) {
                FLOG(@"store successfully backed up");
                migrationPSC = nil;
                // Now reset the backup preference
                [[NSUserDefaults standardUserDefaults] setBool:NO forKey:_makeBackupPreferenceKey];
                [[NSUserDefaults standardUserDefaults] synchronize];
                return TRUE;
            }
            else {
                FLOG(@"Failed to backup store: %@, %@", error, error.userInfo);
                migrationPSC = nil;
                return FALSE;
            }

        }
        migrationPSC = nil;
        return FALSE;
    }
    /*! Creates a backup of the Local store

     @return Returns YES of file was migrated or NO if not.
     */
    - (bool)backupLocalStore {
        FLOG(@"backupLocalStore called");


        // Lets use the existing PSC
        NSPersistentStoreCoordinator *migrationPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];

        // Open the store
        id sourceStore = [migrationPSC addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[self localStoreURL] options:[self localStoreOptions] error:nil];

        if (!sourceStore) {

            FLOG(@" failed to add old store");
            migrationPSC = nil;
            return FALSE;
        } else {
            FLOG(@" Successfully added store to migrate");

            NSError *error;

            FLOG(@" About to migrate the store...");
            id migrationSuccess = [migrationPSC migratePersistentStore:sourceStore toURL:[self backupStoreURL] options:[self localStoreOptions] withType:NSSQLiteStoreType error:&error];

            if (migrationSuccess) {
                FLOG(@"store successfully backed up");
                migrationPSC = nil;
                // Now reset the backup preference
                [[NSUserDefaults standardUserDefaults] setBool:NO forKey:_makeBackupPreferenceKey];
                [[NSUserDefaults standardUserDefaults] synchronize];
                return TRUE;
            }
            else {
                FLOG(@"Failed to backup store: %@, %@", error, error.userInfo);
                migrationPSC = nil;
                return FALSE;
            }

        }
        migrationPSC = nil;
        return FALSE;
    }

/**  Sets the selected file as the current store.
 Creates a backup of the current store first.

 @param fileURL The URL for the file to use.
 */
- (BOOL)restoreFile:(NSURL *)fileURL {
    FLOG(@" called");

    // Check if we are using iCloud
    if (_isCloudEnabled) {
        FLOG(@" using iCloud store so OK to restore");
        NSURL *currentURL = [self storeURL];
        FLOG(@" currentURL is %@", currentURL);

        FLOG(@" URL to use is %@", fileURL);

        [self saveContext];

        [self backupCurrentStoreWithNoCheck];

        // Close the current store and delete it
        _persistentStoreCoordinator = nil;
        _managedObjectContext = nil;

        [self removeICloudStore];

        [self moveStoreFileToICloud:fileURL delete:NO backup:NO];


    } else {
        FLOG(@" using local store so OK to restore");
        NSURL *currentURL = [self storeURL];
        FLOG(@" currentURL is %@", currentURL);

        FLOG(@" URL to use is %@", fileURL);

        [self saveContext];

        [self backupCurrentStoreWithNoCheck];

        // Close the current store and delete it
        _persistentStoreCoordinator = nil;
        _managedObjectContext = nil;

        NSError *error = nil;
        NSFileManager *fm = [[NSFileManager alloc] init];

        // Delete the current store file
        if ([fm fileExistsAtPath:[currentURL path]]) {
            FLOG(@" target file exists");
            if (![fm removeItemAtURL:currentURL error:&error]) {
                FLOG(@" error unable to remove current store file");
                NSLog(@"Error removing item Error: %@, %@", error, error.userInfo);
                return FALSE;
            } else {
                FLOG(@" current store file removed");
            }
        }

        //
        //simply copy the file over
        BOOL copySuccess = [fm copyItemAtPath:[fileURL path]
                                       toPath:[currentURL path]
                                        error:&error];
        if (copySuccess) {
            FLOG(@" replaced current store file successfully");
            //[self postFileUpdateNotification];
        } else {
            FLOG(@"Error copying items Error: %@, %@", error, error.userInfo);
            return FALSE;
        }
    }

    // Now open the store again

    [self openPersistentStore];

    return TRUE;
}

Whatever the persistent store is that you use (binary, SQLite, etc.); it is just a file on the filesystem. You can make a copy of it whenever you want.

If you are using SQLite in iOS 7, be sure to make a copy of the other files associated with it as they are the journal files that go with it. If you are using binary then there will be only a single file.

If you just copy the file there is no import step, you just copy it back to restore it.

There are more advanced designs such as exporting the entire database to something that is portable, such as JSON but that is a different subject.

Update

Well I've used the standard Xcode core data template, so according to the code I've just checked I'm using SQLite. So how do I find all related files? Or could you show me with some example code how to copy and insert back the files needed?

You use NSFileManager to copy the files. You can look at the documents directory in your iOS simulator application to see the names of all the files. Or you could use NSFileManager to scan the documents directory, find everything that starts with the same file name (MyData.* for example) and copy that into a back up directory.

As for sample code, no; it is only a couple of lines of code once you look at the documentation for NSFileManager.

I created the following method with the help of Apple sample code. This will take a backup of core data files and place it to the path that you want.

Swift 5

/// Backing up store type to a new and unique location
/// The method is illustrated in the following code fragment, which shows how you can use migratePersistentStore to take a back up of a store and save it from one location to another.
/// If the old store type is XML, the example also converts the store to SQLite.
/// - Parameters:
///   - path: Where you want the backup to be done, please create a new unique directory with timestamp or the guid
///   - completion: Passes error in case of error or pass nil in case of success
class func backUpCoreDataFiles(path : URL, completion : @escaping (_ error : String?) -> ())
{

    // Every time new container is a must as migratePersistentStore method will loose the reference to the container on migration
    let container = NSPersistentContainer(name : "<YourDataModelName>")
    container.loadPersistentStores
        { (storeDescription, error) in
            if let error = error
            {
                fatalError("Failed to load store: \(error)")
            }
    }
    let coordinator = container.persistentStoreCoordinator
    let store = coordinator.persistentStores[0]
    do
    {
        try coordinator.migratePersistentStore(store, to : path, options : nil, withType : NSSQLiteStoreType)
        completion(nil)
    }
    catch
    {
        completion("\(Errors.coredataBackupError)\(error.localizedDescription)")
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top