Zfs: [question] Get unix timestamp of latest snapshot of given dataset

Created on 2 Apr 2019  ·  10Comments  ·  Source: openzfs/zfs

I want to write nagios check for ZFS replication. But i can't get zfs list to show unix timestamp of snapshot creation date/time. I know i can probably parse formated datetime, but i think having unix timestamp would be less error prone.

Idealy i want to get timestamps of newest (and maybe oldest) snapshot for all datasets.
Do you think, you can add such feature to zfs list?

I have replication scheduled to run every 15 minutes, so i think it would be reasonable to check there is no dataset that has latest snapshot older than 1 hour...

Most helpful comment

I figured out my question so deleted. This should help @Harvie
zfs list -p -o name,creation -t snapshot

All 10 comments

Please read issue template next time, not just delete it. You should use mailing lists/IRC for support questions.

Offtopic - just add -H (scripting mode). https://github.com/zfsonlinux/zfs/blob/master/man/man8/zfs.8#L2837

Thanks for swift reply.

-H Used for scripting mode. Do not print headers and separate fields by a single tab instead of arbitrary white space.

Well that's useful, but it does not provide unix timestamps...

zfs get -p creation dataset@snapshot

I figured out my question so deleted. This should help @Harvie
zfs list -p -o name,creation -t snapshot

@drescherjm @richardelling PERFECT! That is what i was looking for... Thank you very much.

Any ideas how to list only snapshots for given dataset? (without grep :-)

@Harvie “zfs list -t snapshot -r pool/dataset”

@ptx0 maybe i am dumb, but i was not able to find it.
@jwittlincohen I've tried this already, but this as well lists snapshots for all child datasets. eg. pool/dataset/foo/bar@something... i want only pool/dataset@something...

Anyway... Thanks for the help. I've created following script that can be used by nagios to check that backups are working... So i wanted to share it in case someone finds it useful:

#!/bin/sh
#
# (c) Tomas 'Harvie' Mudrunka 2019
#
# This script goes through all ZFS datasets and checks
# if all of them have at least one snapshot newer than $time_limit seconds

time_limit=3600
#time_limit=20
debug=false
dataset_list="$(zfs list -H -p -o name -t filesystem,volume)"
snapshot_list="$(zfs list -H -p -o name,creation -t snapshot)"
current_time="$(date +%s)"

get_snapshots_by_dataset() {
    echo "$1@null   0"
    echo "$snapshot_list" | grep ^"$1"@
}

get_newest_snapshot_timestamp() {
    get_snapshots_by_dataset "$1" | cut -f 2 | grep '[0-9]' | sort -n | tail -n 1
}

status=true
echo "$dataset_list" | while read dataset; do
    current_snapshot="$(get_newest_snapshot_timestamp "$dataset")"
    time_diff="$(($current_time - $current_snapshot))"
    [ $time_diff -gt $time_limit ] && status=false
    [ "$status" = 'false' -o "$debug" = 'true'  ] &&
        echo "Latest snapshot of $dataset is $time_diff seconds old (limit is $time_limit s)"
    $status
done; exit $?

@ptx0 maybe i am dumb, but i was not able to find it.
@jwittlincohen I've tried this already, but this as well lists snapshots for all child datasets. eg. pool/dataset/foo/bar@something... i want only pool/dataset@something...

As ptx0 said, you need the depth option.

zfs list -t snapshot -r pool/dataset -d1 will only show snapshots for the dataset specified, with no snapshots from child datasets.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pruiz picture pruiz  ·  60Comments

crollorc picture crollorc  ·  143Comments

allanjude picture allanjude  ·  72Comments

runderwo picture runderwo  ·  54Comments

mabod picture mabod  ·  53Comments