first commit , first release

This commit is contained in:
admin
2020-04-16 01:49:22 +02:00
parent 8907581851
commit e2bc399c02
8 changed files with 184 additions and 21 deletions

26
Makefile Normal file
View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

108
src/zfs-auto-snapshot.sh Executable file
View File

@@ -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
# }