Skip to content

Instantly share code, notes, and snippets.

@ryanschwartz
Created November 6, 2015 15:52
Show Gist options
  • Save ryanschwartz/3aa6f4589d6055bdb3b5 to your computer and use it in GitHub Desktop.
Save ryanschwartz/3aa6f4589d6055bdb3b5 to your computer and use it in GitHub Desktop.
wrapper for zfs snapshot + mysql_snapback.py, cribbed from http://letsgetdugg.com/2010/03/30/automated-zfs-snapshots/
#!/bin/bash
# sample crontab configuration
# # "frequent" snapshots, one every 15 minutes, keep 4
# 0,15,30,45 * * * * /opt/src/autosnapper.sh pool/home frequently 4
# # hourly snapshots, once an hour, keep 24 hours
# 0 * * * * /opt/src/autosnapper.sh pool/home hourly 24
# # daily snapshots, keep 7 days
# 0 0 * * * /opt/src/autosnapper.sh pool/home daily 7
# # weekly snapshots, keep 4 weeks
# 0 0 * * 0 /opt/src/autosnapper.sh pool/home weekly 4
# # monthly snapshots, keep 48 months.
# 0 0 1 * * /opt/src/autosnapper.sh pool/home monthly 48
function usage {
cat <<EOF
Usage: $0 pool/filesystem frequency_tag number_to_keep
EOF
exit 1
}
function tailv {
local lines=${1:-10}
awk 'BEGIN { i = 0; j = 0; }
{
out[i] = $0;
if (i >= int(LIMIT)) {
print out[j];
j = j + 1;
}
i = i + 1;
}' LIMIT=$lines
} #end func tailv
[ -z "$1" ] && usage
[ -z "$2" ] && usage
[ -z "$3" ] && usage
NAME="$1"
TAG="$2"
LIMIT="$3"
python /backups/mysql_snapback.py -f /backups/mysql_snapback.cfg -t "mysql-autosnap:${TAG}-$(date +%Y-%m-%d-%H:%M)" && echo "\"zfs snapshot ${NAME}@autosnap:${TAG}-$(date +%Y-%m-%d-%H:%M)\" completed" >> /var/log/autosnapper.log
zfs list -t snapshot -o name |\
grep "^${NAME}@mysql-autosnap:${TAG}-" |\
sort |\
tailv $LIMIT |\
while read snapshot; do
zfs destroy $snapshot && echo "\"zfs destroy $snapshot\" completed" >> /var/log/autosnapper.log
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment