git: 4b1d3a30914e - main - daily/223.backup-zfs: improve daily_backup_zfs_verbose behaviour

From: Warner Losh <imp_at_FreeBSD.org>
Date: Fri, 02 Feb 2024 18:28:12 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=4b1d3a30914e176e9fd2c363db81c26124a8ee30

commit 4b1d3a30914e176e9fd2c363db81c26124a8ee30
Author:     Lexi Winter <lexi@le-Fay.ORG>
AuthorDate: 2024-02-02 18:18:54 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-02-02 18:27:05 +0000

    daily/223.backup-zfs: improve daily_backup_zfs_verbose behaviour
    
    - 223.backup-zfs would previously honour the daily_backup_zfs_verbose
      flag for zfs/zpool list, but not for the properties list.  fix it to
      show a diff for both of these if requested.
    
    - if daily_backup_zfs_verbose was disabled, 223.backup-zfs would still
      set rc=1 if the backup files changed, which caused periodic(8) to send
      a useless email even if daily_show_success=NO was set.
    
      change this so that it only sets rc=1 if diff output is enabled, i.e.
      the output is actually useful to the admin.
    
    MFC after:      2 weeks
    Reviewed by:    imp
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/1091
---
 usr.sbin/periodic/etc/daily/223.backup-zfs | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/usr.sbin/periodic/etc/daily/223.backup-zfs b/usr.sbin/periodic/etc/daily/223.backup-zfs
index a49bbb2eaa04..e76421220a0b 100755
--- a/usr.sbin/periodic/etc/daily/223.backup-zfs
+++ b/usr.sbin/periodic/etc/daily/223.backup-zfs
@@ -13,6 +13,7 @@ then
 fi
 
 bak_dir=/var/backups
+rc=0
 
 rotate() {
 	base_name=$1
@@ -20,12 +21,13 @@ rotate() {
 	file="$bak_dir/$base_name"
 
 	if [ -f "${file}.bak" ] ; then
-		rc=0
 		if cmp -s "${file}.bak" "${file}.tmp"; then
 			rm "${file}.tmp"
 		else
-			rc=1
-			[ -n "$show_diff" ] && diff ${daily_diff_flags} "${file}.bak" "${file}.tmp"
+			if [ -n "$show_diff" ]; then
+				rc=1
+				diff ${daily_diff_flags} "${file}.bak" "${file}.tmp"
+			fi
 			mv "${file}.bak" "${file}.bak2" || rc=3
 			mv "${file}.tmp" "${file}.bak" || rc=3
 		fi
@@ -36,6 +38,7 @@ rotate() {
 	fi
 }
 
+show=""
 case "$daily_backup_zfs_verbose" in
 	[Yy][Ee][Ss]) show="YES"
 esac
@@ -43,9 +46,9 @@ esac
 case "$daily_backup_zfs_enable" in
 	[Yy][Ee][Ss])
 
-    zpools=$(zpool list $daily_backup_zpool_list_flags)
+	zpools=$(zpool list $daily_backup_zpool_list_flags)
 
-	if [ -z "$zpools"  ]; then
+	if [ -z "$zpools" ]; then
 		echo 'daily_backup_zfs_enable is set to YES but no zpools found.'
 		rc=2
 	else
@@ -59,18 +62,17 @@ case "$daily_backup_zfs_enable" in
 		rotate "zfs_list" $show
 	fi
 	;;
-	*)  rc=0;;
 esac
 
 case "$daily_backup_zfs_props_enable" in
-    [Yy][Ee][Ss])
+	[Yy][Ee][Ss])
 
-    zfs get $daily_backup_zfs_get_flags > "$bak_dir/zfs_props.tmp"
-    rotate "zfs_props"
+	zfs get $daily_backup_zfs_get_flags > "$bak_dir/zfs_props.tmp"
+	rotate "zfs_props" $show
 
-    zpool get $daily_backup_zpool_get_flags > "$bak_dir/zpool_props.tmp"
-    rotate "zpool_props"
-    ;;
+	zpool get $daily_backup_zpool_get_flags > "$bak_dir/zpool_props.tmp"
+	rotate "zpool_props" $show
+	;;
 esac
 
 exit $rc