Skip to main content

Administrating MarkLogic Server

Restoring to the Safe Timestamp

If you want to restore as much data as possible, you can restore your data to the minimum safe timestamp.

For example, the database you want to restore has four forests, as shown below. You use the xdmp:host-status function to locate the safe-restore-to-time value, which is earliest of the four last-commit timestamps. In this example, the safe-restore-to-time is the timestamp of the last committed transaction in Forest A.

Graphic showing a database to restore which has has four forests,

The following procedure describes how to restore to the minimum timestamp using the XQuery API.

Note

This same procedure can be done using the Admin Interfaces described in Setting Merge Policy, Admin Interface for Database Restore, and Rolling Back a Transaction.

  1. Use the admin:database-get-merge-timestamp function to get the current merge timestamp. Save this value so it can be reset after you have completed the rollback operation.

  2. Use the admin:database-set-merge-timestamp function to set the merge timestamp to any time before your minimum safe timestamp. This will preserve fragments in merge after this timestamp until you have rolled back your forest data.

  3. Use the xdmp:database-restore function with $journal-archiving set to fn:true() and $restoreToTime set to null( ) to restore the database to the latest timestamp.

  4. After the restore operation has completed, use the xdmp:forest-rollback function to roll back the forests to the safe-restore-to-time timestamp returned by the xdmp:host-status function.

    For example, if you are restoring the Documents database, you can use the following query to rollback your forest data:

    xquery version "1.0-ml";
    declare namespace host = "http://marklogic.com/xdmp/status/host";
    let $timestamp :=  
      xdmp:wallclock-to-timestamp(
        xs:dateTime(xdmp:host-status(xdmp:host("your-host.com"))
        /host:restore-jobs/host:restore-job/host:safe-restore-to-time
        /fn:data(.))) 
    return
      xdmp:forest-rollback(
        xdmp:database-forests(xdmp:database("Documents")),
        $timestamp)
  5. Use admin:database-set-merge-timestamp function to set the merge timestamp back to the value you saved in Step 1.