Created
May 28, 2018 14:19
-
-
Save sgielen/18e150e6b9e384045fe2daa94d073b0f to your computer and use it in GitHub Desktop.
ZFS management scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0 10 * * * root /etc/bin/check-zfs-status | |
0 18 * * * root /etc/bin/make-zfs-snapshot storage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Make sure your cron can send e-mails before using this, so you're notified in the case of problems :-)