Search in sources :

Example 1 with SystemExport

use of org.exist.backup.SystemExport in project exist by eXist-db.

the class ConsistencyCheckTask method execute.

@Override
public void execute(final DBBroker broker, final Txn transaction) throws EXistException {
    final Agent agentInstance = AgentFactory.getInstance();
    final BrokerPool brokerPool = broker.getBrokerPool();
    final TaskStatus endStatus = new TaskStatus(TaskStatus.Status.STOPPED_OK);
    agentInstance.changeStatus(brokerPool, new TaskStatus(TaskStatus.Status.INIT));
    if (paused) {
        LOG.info("Consistency check is paused.");
        agentInstance.changeStatus(brokerPool, new TaskStatus(TaskStatus.Status.PAUSED));
        return;
    }
    brokerPool.getProcessMonitor().startJob(ProcessMonitor.ACTION_BACKUP, null, monitor);
    PrintWriter report = null;
    try {
        boolean doBackup = createBackup;
        // TODO: don't use the direct access feature for now. needs more testing
        List<ErrorReport> errors = null;
        if (!incremental || incrementalCheck) {
            LOG.info("Starting consistency check...");
            report = openLog();
            final CheckCallback cb = new CheckCallback(report);
            final ConsistencyCheck check = new ConsistencyCheck(broker, transaction, false, checkDocs);
            agentInstance.changeStatus(brokerPool, new TaskStatus(TaskStatus.Status.RUNNING_CHECK));
            errors = check.checkAll(cb);
            if (!errors.isEmpty()) {
                endStatus.setStatus(TaskStatus.Status.STOPPED_ERROR);
                endStatus.setReason(errors);
                LOG.error("Errors found: {}", errors.size());
                doBackup = true;
                if (fatalErrorsFound(errors)) {
                    LOG.error("Fatal errors were found: pausing the consistency check task.");
                    paused = true;
                }
            }
            LOG.info("Finished consistency check");
        }
        if (doBackup) {
            LOG.info("Starting backup...");
            final SystemExport sysexport = new SystemExport(broker, transaction, logCallback, monitor, false);
            lastExportedBackup = sysexport.export(exportDir, incremental, maxInc, createZip, errors);
            agentInstance.changeStatus(brokerPool, new TaskStatus(TaskStatus.Status.RUNNING_BACKUP));
            if (lastExportedBackup != null) {
                LOG.info("Created backup to file: {}", lastExportedBackup.toAbsolutePath().toString());
            }
            LOG.info("Finished backup");
        }
    } catch (final TerminatedException | PermissionDeniedException e) {
        throw new EXistException(e.getMessage(), e);
    } finally {
        if (report != null) {
            report.close();
        }
        agentInstance.changeStatus(brokerPool, endStatus);
        brokerPool.getProcessMonitor().endJob();
    }
}
Also used : Agent(org.exist.management.Agent) EXistException(org.exist.EXistException) TaskStatus(org.exist.management.TaskStatus) ConsistencyCheck(org.exist.backup.ConsistencyCheck) ErrorReport(org.exist.backup.ErrorReport) PermissionDeniedException(org.exist.security.PermissionDeniedException) SystemExport(org.exist.backup.SystemExport) TerminatedException(org.exist.xquery.TerminatedException) PrintWriter(java.io.PrintWriter)

Example 2 with SystemExport

use of org.exist.backup.SystemExport in project exist by eXist-db.

the class FnExport 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();
    boolean incremental = false;
    if (args[1].hasOne()) {
        incremental = args[1].effectiveBooleanValue();
    }
    boolean zip = false;
    if (args[2].hasOne()) {
        zip = args[2].effectiveBooleanValue();
    }
    try {
        MemTreeBuilder builder = null;
        if (NAME.equals(getName())) {
            context.pushDocumentContext();
            builder = context.getDocumentBuilder();
            builder.startDocument();
            builder.startElement(EXPORT_ELEMENT, null);
        }
        try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
            final SystemExport export = new SystemExport(context.getBroker(), transaction, new Callback(builder), null, true);
            export.export(dirOrFile, incremental, zip, null);
            transaction.commit();
        } catch (final Exception e) {
            throw new XPathException(this, "export 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 (NAME.equals(getName())) {
            context.popDocumentContext();
        }
    }
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) XPathException(org.exist.xquery.XPathException) Txn(org.exist.storage.txn.Txn) SystemExport(org.exist.backup.SystemExport) TerminatedException(org.exist.xquery.TerminatedException) XPathException(org.exist.xquery.XPathException)

Aggregations

SystemExport (org.exist.backup.SystemExport)2 TerminatedException (org.exist.xquery.TerminatedException)2 PrintWriter (java.io.PrintWriter)1 EXistException (org.exist.EXistException)1 ConsistencyCheck (org.exist.backup.ConsistencyCheck)1 ErrorReport (org.exist.backup.ErrorReport)1 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)1 Agent (org.exist.management.Agent)1 TaskStatus (org.exist.management.TaskStatus)1 PermissionDeniedException (org.exist.security.PermissionDeniedException)1 Txn (org.exist.storage.txn.Txn)1 XPathException (org.exist.xquery.XPathException)1 NodeValue (org.exist.xquery.value.NodeValue)1