xref: /freebsd/usr.sbin/periodic/etc/daily/222.backup-gmirror (revision 15e35d058cff847d1a7bc671fcf48b99f5d2f73b)
1#!/bin/sh
2# Created by: Miroslav Lachman <000.fbsd@quip.cz>
3
4# Backup output from `gmirror list`, which provides detailed information
5# of all gmirrors. The backup will be stored in /var/backups/.
6
7# If there is a global system configuration file, suck it in.
8#
9if [ -r /etc/defaults/periodic.conf ]
10then
11	. /etc/defaults/periodic.conf
12	source_periodic_confs
13fi
14
15bak_dir=/var/backups
16
17rotate() {
18	base_name=$1
19	show_diff=$2
20	file="$bak_dir/$base_name"
21
22	if [ -f "${file}.bak" ]; then
23		rc=0
24		if cmp -s "${file}.bak" "${file}.tmp"; then
25			rm "${file}.tmp"
26		else
27			rc=1
28			[ -n "$show_diff" ] && diff ${daily_diff_flags} "${file}.bak" "${file}.tmp"
29			mv "${file}.bak" "${file}.bak2" || rc=3
30			mv "${file}.tmp" "${file}.bak" || rc=3
31		fi
32	else
33		rc=1
34		mv "${file}.tmp" "${file}.bak" || rc=3
35		[ -n "$show_diff" ] && cat "${file}.bak"
36	fi
37}
38
39case "$daily_backup_gmirror_verbose" in
40	[Yy][Ee][Ss]) show="YES"
41esac
42
43case "$daily_backup_gmirror_enable" in
44	[Yy][Ee][Ss])
45
46	gmirrors=$(gmirror status 2> /dev/null | \
47		awk '$1 ~ /^mirror\// { sub(/mirror\//, ""); print $1 }')
48
49	if [ -z "$gmirrors"  ]; then
50        echo ""
51		echo "daily_backup_gmirror_enable is set to YES but no gmirrors found."
52		rc=2
53	else
54		echo ""
55		echo "Backup of gmirror information for:";
56
57		for m in ${gmirrors}; do
58			echo "$m"
59			safe_name=$(echo "gmirror.${m}" | tr -cs ".[:alnum:]\n" "_")
60			if ! gmirror status -s "${m}" | grep -F -v "COMPLETE"; then
61				gmirror list "${m}" > "$bak_dir/$safe_name.tmp"
62				rotate "$safe_name" $show
63			fi
64		done
65	fi
66	;;
67	*)  rc=0;;
68esac
69
70exit $rc
71