List snapshots only

Hello LXDers,

I need to list all the snapshots in all my containers. I am about to write a script (awk, golang or else) to extract out and parse the snapshots table that is displayed at the end of 'lxc info '.

However, I am wondering if there is something that me and my googling missed.

Ideally a command that would give out a snapshot list in a parsable format (json or similar), or an API that I could use in a go program.

Many thanks.
L.

I found this:

https://linuxcontainers.org/lxd/api/master/#/instances/instance_snapshots_get

Time for me to start understanding how to use LXD’s API, unless someone has a simpler way of getting a snapshot list.

Using this: lxd package - github.com/lxc/lxd/client - pkg.go.dev

It turns out that it’s super easy to get a snapshot list for each containers:

package main

import (
  "fmt"
  lxd "github.com/lxc/lxd/client"
)

func main() {
  c, err := lxd.ConnectLXDUnix("", nil)
  E(err)
  names, err := c.GetContainerNames()
  E(err)
  for _, name := range names {
        fmt.Printf("Container %s:\n", name)
        snaps, err := c.GetContainerSnapshots(name)
        E(err)
        for _, snap := range snaps {
                fmt.Printf(" - %s\n", snap.Name)
        }
  }
}

func E(err error) {
  if err != nil {
        panic(err)
  }
}
2 Likes

Hi lyderic,
It is not an elegant method, it is a little bit old school way.
for k in $(lxc ls -c"n" -fcsv) ; do curl -s --unix-socket /var/snap/lxd/common/lxd/unix.socket a/1.0/instances/$k/snapshots? | jq .metadata[]; done
Thanks for the source code by the way.
Regards.

2 Likes

Very good indeed. To use in a shell script if golang is not an option.
Thanks!

lxc query /1.0/instances/NAME/snapshots would be even shorter :slight_smile:

2 Likes

Ah ah, excellent Stéphane! I completely missed ‘lxc query’. I will use it a lot in the future.

Mille mercis.
L.

lxc query [<remote>:]<API path> [flags]
Global Flags:
–project Override the source project

Doenst work:

lxc query /1.0/instances/NAME/snapshots --project x
Error: --project cannot be used with the query command

Works:

lxc query /1.0/instances/NAME/snapshots?project=x