Update bashclub-zfs

This commit is contained in:
cpzengel 2022-01-31 16:49:26 +01:00 committed by GitHub
parent 3f681bce20
commit 6830ddf05f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -63,24 +63,26 @@ ZFS() {
fi fi
} }
### (
### defaults flock -n 9 || exit 1
### ###
tag="$prog" ### defaults
dateopts="+%F_%T" ###
keep=5 tag="$prog"
verbose=false dateopts="+%F_%T"
quiet=false keep=5
tossh=false verbose=false
fromssh=false quiet=false
port=22 tossh=false
sshcompress="" fromssh=false
reinit="" port=22
intermediate="-i " sshcompress=""
### reinit=""
### parse options intermediate="-i "
### ###
while getopts "hvqk:p:t:d:srCIRg:" opt ; do ### parse options
###
while getopts "hvqk:p:t:d:srCIRg:" opt ; do
case $opt in case $opt in
h) usage 0 ;; h) usage 0 ;;
v) v)
@ -101,32 +103,32 @@ while getopts "hvqk:p:t:d:srCIRg:" opt ; do
g) gpgid="$OPTARG" ;; g) gpgid="$OPTARG" ;;
*) usage 1 ;; *) usage 1 ;;
esac esac
done done
shift $((OPTIND-1)) shift $((OPTIND-1))
date="$(date $dateopts)" date="$(date $dateopts)"
$tossh && $fromssh && die 1 "-s and -r are mutually exclusive" $tossh && $fromssh && die 1 "-s and -r are mutually exclusive"
if ! $tossh && [[ -n $gpgid ]] ; then if ! $tossh && [[ -n $gpgid ]] ; then
die 1 "-g can only be used with -s" die 1 "-g can only be used with -s"
fi fi
### ###
### parse src & dest host/fs info ### parse src & dest host/fs info
### ###
# fail if there's ever >1 colon # fail if there's ever >1 colon
if [[ $1 =~ :.*: || $2 =~ :.*: ]] ; then if [[ $1 =~ :.*: || $2 =~ :.*: ]] ; then
die 1 "invalid fsspec: '$1' or '$2'" die 1 "invalid fsspec: '$1' or '$2'"
fi fi
# fail if src or dest isn't specified # fail if src or dest isn't specified
if [[ -z $1 || -z $2 ]] ; then if [[ -z $1 || -z $2 ]] ; then
usage 1 usage 1
fi fi
src="$1" src="$1"
dest="$2" dest="$2"
### ###
### ssh mode - output snaps from local fs to ssh or read snaps from ssh to local fs ### ssh mode - output snaps from local fs to ssh or read snaps from ssh to local fs
if $tossh ; then if $tossh ; then
log "sending from local zfs filesystem to SSH server" log "sending from local zfs filesystem to SSH server"
# make sure src exists # make sure src exists
@ -188,7 +190,7 @@ if $tossh ; then
fi fi
exit exit
elif $fromssh ; then elif $fromssh ; then
log "receving from SSH server to local zfs filesystem" log "receving from SSH server to local zfs filesystem"
# make sure dest exists # make sure dest exists
@ -223,56 +225,58 @@ elif $fromssh ; then
done done
exit exit
fi fi
# discard anything before a colon to get the fs # discard anything before a colon to get the fs
srcfs="${src#*:}" srcfs="${src#*:}"
destfs="${dest#*:}" destfs="${dest#*:}"
# iff there is a colon, discard everything after it to get the host # iff there is a colon, discard everything after it to get the host
[[ $src =~ : ]] && srchost="${src%:*}" [[ $src =~ : ]] && srchost="${src%:*}"
[[ $dest =~ : ]] && desthost="${dest%:*}" [[ $dest =~ : ]] && desthost="${dest%:*}"
# get the last src component # get the last src component
srcbase="${srcfs##*/}" srcbase="${srcfs##*/}"
# ensure the destination fs exists before proceeding # ensure the destination fs exists before proceeding
if [[ $(ZFS "$desthost" list -H -o name "$destfs" 2>/dev/null) != $destfs ]] ; then if [[ $(ZFS "$desthost" list -H -o name "$destfs" 2>/dev/null) != $destfs ]] ; then
die 1 "destination fs '$destfs' doesn't exist" die 1 "destination fs '$destfs' doesn't exist"
fi fi
### ###
### create new snapshot on src ### create new snapshot on src
### ###
cur="$srcfs@${tag}_$date" cur="$srcfs@${tag}_$date"
ZFS "$srchost" snapshot -r "$cur" || die $? "zfs snapshot failed" ZFS "$srchost" snapshot -r "$cur" || die $? "zfs snapshot failed"
### ###
### get newest snapshot on dest - it must exist on src ### get newest snapshot on dest - it must exist on src
### ###
last="$(ZFS "$desthost" list -d 1 -t snapshot -H -S creation -o name $destfs/$srcbase | head -n1 | cut -f2 -d@)" last="$(ZFS "$desthost" list -d 1 -t snapshot -H -S creation -o name $destfs/$srcbase | head -n1 | cut -f2 -d@)"
### ###
### send & receive ### send & receive
### ###
# 1st time: send full snapshot # 1st time: send full snapshot
if [[ -z $last ]] ; then if [[ -z $last ]] ; then
log "sending full recursive snapshot from $src to $dest" log "sending full recursive snapshot from $src to $dest"
ZFS "$srchost" send $send_opts $reinit "$cur" | ZFS "$desthost" receive $recv_opts -Fue "$destfs" || die $? "zfs full send failed" ZFS "$srchost" send $send_opts $reinit "$cur" | ZFS "$desthost" receive $recv_opts -Fue "$destfs" || die $? "zfs full send failed"
# special case: tagged snapshots exist on dest, but src has rotated through all # special case: tagged snapshots exist on dest, but src has rotated through all
elif ! ZFS "$srchost" list $srcfs@$last &>/dev/null ; then elif ! ZFS "$srchost" list $srcfs@$last &>/dev/null ; then
die 1 "no incremental path from from $src to $dest" die 1 "no incremental path from from $src to $dest"
# normal case: send incremental # normal case: send incremental
else else
log "sending incremental snapshot from $src to $dest (${last#${tag}_}..${cur#*@${tag}_})" log "sending incremental snapshot from $src to $dest (${last#${tag}_}..${cur#*@${tag}_})"
ZFS "$srchost" send $send_opts -R $intermediate "$last" "$cur" | ZFS "$desthost" receive $recv_opts -Fue "$destfs" || die $? "zfs incremental send failed" ZFS "$srchost" send $send_opts -R $intermediate "$last" "$cur" | ZFS "$desthost" receive $recv_opts -Fue "$destfs" || die $? "zfs incremental send failed"
fi fi
### ###
### clean up old snapshots ### clean up old snapshots
### ###
for snap in $(ZFS "$srchost" list -d 1 -t snapshot -H -S creation -o name $srcfs \ for snap in $(ZFS "$srchost" list -d 1 -t snapshot -H -S creation -o name $srcfs \
| grep -F "@${tag}_" | cut -f2 -d@ | tail -n+$((keep+1)) ) ; | grep -F "@${tag}_" | cut -f2 -d@ | tail -n+$((keep+1)) ) ;
do do
ZFS "$srchost" destroy -r $srcfs@$snap ZFS "$srchost" destroy -r $srcfs@$snap
done done
) 9>/var/lock/bashclub-zfs.lock