use of org.exist.xmldb.EXistRestoreService in project exist by eXist-db.
the class RestoreTask method execute.
@Override
public void execute() throws BuildException {
if (uri == null) {
throw (new BuildException("You have to specify an XMLDB collection URI"));
}
if ((dir == null) && (dirSet == null) && (zipFile == null)) {
throw (new BuildException("Missing required argument: either dir, dirset or file required"));
}
if ((dir != null) && !Files.isReadable(dir)) {
final String msg = "Cannot read restore file: " + dir.toAbsolutePath().toString();
if (failonerror) {
throw (new BuildException(msg));
} else {
log(msg, Project.MSG_ERR);
}
} else {
registerDatabase();
try {
if (dir != null) {
log("Restoring from " + dir.toAbsolutePath().toString(), Project.MSG_INFO);
final Path file = dir.resolve("__contents__.xml");
if (!Files.exists(file)) {
final String msg = "Could not find file " + file.toAbsolutePath().toString();
if (failonerror) {
throw (new BuildException(msg));
} else {
log(msg, Project.MSG_ERR);
}
} else {
final RestoreServiceTaskListener listener = new ConsoleRestoreServiceTaskListener();
final Collection collection = DatabaseManager.getCollection(uri, user, password);
final EXistRestoreService service = (EXistRestoreService) collection.getService("RestoreService", "1.0");
service.restore(file.normalize().toAbsolutePath().toString(), restorePassword, listener, overwriteApps);
}
} else if (dirSet != null) {
final DirectoryScanner scanner = dirSet.getDirectoryScanner(getProject());
scanner.scan();
final String[] includedFiles = scanner.getIncludedFiles();
log("Found " + includedFiles.length + " files.\n");
for (final String included : includedFiles) {
dir = scanner.getBasedir().toPath().resolve(included);
final Path contentsFile = dir.resolve("__contents__.xml");
if (!Files.exists(contentsFile)) {
final String msg = "Did not found file " + contentsFile.toAbsolutePath().toString();
if (failonerror) {
throw (new BuildException(msg));
} else {
log(msg, Project.MSG_ERR);
}
} else {
log("Restoring from " + contentsFile.toAbsolutePath().toString() + " ...\n");
// TODO subdirectories as sub-collections?
final RestoreServiceTaskListener listener = new ConsoleRestoreServiceTaskListener();
final Collection collection = DatabaseManager.getCollection(uri, user, password);
final EXistRestoreService service = (EXistRestoreService) collection.getService("RestoreService", "1.0");
service.restore(contentsFile.normalize().toAbsolutePath().toString(), restorePassword, listener, overwriteApps);
}
}
} else if (zipFile != null) {
log("Restoring from " + zipFile.toAbsolutePath().toString(), Project.MSG_INFO);
if (!Files.exists(zipFile)) {
final String msg = "File not found: " + zipFile.toAbsolutePath().toString();
if (failonerror) {
throw (new BuildException(msg));
} else {
log(msg, Project.MSG_ERR);
}
} else {
final RestoreServiceTaskListener listener = new ConsoleRestoreServiceTaskListener();
final Collection collection = DatabaseManager.getCollection(uri, user, password);
final EXistRestoreService service = (EXistRestoreService) collection.getService("RestoreService", "1.0");
service.restore(zipFile.normalize().toAbsolutePath().toString(), restorePassword, listener, overwriteApps);
}
}
} catch (final Exception e) {
e.printStackTrace();
final String msg = "Exception during restore: " + e.getMessage();
if (failonerror) {
throw new BuildException(msg, e);
} else {
log(msg, e, Project.MSG_ERR);
}
}
}
}
use of org.exist.xmldb.EXistRestoreService in project exist by eXist-db.
the class XMLDBBackupTest method restore.
private void restore(final Path backupFile, final XmldbURI collectionUri) throws XMLDBException, SAXException, URISyntaxException, ParserConfigurationException, IOException {
final Collection collection = DatabaseManager.getCollection(collectionUri.toString(), TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
final EXistRestoreService restoreService = (EXistRestoreService) collection.getService("RestoreService", "1.0");
final TestRestoreListener listener = new TestRestoreListener();
restoreService.restore(backupFile.normalize().toAbsolutePath().toString(), null, listener, false);
}
use of org.exist.xmldb.EXistRestoreService in project exist by eXist-db.
the class BackupRestoreSecurityPrincipalsTest method restoreConflictingUsername.
/**
* 1. With an empty database we create three
* users: 'frank', 'joe', and 'jack'.
*
* 2. We create a backup of the database which contains
* the three users from (1).
*
* 3. We destroy the database, restart the server,
* and start again with a clean database.
*
* 4. With an (again) empty database we create two
* users: 'frank', and 'jack'.
*
* 5. We then try and restore the database backup from (2), which
* contains the original 'frank', 'joe', and 'jack' users.
*
* frank will have the same username and user id in the current
* database and the backup we are trying to restore.
*
* joe does not exist in the current database, but his user id
* in the backup will collide with that of jack in the current database.
*
* jack will have a different user id in the backup when compared to the current
* database, however he will have the same username.
*
* We want to make sure that after the restore, all three users are present
* that they have distinct and expected user ids and that any resources
* that were owned by them are still correctly owner by them (and not some other user).
*/
@Test
public void restoreConflictingUsername() throws PermissionDeniedException, EXistException, SAXException, ParserConfigurationException, IOException, URISyntaxException, XMLDBException, IllegalAccessException, ClassNotFoundException, InstantiationException {
// creates a database with new users: 'frank(id=11)', 'joe(id=12)', and 'jack(id=13)'
createInitialUsers(FRANK_USER, JOE_USER, JACK_USER);
// create a backup of the database (which has the initial users)
final Path backupFile = backupDatabase();
// reset database to empty
server.restart(true);
// create new users: 'frank(id=11)' and 'jack(id=12)'
createInitialUsers(FRANK_USER, JACK_USER);
final String accountQuery = "declare namespace c = 'http://exist-db.org/Configuration';\n" + "for $account in //c:account\n" + "return\n" + "<user id='{$account/@id}' name='{$account/c:name}'/>";
final XPathQueryService xqs = (XPathQueryService) server.getRoot().getService("XPathQueryService", "1.0");
final SecurityManagerImpl sm = (SecurityManagerImpl) BrokerPool.getInstance().getSecurityManager();
// check the current user accounts
ResourceSet result = xqs.query(accountQuery);
assertUser(RealmImpl.ADMIN_ACCOUNT_ID, SecurityManager.DBA_USER, ((XMLResource) result.getResource(0)).getContentAsDOM());
assertUser(RealmImpl.GUEST_ACCOUNT_ID, SecurityManager.GUEST_USER, ((XMLResource) result.getResource(1)).getContentAsDOM());
assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 1, "frank", ((XMLResource) result.getResource(2)).getContentAsDOM());
assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 2, "jack", ((XMLResource) result.getResource(3)).getContentAsDOM());
// check the last user id
// last account id should be that of 'jack'
assertEquals(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 2, sm.getLastAccountId());
// create a test collection and give everyone access
final CollectionManagementService cms = (CollectionManagementService) server.getRoot().getService("CollectionManagementService", "1.0");
final Collection test = cms.createCollection("test");
final UserManagementService testUms = (UserManagementService) test.getService("UserManagementService", "1.0");
testUms.chmod("rwxrwxrwx");
// create and store a new document as 'frank'
final Collection frankTest = DatabaseManager.getCollection("xmldb:exist:///db/test", FRANK_USER, FRANK_USER);
final String FRANKS_DOCUMENT = "franks-document.xml";
final Resource frankDoc = frankTest.createResource(FRANKS_DOCUMENT, XMLResource.RESOURCE_TYPE);
frankDoc.setContent("<hello>frank</hello>");
frankTest.storeResource(frankDoc);
// create and store a new document as 'jack'
final Collection jackTest = DatabaseManager.getCollection("xmldb:exist:///db/test", JACK_USER, JACK_USER);
final String JACKS_DOCUMENT = "jacks-document.xml";
final Resource jackDoc = jackTest.createResource(JACKS_DOCUMENT, XMLResource.RESOURCE_TYPE);
jackDoc.setContent("<hello>jack</hello>");
jackTest.storeResource(jackDoc);
// restore the database backup
final EXistRestoreService service = (EXistRestoreService) server.getRoot().getService("RestoreService", "1.0");
service.restore(backupFile.normalize().toAbsolutePath().toString(), null, new NullRestoreServiceTaskListener(), false);
// check the current user accounts after the restore
result = xqs.query(accountQuery);
assertUser(RealmImpl.ADMIN_ACCOUNT_ID, SecurityManager.DBA_USER, ((XMLResource) result.getResource(0)).getContentAsDOM());
assertUser(RealmImpl.GUEST_ACCOUNT_ID, SecurityManager.GUEST_USER, ((XMLResource) result.getResource(1)).getContentAsDOM());
assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 1, FRANK_USER, ((XMLResource) result.getResource(2)).getContentAsDOM());
assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 2, JACK_USER, ((XMLResource) result.getResource(3)).getContentAsDOM());
assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 3, JOE_USER, ((XMLResource) result.getResource(4)).getContentAsDOM());
// check the last user id after the restore
// last account id should be that of 'joe'
assertEquals(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 3, sm.getLastAccountId());
// check the owner of frank's document after restore
final Resource fDoc = test.getResource(FRANKS_DOCUMENT);
final Permission franksDocPermissions = testUms.getPermissions(fDoc);
assertEquals(FRANK_USER, franksDocPermissions.getOwner().getName());
// check the owner of jack's document after restore
final Resource jDoc = test.getResource(JACKS_DOCUMENT);
final Permission jacksDocPermissions = testUms.getPermissions(jDoc);
assertEquals(JACK_USER, jacksDocPermissions.getOwner().getName());
}
Aggregations