Search in sources :

Example 1 with RestoreListener

use of org.exist.backup.restore.listener.RestoreListener in project exist by eXist-db.

the class SystemExportFiltersTest method exportImport.

@Test
public void exportImport() throws Exception {
    Path file;
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        List<String> filters = new ArrayList<>();
        filters.add(FilterForBackup.class.getName());
        broker.getConfiguration().setProperty(SystemExport.CONFIG_FILTERS, filters);
        final Collection test = broker.getCollection(TEST_COLLECTION_URI);
        assertNotNull(test);
        boolean direct = true;
        final SystemExport sysexport = new SystemExport(broker, transaction, null, null, direct);
        final Path backupDir = tempFolder.newFolder().toPath();
        file = sysexport.export(backupDir.toAbsolutePath().toString(), false, false, null);
        transaction.commit();
    }
    TestUtils.cleanupDB();
    final SystemImport restore = new SystemImport(pool);
    final RestoreListener listener = new LogRestoreListener();
    restore.restore(TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD, null, file, listener);
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        final Collection test = broker.getCollection(TEST_COLLECTION_URI);
        assertNotNull(test);
        DocumentImpl doc = getDoc(broker, test, doc01uri.lastSegment());
        assertEquals(XML1_BACKUP, serializer(broker, doc));
        doc = getDoc(broker, test, doc02uri.lastSegment());
        assertEquals(XML2_PROPER, serializer(broker, doc));
        doc = getDoc(broker, test, doc03uri.lastSegment());
        assertEquals(XML3_PROPER, serializer(broker, doc));
        doc = getDoc(broker, test, doc11uri.lastSegment());
        assertTrue(doc instanceof BinaryDocument);
        try (final InputStream is = broker.getBinaryResource(transaction, ((BinaryDocument) doc))) {
            assertEquals(BINARY, InputStreamUtil.readString(is, UTF_8));
        }
        transaction.commit();
    }
}
Also used : Path(java.nio.file.Path) LogRestoreListener(org.exist.backup.restore.listener.LogRestoreListener) InputStream(java.io.InputStream) Txn(org.exist.storage.txn.Txn) DocumentImpl(org.exist.dom.persistent.DocumentImpl) BinaryDocument(org.exist.dom.persistent.BinaryDocument) DBBroker(org.exist.storage.DBBroker) Collection(org.exist.collections.Collection) LogRestoreListener(org.exist.backup.restore.listener.LogRestoreListener) RestoreListener(org.exist.backup.restore.listener.RestoreListener) BrokerPool(org.exist.storage.BrokerPool)

Example 2 with RestoreListener

use of org.exist.backup.restore.listener.RestoreListener in project exist by eXist-db.

the class SystemExportImportTest method exportImport.

@Test
public void exportImport() throws EXistException, IOException, PermissionDeniedException, SAXException, ParserConfigurationException, AuthenticationException, URISyntaxException, XMLDBException {
    Path file;
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        final Collection test = broker.getCollection(TEST_COLLECTION_URI);
        assertNotNull(test);
        final SystemExport sysexport = new SystemExport(broker, transaction, null, null, direct);
        final String backupDir = temporaryFolder.newFolder().getAbsolutePath();
        file = sysexport.export(backupDir, false, zip, null);
        transaction.commit();
    }
    clean();
    final SystemImport restore = new SystemImport(pool);
    final RestoreListener listener = new LogRestoreListener();
    restore.restore(TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD, null, file, listener);
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        final Collection test = broker.getCollection(TEST_COLLECTION_URI);
        assertNotNull(test);
        DocumentImpl doc = getDoc(broker, test, doc01uri.lastSegment());
        assertEquals(XML1, serializer(broker, doc));
        doc = getDoc(broker, test, doc02uri.lastSegment());
        assertEquals(XML2_PROPER, serializer(broker, doc));
        doc = getDoc(broker, test, doc03uri.lastSegment());
        assertEquals(XML3_PROPER, serializer(broker, doc));
        doc = getDoc(broker, test, doc11uri.lastSegment());
        assertTrue(doc instanceof BinaryDocument);
        try (final InputStream is = broker.getBinaryResource(transaction, ((BinaryDocument) doc))) {
            assertEquals(BINARY, InputStreamUtil.readString(is, UTF_8));
        }
        transaction.commit();
    }
}
Also used : Path(java.nio.file.Path) LogRestoreListener(org.exist.backup.restore.listener.LogRestoreListener) InputStream(java.io.InputStream) Txn(org.exist.storage.txn.Txn) DocumentImpl(org.exist.dom.persistent.DocumentImpl) BinaryDocument(org.exist.dom.persistent.BinaryDocument) DBBroker(org.exist.storage.DBBroker) Collection(org.exist.collections.Collection) LogRestoreListener(org.exist.backup.restore.listener.LogRestoreListener) RestoreListener(org.exist.backup.restore.listener.RestoreListener) BrokerPool(org.exist.storage.BrokerPool)

Example 3 with RestoreListener

use of org.exist.backup.restore.listener.RestoreListener in project exist by eXist-db.

the class FnImport method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (!context.getSubject().hasDbaRole()) {
        throw (new XPathException(this, "Permission denied, calling user '" + context.getSubject().getName() + "' must be a DBA to kill a running xquery"));
    }
    final String dirOrFile = args[0].getStringValue();
    String adminPass = null;
    if (args[1].hasOne()) {
        adminPass = args[1].getStringValue();
    }
    String adminPassAfter = null;
    if (args[2].hasOne()) {
        adminPassAfter = args[2].getStringValue();
    }
    MemTreeBuilder builder = null;
    context.pushDocumentContext();
    try {
        if (NAME.equals(getName())) {
            builder = context.getDocumentBuilder();
            builder.startDocument();
            builder.startElement(IMPORT_ELEMENT, null);
        }
        try {
            final SystemImport restore = new SystemImport(context.getDatabase());
            final RestoreListener listener = new XMLRestoreListener(builder);
            restore.restore(org.exist.security.SecurityManager.DBA_USER, adminPass, adminPassAfter, Paths.get(dirOrFile), listener);
        } catch (final Exception e) {
            throw new XPathException(this, "restore failed with exception: " + e.getMessage(), e);
        }
        if (builder == null) {
            return Sequence.EMPTY_SEQUENCE;
        } else {
            builder.endElement();
            builder.endDocument();
            return (NodeValue) builder.getDocument().getDocumentElement();
        }
    } finally {
        if (builder != null) {
            context.popDocumentContext();
        }
    }
}
Also used : SystemImport(org.exist.backup.SystemImport) NodeValue(org.exist.xquery.value.NodeValue) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) XPathException(org.exist.xquery.XPathException) RestoreListener(org.exist.backup.restore.listener.RestoreListener) AbstractRestoreListener(org.exist.backup.restore.listener.AbstractRestoreListener) XPathException(org.exist.xquery.XPathException)

Example 4 with RestoreListener

use of org.exist.backup.restore.listener.RestoreListener in project exist by eXist-db.

the class Restore method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final String dirOrFile = args[0].getStringValue();
    String adminPass = null;
    if (args[1].hasOne()) {
        adminPass = args[1].getStringValue();
    }
    String adminPassAfter = null;
    if (args[2].hasOne()) {
        adminPassAfter = args[2].getStringValue();
    }
    final boolean overwriteApps = args.length == 4 && args[3].effectiveBooleanValue();
    context.pushDocumentContext();
    try {
        final MemTreeBuilder builder = context.getDocumentBuilder();
        builder.startDocument();
        builder.startElement(RESTORE_ELEMENT, null);
        final BrokerPool pool = context.getBroker().getBrokerPool();
        try {
            final Subject admin = pool.getSecurityManager().authenticate(SecurityManager.DBA_USER, adminPass);
            try (final DBBroker broker = pool.get(Optional.of(admin));
                final Txn transaction = broker.continueOrBeginTransaction()) {
                final RestoreListener listener = new XMLRestoreListener(builder);
                final org.exist.backup.Restore restore = new org.exist.backup.Restore();
                restore.restore(broker, transaction, adminPassAfter, Paths.get(dirOrFile), listener, overwriteApps);
                transaction.commit();
            }
        } catch (final Exception e) {
            throw new XPathException(this, "restore failed with exception: " + e.getMessage(), e);
        }
        builder.endElement();
        builder.endDocument();
        return (NodeValue) builder.getDocument().getDocumentElement();
    } finally {
        context.popDocumentContext();
    }
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) Txn(org.exist.storage.txn.Txn) Subject(org.exist.security.Subject) XPathException(org.exist.xquery.XPathException) DBBroker(org.exist.storage.DBBroker) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) RestoreListener(org.exist.backup.restore.listener.RestoreListener) AbstractRestoreListener(org.exist.backup.restore.listener.AbstractRestoreListener) BrokerPool(org.exist.storage.BrokerPool)

Aggregations

RestoreListener (org.exist.backup.restore.listener.RestoreListener)4 BrokerPool (org.exist.storage.BrokerPool)3 DBBroker (org.exist.storage.DBBroker)3 Txn (org.exist.storage.txn.Txn)3 InputStream (java.io.InputStream)2 Path (java.nio.file.Path)2 AbstractRestoreListener (org.exist.backup.restore.listener.AbstractRestoreListener)2 LogRestoreListener (org.exist.backup.restore.listener.LogRestoreListener)2 Collection (org.exist.collections.Collection)2 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)2 BinaryDocument (org.exist.dom.persistent.BinaryDocument)2 DocumentImpl (org.exist.dom.persistent.DocumentImpl)2 XPathException (org.exist.xquery.XPathException)2 NodeValue (org.exist.xquery.value.NodeValue)2 SystemImport (org.exist.backup.SystemImport)1 Subject (org.exist.security.Subject)1