commit 9905633c480e478151865b3eaeb9b804d3fe567a
parent 7432ad423080adab32c9ee0321b4c2e912897465
Author: Christos Margiolis <christos@margiolis.net>
Date: Sun, 3 Apr 2022 22:52:05 +0300
more fixes
Diffstat:
M | backup | | | 22 | ++++++++++++++-------- |
1 file changed, 14 insertions(+), 8 deletions(-)
diff --git a/backup b/backup
@@ -14,7 +14,7 @@ err()
pool_exists()
{
- local foo=$(zpool list | sed 1d | awk '{print $1}' | grep -x "${1}")
+ local foo=$(zpool list -o name | sed 1d | grep -x "${1}")
test -z "${foo}" && err "pool ${1} does not exist"
}
@@ -44,10 +44,22 @@ pool_exists "${src_pool}"
pool_exists "${dst_pool}"
new_snap="$(date +%Y%m%d)"
+src_last_snap=$(last_snap "${src_pool}")
dst_last_snap=$(last_snap "${dst_pool}")
nsnaps=$(zfs list -t snapshot -o name ${dst_pool} | sed 1d | wc -l)
-# Overwrite snapshot if it already exists when the -r option is passed,
+# Make appropriate checks before creating new snapshot.
+# If the snapshot already exists, either renew it or exit.
+if [ "${src_last_snap}" = "${new_snap}" ]; then
+ if [ ${f_snap} -eq 1 ]; then
+ zfs destroy -r ${src_pool}@${new_snap}
+ else
+ err "snapshot ${new_snap} exists already"
+ fi
+fi
+zfs snapshot -r ${src_pool}@${new_snap}
+
+# Overwrite dst snapshot if it already exists when the -r option is passed,
# otherwise, exit. Also make sure there are more than 1 snapshots before we
# delete anything.
if [ ! ${nsnaps} -le 1 ] && [ "${dst_last_snap}" = "${new_snap}" ]; then
@@ -55,12 +67,6 @@ if [ ! ${nsnaps} -le 1 ] && [ "${dst_last_snap}" = "${new_snap}" ]; then
zfs destroy -r ${dst_pool}@${new_snap}
fi
-# Create new src snapshots when the -r option is passed.
-if [ ${f_snap} -eq 1 ]; then
- zfs destroy -r ${src_pool}@${new_snap}
- zfs snapshot -r ${src_pool}@${new_snap}
-fi
-
if [ ${f_incr} -eq 1 ]; then
zfs send -RI ${src_pool}@${last_snap} ${src_pool}@${new_snap} | pv | zfs recv -Fu ${dst_pool}
else