IOS: How to back up my main database? And how to export / import this copy?

I want to offer users of my application the ability to back up the main database, especially if it switches to a new device, etc.

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

thanks!

+8
ios core-data backup
source share
3 answers

Take a look at this sample application, it includes functions for creating backups, copying backups to and from iCloud, sending email 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 is much safer to use the migratePersistentStore API to create / import backups if you do this in and out of iCloud. Also keep in mind that the sample application assumes that you are not using WAL mode, which is the default mode for iOS 7. WAL mode uses several files, all of which must be copied or copied.

Here is a link to a video demonstrating examples of application 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 backup copies. Please note that you can open a repository with several persistentStoreCoordinators, so you do not need to close it when creating a backup. Its restoration requires, first of all, the removal of the existing storage. Please note that there is a slight difference between the two methods, except that the source repository opens 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; } 
+5
source share

No matter what persistent storage you use (binary, SQLite, etc.); it is just a file in the file system. You can make a copy of it whenever you want.

If you use SQLite in iOS 7, be sure to create a copy of the other related files, as they are the log files that come with it. If you are using a binary, then there will be only one file.

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

There are more advanced projects, such as exporting the entire database to a portable one, such as JSON, but that's another question.

Update

Well, I used the standard basic Xcode data pattern, so according to the code I just checked, I use SQLite. So how do I find all related files? Or can you show me sample code how to copy and paste the necessary files back?

You are using NSFileManager to copy files. You can view the document catalog in the iOS simulator app to view the names of all files. Or you can use NSFileManager to scan a document directory, find everything that starts with the same file name (for example, MyData.* ) And copy it to the backup directory.

Regarding the sample code, no; it's just a couple lines of code as soon as you look at the documentation for NSFileManager .

+1
source share

I created the following method using sample Apple code . This will back up the master data files and put it in the path you need.

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)") } } 
0
source share

All Articles