Skip to content

Instantly share code, notes, and snippets.

@olivierlambert
Created April 26, 2023 19:36
Show Gist options
  • Save olivierlambert/a7d4a0add61c96d837db81970bc7212f to your computer and use it in GitHub Desktop.
Save olivierlambert/a7d4a0add61c96d837db81970bc7212f to your computer and use it in GitHub Desktop.
An example script to generate a CSV of all you non-backuped VMs
const host = process.argv[2]
const token = process.argv[3]
const filterRemoteId = process.argv[4]
async function get(path) {
const headers = {
cookie: `authenticationToken=${token}`,
}
try{
const res = await (
await fetch(`${host}${path}`, {
headers
})
).json()
return res
}catch(err){
}
return {}
}
function escape(str='') {
return str.replace(/"/g,'""' )
}
const logs = await get('/rest/v0/backups/logs?fields=tasks')
const vmLastBackup = {}
for(const log of logs){
for(const task of log.tasks ?? []){
if(task.data?.type !== 'VM'){
continue
}
const vmId = task.data.id
for(const subtask of task.tasks ?? []){
if(subtask.data?.type !== 'remote'){ // we're looking for backup to a remote , not replication
continue
}
if(filterRemoteId && subtask.data?.id !== filterRemoteId){
continue
}
if(subtask.status !== 'success'){
//failed or running backup don't count
continue
}
vmLastBackup[vmId] = true
}
}
}
const vms = await get('/rest/v0/vms?fields=id,$container,$pool,tags,name_label')
console.log(`"VM-NAME";"VM-UUID"; "HOST/CLUSTER";"Tags"`)
for(const vm of vms){
if(!vmLastBackup[vm.id]){
console.log(`"${escape(vm.name_label)}";"${vm.id}";"${vm.$container}/${vm.$pool}";"${escape(vm.tags?.join(','))}"`)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment