From e2bc399c024df30b2ce0b3e22962b9d59e6cd8e5 Mon Sep 17 00:00:00 2001 From: admin Date: Thu, 16 Apr 2020 01:49:22 +0200 Subject: [PATCH] first commit , first release --- Makefile | 26 +++++++ README.md | 40 +++++------ etc/zfs-auto-snapshot.cron.daily | 7 ++ etc/zfs-auto-snapshot.cron.frequent | 3 + etc/zfs-auto-snapshot.cron.hourly | 7 ++ etc/zfs-auto-snapshot.cron.monthly | 7 ++ etc/zfs-auto-snapshot.cron.weekly | 7 ++ src/zfs-auto-snapshot.sh | 108 ++++++++++++++++++++++++++++ 8 files changed, 184 insertions(+), 21 deletions(-) create mode 100644 Makefile create mode 100644 etc/zfs-auto-snapshot.cron.daily create mode 100644 etc/zfs-auto-snapshot.cron.frequent create mode 100644 etc/zfs-auto-snapshot.cron.hourly create mode 100644 etc/zfs-auto-snapshot.cron.monthly create mode 100644 etc/zfs-auto-snapshot.cron.weekly create mode 100755 src/zfs-auto-snapshot.sh diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4b0e54d --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +all: + +install: + install -d /etc/cron.d + install -d /etc/cron.daily + install -d /etc/cron.hourly + install -d /etc/cron.weekly + install -d /etc/cron.monthly + install -m 0644 etc/zfs-auto-snapshot.cron.frequent /etc/cron.d/zfs-auto-snapshot + install etc/zfs-auto-snapshot.cron.hourly /etc/cron.hourly/zfs-auto-snapshot + install etc/zfs-auto-snapshot.cron.daily /etc/cron.daily/zfs-auto-snapshot + install etc/zfs-auto-snapshot.cron.weekly /etc/cron.weekly/zfs-auto-snapshot + install etc/zfs-auto-snapshot.cron.monthly /etc/cron.monthly/zfs-auto-snapshot + install -d /usr/local/sbin + install src/zfs-auto-snapshot.sh /usr/local/sbin/zfs-auto-snapshot + +uninstall: + rm /etc/cron.d/zfs-auto-snapshot + rm /etc/cron.hourly/zfs-auto-snapshot + rm /etc/cron.daily/zfs-auto-snapshot + rm /etc/cron.weekly/zfs-auto-snapshot + rm /etc/cron.monthly/zfs-auto-snapshot + rm /usr/local/sbin/zfs-auto-snapshot + + + diff --git a/README.md b/README.md index 39af52c..57d96be 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,27 @@ # README # -This README would normally document whatever steps are necessary to get your application up and running. +zfs-auto-snapshot scripts allow automatic snapshots of all zfs filesystems and volumes. +The script will automatically rotate snapshots deleting old ones. -### What is this repository for? ### +It creates: +4 snapshots for hour to rotate every 15 min +24 hourly snapshots +30 daily snapshots +12 weekly snapshots +12 yearly snapshots -* Quick summary -* Version -* [Learn Markdown](https://bitbucket.org/tutorials/markdowndemo) +Script has been tested on zfs on linux 0.8.3, centos 7 +Original script from https://github.com/zfsonlinux/zfs-auto-snapshot +I semplified the code and fixed some bugs -### How do I get set up? ### +# to install +cd to current folder +make install -* Summary of set up -* Configuration -* Dependencies -* Database configuration -* How to run tests -* Deployment instructions +# to uninstall +cd to current folder +make uninstall -### Contribution guidelines ### +# NOTE # +uninstalling the script doesn't delete made snapshots -* Writing tests -* Code review -* Other guidelines - -### Who do I talk to? ### - -* Repo owner or admin -* Other community or team contact \ No newline at end of file diff --git a/etc/zfs-auto-snapshot.cron.daily b/etc/zfs-auto-snapshot.cron.daily new file mode 100644 index 0000000..2403d8b --- /dev/null +++ b/etc/zfs-auto-snapshot.cron.daily @@ -0,0 +1,7 @@ +#!/bin/sh +PATH=$PATH:/usr/local/sbin + +# Only call zfs-auto-snapshot if it's available +which zfs-auto-snapshot > /dev/null || exit 0 + +exec zfs-auto-snapshot --label daily --keep 31 diff --git a/etc/zfs-auto-snapshot.cron.frequent b/etc/zfs-auto-snapshot.cron.frequent new file mode 100644 index 0000000..d66c404 --- /dev/null +++ b/etc/zfs-auto-snapshot.cron.frequent @@ -0,0 +1,3 @@ +PATH="/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin" + +*/15 * * * * root which zfs-auto-snapshot > /dev/null || exit 0 ; zfs-auto-snapshot --label quarterhour --keep 4 diff --git a/etc/zfs-auto-snapshot.cron.hourly b/etc/zfs-auto-snapshot.cron.hourly new file mode 100644 index 0000000..3be78eb --- /dev/null +++ b/etc/zfs-auto-snapshot.cron.hourly @@ -0,0 +1,7 @@ +#!/bin/sh +PATH=$PATH:/usr/local/sbin + +# Only call zfs-auto-snapshot if it's available +which zfs-auto-snapshot > /dev/null || exit 0 + +exec zfs-auto-snapshot --label hourly --keep 24 diff --git a/etc/zfs-auto-snapshot.cron.monthly b/etc/zfs-auto-snapshot.cron.monthly new file mode 100644 index 0000000..6be7b12 --- /dev/null +++ b/etc/zfs-auto-snapshot.cron.monthly @@ -0,0 +1,7 @@ +#!/bin/sh +PATH=$PATH:/usr/local/sbin + +# Only call zfs-auto-snapshot if it's available +which zfs-auto-snapshot > /dev/null || exit 0 + +exec zfs-auto-snapshot --label monthly --keep 12 diff --git a/etc/zfs-auto-snapshot.cron.weekly b/etc/zfs-auto-snapshot.cron.weekly new file mode 100644 index 0000000..054ce68 --- /dev/null +++ b/etc/zfs-auto-snapshot.cron.weekly @@ -0,0 +1,7 @@ +#!/bin/sh +PATH=$PATH:/usr/local/sbin + +# Only call zfs-auto-snapshot if it's available +which zfs-auto-snapshot > /dev/null || exit 0 + +exec zfs-auto-snapshot --label weekly --keep 12 diff --git a/src/zfs-auto-snapshot.sh b/src/zfs-auto-snapshot.sh new file mode 100755 index 0000000..419fc47 --- /dev/null +++ b/src/zfs-auto-snapshot.sh @@ -0,0 +1,108 @@ +#!/bin/sh + +opt_prefix='zfs-auto-snap' + +do_snapshots () +{ + local KEEP='' + + for ii in $TARGETS + do + zfs snapshot "$ii@$SNAPNAME" + + KEEP="$opt_keep" + + # ASSERT: The old snapshot list is sorted by increasing age. + for jj in $SNAPSHOTS_OLD + do + # Check whether this is an old snapshot of the filesystem. + if [ -z "${jj#$ii@$SNAPGLOB}" ] + then + KEEP=$(( $KEEP - 1 )) + if [ "$KEEP" -le '0' ] + then + zfs destroy -d "$jj" + + fi + fi + done + done +} + + +# main () +# { + +while [ "$#" -gt '0' ] +do + case "$1" in + --keep) + if [ ! "$2" -gt '0' ]; + then + exit 1 + fi + opt_keep="$2" + shift 2 + ;; + --label) + opt_label="$2" + shift 2 + ;; + esac +done + +ZPOOL_STATUS=$(env LC_ALL=C zpool status 2>&1 ) +ZFS_LIST=$(env LC_ALL=C zfs list -t volume,filesystem -H) +CANDIDATES=$(echo "$ZFS_LIST" | awk -F '\t' '{print $1}') +SNAPSHOTS_OLD=$(env LC_ALL=C zfs list -H -t snapshot -S creation -o name) + +# Get a list of pools that are being scrubbed. +ZPOOLS_SCRUBBING=$(echo "$ZPOOL_STATUS" | awk -F ': ' \ + '$1 ~ /^ *pool$/ { pool = $2 } ; \ + $1 ~ /^ *scan$/ && $2 ~ /scrub in progress/ { print pool }' \ + | sort ) + +# Get a list of pools that cannot do a snapshot. +ZPOOLS_NOTREADY=$(echo "$ZPOOL_STATUS" | awk -F ': ' \ + '$1 ~ /^ *pool$/ { pool = $2 } ; \ + $1 ~ /^ *state$/ && $2 !~ /ONLINE|DEGRADED/ { print pool } ' \ + | sort) + +# Initialize the list of datasets that will get a recursive snapshot. +TARGETS='' + +for ii in $CANDIDATES +do + # Exclude datasets in pools that cannot do a snapshot. + + for jj in $ZPOOLS_NOTREADY + do + if [ "$(echo "$ii" | awk -F/ '{print $1}')" = "$jj" ] + then + continue 2 + fi + done + for jj in $ZPOOLS_SCRUBBING + do + if [ "$(echo "$ii" | awk -F/ '{print $1}')" = "$jj" ] + then + continue 2 + fi + done + + TARGETS="$TARGETS $ii" +done + +# ISO style date; fifteen characters: YYYY-MM-DD-HH-MM +DATE=$(date --utc +%F-%H-%M) + +# The snapshot name after the @ symbol. +SNAPNAME="${opt_prefix}_${opt_label}_$DATE" + +# The expression for matching old snapshots. +SNAPGLOB="${opt_prefix}_${opt_label}_????-??-??-??-??" + +do_snapshots "$SNAPNAME" "$SNAPGLOB" "$TARGETS" + +exit 0 +# }