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