xref: /freebsd/usr.sbin/periodic/etc/daily/221.backup-gpart (revision 15e35d058cff847d1a7bc671fcf48b99f5d2f73b)
1#!/bin/sh
2## Created by: Miroslav Lachman <000.fbsd@quip.cz>
3
4## Backup of disk partitions layout, useful for gpart restore.
5## Data are stored on local filesystem, in /var/backup.
6## It is recommended to copy those files to off-site storage.
7
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 ${daily_diff_flags} "${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_gpart_verbose" in
42	[Yy][Ee][Ss]) show="YES"
43esac
44
45case "$daily_backup_gpart_enable" in
46	[Yy][Ee][Ss])
47
48	echo ""
49	echo "Dump of kern.geom.conftxt:";
50	sysctl -n kern.geom.conftxt > "$bak_dir/kern.geom.conftxt.tmp"
51	rotate "kern.geom.conftxt" $show
52
53	gpart_devs=$(gpart show | awk '$1 == "=>" { print $4 }')
54	if [ -n "$daily_backup_gpart_exclude" ]; then
55		gpart_devs=$(echo "${gpart_devs}" | grep -E -v "${daily_backup_gpart_exclude}")
56	fi
57
58	if [ -z "$gpart_devs"  ]; then
59		echo '$daily_backup_gpart_enable is set but no disk probed by kernel.' \
60		"perhaps NFS diskless client."
61		rc=2
62	else
63		echo ""
64		echo "Backup of partitions information for:";
65
66		for d in ${gpart_devs}; do
67			echo "$d"
68			safe_name=$(echo "gpart.${d}" | tr -cs ".[:alnum:]\n" "_")
69			gpart backup "$d" > "$bak_dir/$safe_name.tmp"
70			rotate "$safe_name" $show
71		done
72
73		gpart_show=$(gpart show -p)
74		boot_part=$(echo "$gpart_show" | awk '$4 ~ /(bios|freebsd)-boot/ { print $3 }')
75		if [ -n "$boot_part" ]; then
76			echo ""
77			echo "Backup of boot partition content:"
78			for b in ${boot_part}; do
79				echo "$b"
80				safe_name=$(echo "boot.${b}" | tr -cs ".[:alnum:]\n" "_")
81				dd if="/dev/${b}" of="$bak_dir/$safe_name.tmp" 2> /dev/null
82				rotate "$safe_name"
83			done
84		fi
85
86		mbr_part=$(echo "$gpart_show" | awk '$1 == "=>" && $5 == "MBR" { print $4 }')
87		if [ -n "$mbr_part" ]; then
88			echo ""
89			echo "Backup of MBR record:"
90			for mb in ${mbr_part}; do
91				echo "$mb"
92				safe_name=$(echo "boot.${mb}" | tr -cs ".[:alnum:]\n" "_")
93				dd if="/dev/${mb}" of="$bak_dir/$safe_name.tmp" bs=512 count=1 2> /dev/null
94				rotate "$safe_name"
95			done
96		fi
97
98	fi
99	;;
100
101	*)  rc=0
102	;;
103esac
104
105case "$daily_backup_efi_enable" in
106    [Yy][Ee][Ss])
107
108    efi_part=$(gpart show -p | awk '$4 ~ /efi/ {print $3}')
109    if [ -n "$efi_part" ]; then
110        echo ""
111        echo "Backup of EFI partition content:"
112        for efi in ${efi_part}; do
113            echo "$efi"
114            safe_name=$(echo "efi.${efi}" | tr -cs ".[:alnum:]\n" "_")
115            dd if="/dev/${efi}" of="$bak_dir/$safe_name.tmp" 2> /dev/null
116            rotate "$safe_name"
117        done
118    fi
119    ;;
120esac
121
122exit $rc
123