xref: /freebsd/usr.sbin/periodic/etc/daily/223.backup-zfs (revision 725a9f47324d42037db93c27ceb40d4956872f3e)
1#!/bin/sh
2# Created by: Miroslav Lachman <000.fbsd@quip.cz>
3
4# Backup of zpool list, zfs list, zpool properties and zfs properties
5# for each filesystem. 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
16rc=0
17
18rotate() {
19	base_name=$1
20	show_diff=$2
21	file="$bak_dir/$base_name"
22
23	if [ -f "${file}.bak" ] ; then
24		if cmp -s "${file}.bak" "${file}.tmp"; then
25			rm "${file}.tmp"
26		else
27			if [ -n "$show_diff" ]; then
28				rc=1
29				diff ${daily_diff_flags} "${file}.bak" "${file}.tmp"
30			fi
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
41show=""
42case "$daily_backup_zfs_verbose" in
43	[Yy][Ee][Ss]) show="YES"
44esac
45
46case "$daily_backup_zfs_enable" in
47	[Yy][Ee][Ss])
48
49	zpools=$(zpool list $daily_backup_zpool_list_flags)
50
51	if [ -z "$zpools" ]; then
52		echo 'daily_backup_zfs_enable is set to YES but no zpools found.'
53		rc=2
54	else
55		echo ""
56		echo "Backup of ZFS information for all imported pools";
57
58		echo "$zpools" > "$bak_dir/zpool_list.tmp"
59		rotate "zpool_list" $show
60
61		zfs list $daily_backup_zfs_list_flags > "$bak_dir/zfs_list.tmp"
62		rotate "zfs_list" $show
63	fi
64	;;
65esac
66
67case "$daily_backup_zfs_props_enable" in
68	[Yy][Ee][Ss])
69
70	zfs get $daily_backup_zfs_get_flags > "$bak_dir/zfs_props.tmp"
71	rotate "zfs_props" $show
72
73	zpool get $daily_backup_zpool_get_flags > "$bak_dir/zpool_props.tmp"
74	rotate "zpool_props" $show
75	;;
76esac
77
78exit $rc
79