Skip to content

Instantly share code, notes, and snippets.

@sgielen
Created May 28, 2018 14:19
Show Gist options
  • Save sgielen/18e150e6b9e384045fe2daa94d073b0f to your computer and use it in GitHub Desktop.
Save sgielen/18e150e6b9e384045fe2daa94d073b0f to your computer and use it in GitHub Desktop.
ZFS management scripts
#!/bin/bash
PATH="/sbin:/bin:/usr/sbin:/usr/bin"
state=$(zpool status | grep state)
if [ "$state" != " state: ONLINE" ]; then
echo "Zpool status state is not ONLINE"
zpool status
exit 1
fi
errors=$(zpool status | grep errors | grep -v "with 0 errors")
if [ "$errors" != "errors: No known data errors" ]; then
echo "Zpool status possible known data errors"
zpool status
exit 1
fi
#!/bin/bash
set -e
PATH="/sbin:/bin:/usr/sbin:/usr/bin"
zpoolname="$1"
currdate=$(date +%Y%m%d)
snapshotname="${zpoolname}@autosnapshot-$currdate"
zfs snapshot $snapshotname
thendate=$(date -d "now - 14 days" +%Y%m%d)
thensnapshotname="${zpoolname}@autosnapshot-$thendate"
if zfs list -t snapshot | grep "$thensnapshotname " >/dev/null; then
exists=1
else
exists=0
fi
# check if this snapshot is the oldest one
zfs list -t snapshot | grep ${zpoolname}@autosnapshot | awk '{print $1}' | while read snapname; do
snapdate=$(echo "$snapname" | cut -d- -f2)
if [ "$snapdate" -lt "$thendate" ]; then
echo "Warning: An autosnapshot exists older than 2 weeks ago: $snapname"
echo "Exiting to prevent causing trouble"
exit 1
fi
done
if [ "$exists" != "1" ]; then
echo "Warning: The 2 week old snapshot $thensnapshotname didn't exist"
echo "Exiting to prevent causing trouble"
exit 1
fi
zfs destroy $thensnapshotname
0 10 * * * root /etc/bin/check-zfs-status
0 18 * * * root /etc/bin/make-zfs-snapshot storage
@sgielen
Copy link
Author

sgielen commented May 28, 2018

Make sure your cron can send e-mails before using this, so you're notified in the case of problems :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment