Output of the new replicatedctl snapshot ls
command is randomly ordered, but often you will only care about the most recent one. By setting the output format to json with --output json
, we can get a machine-readable output. Parsing json on the command line is difficult, but is simplified greatly by jq, a utility that describes itself as “sed for JSON data”.
So if we sort by finished
, we’ll have an ordered list - but the most recent date will be last. However, jq allows for indexing from the end of the array, with -1 being the last item.
Putting it together, replicatedctl snapshot ls --output json | jq '. | sort_by(.finished) | .[-1].id'
yields a single snapshot id string, such as "6c9482671fb045de6d775124c1856423"
, that can then be used in a restore operation.
The replicatedctl snapshot
documentation can be found here.