xref: /freebsd/usr.sbin/periodic/etc/daily/223.backup-zfs (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1#!/bin/sh
2
3# $FreeBSD$
4# Created by: Miroslav Lachman <000.fbsd@quip.cz>
5
6# Backup of zpool list, zfs list, zpool properties and zfs properties
7# for each filesystem. 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_zfs_verbose" in
42	[Yy][Ee][Ss]) show="YES"
43esac
44
45case "$daily_backup_zfs_enable" in
46	[Yy][Ee][Ss])
47
48    zpools=$(zpool list $daily_backup_zpool_list_flags)
49
50	if [ -z "$zpools"  ]; then
51		echo 'daily_backup_zfs_enable is set to YES but no zpools found.'
52		rc=2
53	else
54		echo ""
55		echo "Backup of ZFS information for all imported pools";
56
57		echo "$zpools" > "$bak_dir/zpool_list.tmp"
58		rotate "zpool_list" $show
59
60		zfs list $daily_backup_zfs_list_flags > "$bak_dir/zfs_list.tmp"
61		rotate "zfs_list" $show
62	fi
63	;;
64	*)  rc=0;;
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"
72
73    zpool get $daily_backup_zpool_get_flags > "$bak_dir/zpool_props.tmp"
74    rotate "zpool_props"
75    ;;
76esac
77
78exit $rc
79