XCP-ng
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Example - List all VM backups

    Scheduled Pinned Locked Moved REST API
    7 Posts 4 Posters 753 Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K Offline
      KPS Top contributor
      last edited by KPS

      Hi!

      I am struggeling, when it comes to write small scripts with API-calls. Perhaps, this one can help some of you.

      The code is not "beautiful", but working.
      It lists all backups to be able to browse them, monitor, etc.:

      #!/bin/bash
      
      export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
      
      /usr/local/bin/xo-cli --register --alowUnauthorized  --token XXXX http://xoa
      
      # Script lists all backups on all remotes.
      # --verbose gives the "full" output
      # Without --verbose, the fields in the output are limited to the most important ones.
      
      # Restore possible with e.g.: xo-cli backupNg.importVmBackup id='<BackupID>' sr=<TARGET-SR-UUID> settings=json:'{"newMacAddresses":false}'
      
      VERBOSE_MODE=0
      
      for arg in "$@"
      do
          if [ "$arg" = "--verbose" ] || [ "$arg" = "-v" ]; then
              VERBOSE_MODE=1
          fi
      done
      
      # Create Empty TMP-file
      cat /dev/null >  /tmp/listVMs.txt
      
      # Check every "enabled" remote
      for remote in $(xo-cli remote.getAll --json | jq -r '.[] | [.enabled,.name,.id] | @csv' | | grep -e '^true')
      do
              remoteenabled=$(echo $remote | awk -F "," '{print $1}')
              remotename=$(echo $remote | awk -F "," '{print $2}' | sed 's/["'\'']//g' )
              remoteuuid=$(echo $remote | awk -F "," '{print $3}' )
                      
      # Check every backup on that remote
              for i in $( xo-cli backupNg.listVmBackups remotes=json:["$remoteuuid"] | grep 'name_label\|id:\|size\|timestamp' | grep -v uuid | sed ':a;N;$!ba;s/,\n/;/g' | sed 's/[[:space:]]//g')
              do
      
                      backupid=$(echo $i | grep -Po "id:\'[0-9a-z].*" | awk -F ";" '{print $1}' | awk -F "'" '{print $2}')
      
                      backuptimestamp=$(echo $i | grep -Po 'timestamp:[0-9]{13}' | awk -F ":" '{print $2}')
                      backuptimestamp=$(($backuptimestamp + 0))
                      backuptimedate=$(date -d @$(($backuptimestamp / 1000)))
                      backupage=$(awk "BEGIN {printf \"%.2f\", ($(date +%s%3N) - $backuptimestamp) / 86400000 }")
      
                      vmname=$(echo $i |  grep -Po "name_label:'.*'" | awk -F "'" '{print $2}')
                      if [ -z "$vmname" ];then vmname="UNKNOWN"; fi
      
                      backupsize=$(echo $i | grep -Po 'size:[0-9]{1,30}' | awk -F ":" '{print $2}')
                      backupsize=$(awk "BEGIN {printf \"%.2f\", $backupsize / 1024 / 1024}")
      
                      echo $vmname";"$backupage";"$backuptimestamp";"$backuptimedate";"$backupsize";"$remotename";"$backupid >> /tmp/listVMs.txt
              done
      
      
      done
      
      if [ "$VERBOSE_MODE" -eq 1 ]
      then
              echo "VM-Name;BackupAge(d);BackupTimestamp;BackupTimeDate;BackupSize(MB);RemoteName;BackupID"
              cat /tmp/listVMs.txt | sort
      else
              echo "VM-Name;BackupAge(d);BackupTimestamp;BackupTimeDate;BackupSize(MB);RemoteName;BackupID" | awk -F ";" '{print $1 ";" $3 ";" $4 ";" $5 ";" $6}'
              cat /tmp/listVMs.txt | sort | awk -F ";" '{print $1 ";" $3 ";" $4 ";" $5 ";" $6}'
      fi
      

      Feel free to use, change and "improve" it...
      I was not able to parse the "listVmBackups"-output with jq...

      KPS

      julien-fJ 1 Reply Last reply Reply Quote 0
      • olivierlambertO Offline
        olivierlambert Vates 🪐 Co-Founder CEO
        last edited by

        Maybe @julien-f could provide some feedback 🙂

        1 Reply Last reply Reply Quote 0
        • julien-fJ Offline
          julien-f Vates 🪐 Co-Founder XO Team @KPS
          last edited by julien-f

          @KPS As explained in xo-cli --help, you can use the --json flag to heve the result in JSON format:

          xo-cli backupNg.listVmBackups --json remotes=json:["$remoteuuid"]
          
          K 1 Reply Last reply Reply Quote 0
          • K Offline
            KPS Top contributor @julien-f
            last edited by

            @julien-f
            Thank you. I am just lost with the next step in jq.
            How can I export something like two fields from the json-result?

            xo-cli backupNg.listVmBackups --json remotes=json:'["<sruuid>"]' | jq '. | .timestamp'
            

            --> Does not give results...

            julien-fJ C 2 Replies Last reply Reply Quote 0
            • julien-fJ Offline
              julien-f Vates 🪐 Co-Founder XO Team @KPS
              last edited by

              @KPS I cannot help you, I'm not familiar with jq.

              1 Reply Last reply Reply Quote 0
              • C Offline
                cmuller @KPS
                last edited by cmuller

                @KPS hello. Sorry for the late answer, I am just starting using Xcpng (and xo-cli).

                As for your jq program you can start by piping into jq . and then you will see that the returned json is a list of list of list where.. you will find a timestamp field. So you cannot directly request .timestamp you have to use something like:

                $ xo-cli backupNg.listVmBackups --json remotes="json:[\"$REMOTEUUID\"]" \
                   | jq '.[][][].timestamp'
                1721253629744
                1721253606389
                

                in order to get all timestamp fields from the lists of lists of lists.

                Then if you want to get several fields it is possible to create new jsons with new field names (or the same) their value being extracted from the parsed json. For example here I create jsons with "VM" and "timestamp" fields:

                $ xo-cli backupNg.listVmBackups --json remotes="json:[\"$REMOTEUUID\"]" \
                   | jq '.[][][] | {VM: .vm.name_label, timestamp: .timestamp}'
                {
                  "VM": "XenOrchestra",
                  "timestamp": 1721253675148
                }
                {
                  "VM": "RockyBench",
                  "timestamp": 1721253606389
                }
                

                Hope it'll help.

                Here is a small tutorial: https://mosermichael.github.io/jq-illustrated/dir/content.html
                And the reference doc: https://jqlang.github.io/jq/manual/

                Cheers,
                Christophe.

                K 1 Reply Last reply Reply Quote 2
                • K Offline
                  KPS Top contributor @cmuller
                  last edited by

                  @cmuller
                  Thank you! It's great to see code-sharing in the forum!

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post