Skip to content

Instantly share code, notes, and snippets.

@coderdiaz
Last active November 9, 2016 13:27
Show Gist options
  • Save coderdiaz/e6c2821ee51e742ae531 to your computer and use it in GitHub Desktop.
Save coderdiaz/e6c2821ee51e742ae531 to your computer and use it in GitHub Desktop.
How to backup Database with Ionic Framework
/*
* Implementation with Ionic Framework with $cordovaFile plugin
* ngCordova.
*/
.controller('Ctrl', function($scope, $cordovaFile) {
$scope.backupDatabase = function() {
// Backup Database ;
var URI_DB = cordova.file.applicationStorageDirectory+'databases/';
var PARENT_DIR = "file://mnt/sdcard/"
var NAME_DB ="project_database.db";
var NEW_NAME_BACKUP = "backup.db";
window.resolveLocalFileSystemURL(URI_DB+NAME_DB, function(fs) {
window.resolveLocalFileSystemURL(PARENT_DIR, function(directoryEntry){
fs.copyTo(directoryEntry, NEW_NAME_BACKUP, function() {
console.log("The database backup was successful.");
});
});
});
};
});
/*
* Example on Cordova 3.5 in PhoneGap
* with org.cordova.file Plugin
*/
window.resolveLocalFileSystemURL("file:///data/data/my-app-name/databases/name-of.db", function(fs) {
var parent = "file://mnt/external_sd/";
var newName = "mybackup.db";
window.resolveLocalFileSystemURL(parent, function(directoryEntry) {
fs.copyTo(directoryEntry, newName, function() {
alert("Backup ok");
}, failFiles);
});
}, failFiles);
function failFiles(error) {
if (error.code == FileError.NOT_FOUND_ERR) alert("Message : NOT_FOUND_ERR" )
else if (error.code == FileError.SECURITY_ERR) alert("Message : SECURITY_ERR" )
else if (error.code == FileError.ABORT_ERR) alert("Message : ABORT_ERR" )
else if (error.code == FileError.NOT_READABLE_ERR) alert("Message : NOT_READABLE_ERR" )
else if (error.code == FileError.ENCODING_ERR) alert("Message : ENCODING_ERR" )
else if (error.code == FileError.NO_MODIFICATION_ALLOWED_ERR) alert("Message : NO_MODIFICATION_ALLOWED_ERR" )
else if (error.code == FileError.INVALID_STATE_ERR) alert("Message : INVALID_STATE_ERR" )
else if (error.code == FileError.SYNTAX_ERR) alert("Message : SYNTAX_ERR" )
else if (error.code == FileError.INVALID_MODIFICATION_ERR) alert("Message : INVALID_MODIFICATION_ERR" )
else if (error.code == FileError.QUOTA_EXCEEDED_ERR) alert("Message : QUOTA_EXCEEDED_ERR" )
else if (error.code == FileError.PATH_EXISTS_ERR) alert("Message : PATH_EXISTS_ERR" )
}
@XimenaAlejandro
Copy link

thanks for the code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment