Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 579
MichiZH Avatar asked Mar 03 '14 16:03

MichiZH


1 Answers

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;
}
like image 149
Duncan Groenewald Avatar answered Oct 16 '22 16:10

Duncan Groenewald