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