0
votes

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 - completed, in-progress and failed. Not sure the exact value of last one but anyways this is my xquery -

    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

We maintain a configuration file that stores backup status. If there is a new full backup day then $backup-info 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 $backup-info is false and so it goes to the last line i.e. else condition. this doesn't return anything for incremental backups. Neither completed nor in-progress. I wonder how markLogic picks up the timestamp. Please assist on this.

Feel free to provide your own xquery from scratch. I can update mine.
I even took out the Job id and search in the output of the function xdmp:database-backup-status(()) but that job id too doesn't exist in the result set.

1

1 Answers

1
votes

MarkLogic provides the Admin modules to provide much of the information you are attempting to get via other methods. The Admin UI modules (typically found in /opt/MarkLogic/Modules/MarkLogic/Admin/Lib) contains a lot of helpful code that can be adapted to get these sorts of details. In this case I would refer to database-status-form.xqy

define function db-mount-state(
  $fstats as node()*,
  $fcounts as node()*,
  $dbid as xs:unsignedLong)
{
let $times := $fstats/fs:last-state-change,
  $ls := max($times),
  $since :=
    if (not(empty($ls)))
    then concat(" since ", longDate($ls), " ", longTimeSecs($ls))
    else ""
return concat(database-status($dbid,$fstats,$fcounts),$since)
}

define function backup-recov-state($fstats as node()*)
{
  if(empty($fstats/fs:backups/fs:backup)
       and
     empty($fstats/fs:restore))
  then
    "No backup or restore in progress"
  else
    if(empty($fstats/fs:backups/fs:backup))
    then
      "Restore in progress (see below for details)"     
    else
      "Backup in progress (see below for details)"
}

... Call the functions against your database, then pull the details from the elements you want:

let $last-full-backup := max($fstats/fs:last-backup)
let $last-incremental-backup : = max($fstats/fs:last-incr-backup
return ($last-full-backup, $last-incremental-backup)

This is just some sample code snippets, not executable, but it should get you moving in the right direction.