title: Xquery. How to check current incremental backup status?
body: <p>I have written an Xquery to that gets executed at the time of when incremental backup is in progress. I know the backup status returns three possible values - 
  <code>completed</code>, <code>in-progress</code> and <code>failed</code>. Not sure the exact value of last one but anyways this is my xquery - </p>
  
  <pre><code>    xquery version "1.0-ml";
  
  declare function local:escape-for-regex
    ( $arg as xs:string? )  as xs:string {
  
     replace($arg,
             '(\.|\[|\]|\\|\||\-|\^|\$|\?|\*|\+|\{|\}|\(|\))','\\$1')
   } ;
  
  declare function local:substring-before-last
    ( $arg as xs:string? ,
      $delim as xs:string )  as xs:string {
  
     if (matches($arg, local:escape-for-regex($delim)))
     then replace($arg,
              concat('^(.*)', local:escape-for-regex($delim),'.*'),
              '$1')
     else ''
   } ;
  
  let $server-info := doc("/config/server-info.xml")
  let $content-database :="xyzzy"
  let $backup-directory:=$server-info/configuration/server-info/backup-directory/text()
  let $backup-latest-dateTime := xdmp:filesystem-directory(fn:concat( $backup-directory,'/',$content-database))/dir:entry[1]/dir:filename/text()
  let $backup-latest-date := fn:substring-before($backup-latest-dateTime,"-")
  let $backup-info := cts:search(/,cts:element-value-query(xs:QName("directory-name"),$backup-latest-date))
  let $new-backup := if($backup-info)
                      then fn:false()
                     else fn:true()
  let $db-bkp-status := if($new-backup)
                          then (xdmp:database-backup-status(())[./*:forest/*:backup-path[fn:contains(., $backup-latest-dateTime)]][./*:forest/*:incremental-backup eq "false"]/*:status)
                        else (xdmp:database-backup-status(())[./*:forest/*:backup-path[fn:contains(., $backup-latest-dateTime)]][./*:forest/*:incremental-backup eq "true"][./*:forest/*:incremental-backup-path[fn:contains(., fn:replace(local:substring-before-last(xs:string(fn:current-date()), "-"), "-", ""))]]/*:status)
  
  return $db-bkp-status
  </code></pre>
  
  <p>We maintain a configuration file that stores backup status. If there is a new full backup day then <code>$backup-info</code> will return nothing. If it is daily incremental backup day then it will return the config. I'm using it just to check if todays backup is new full or incremental. For incremental day <code>$backup-info</code> is false and so it goes to the last line i.e. else condition. this doesn't return anything for incremental backups. Neither <code>completed</code> nor <code>in-progress</code>. I wonder how markLogic picks up the timestamp. Please assist on this.  </p>
  
  <p>Feel free to provide your own xquery from scratch. I can update mine.<br>
  I even took out the Job id and search in the output of the function <code>xdmp:database-backup-status(())</code> but that job id too doesn't exist in the result set.</p>