Example - List all VM backups
-
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
-
Maybe @julien-f could provide some feedback
-
@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"]
-
@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...
-
@KPS I cannot help you, I'm not familiar with
jq
.