]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - etc/periodic/daily/800.scrub-zfs
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.git] / etc / periodic / daily / 800.scrub-zfs
1 #!/bin/sh
2 #
3 # $FreeBSD$
4 #
5
6 # If there is a global system configuration file, suck it in.
7 #
8 if [ -r /etc/defaults/periodic.conf ]
9 then
10     . /etc/defaults/periodic.conf
11     source_periodic_confs
12 fi
13
14 : ${daily_scrub_zfs_default_threshold=30}
15
16 case "$daily_scrub_zfs_enable" in
17     [Yy][Ee][Ss])
18         echo
19         echo 'Scrubbing of zfs pools:'
20
21         if [ -z "${daily_scrub_zfs_pools}" ]; then
22                 daily_scrub_zfs_pools="$(zpool list -H -o name)"
23         fi
24
25         for pool in ${daily_scrub_zfs_pools}; do
26                 # sanity check
27                 zpool list ${pool} >/dev/null 2>&1
28                 if [ $? -ne 0 ]; then
29                         echo "   WARNING: pool '${pool}' specified in"
30                         echo "            '/etc/periodic.conf:daily_scrub_zfs_pools'"
31                         echo "            does not exist"
32                         continue
33                 fi
34
35                 # successful only if there is at least one pool to scrub
36                 rc=0
37
38                 # determine how many days shall be between scrubs
39                 eval _pool_threshold=\${daily_scrub_zfs_${pool}_threshold}
40                 if [ -z "${_pool_threshold}" ];then
41                         _pool_threshold=${daily_scrub_zfs_default_threshold}
42                 fi
43
44                 _last_scrub=$(zpool history ${pool} | \
45                     egrep "^[0-9\.\:\-]{19} zpool scrub ${pool}\$" | tail -1 |\
46                     cut -d ' ' -f 1)
47                 if [ -z "${_last_scrub}" ]; then
48                         # creation time of the pool if no scrub was done
49                         _last_scrub=$(zpool history ${pool} | \
50                             sed -ne '2s/ .*$//p')
51                 fi
52
53                 # Now minus last scrub (both in seconds) converted to days.
54                 _scrub_diff=$(expr -e \( $(date +%s) - \
55                     $(date -j -f %F.%T ${_last_scrub} +%s) \) / 60 / 60 / 24)
56                 if [ ${_scrub_diff} -lt ${_pool_threshold} ]; then
57                         echo "   skipping scrubbing of pool '${pool}':"
58                         echo "      last scrubbing is ${_scrub_diff} days ago, threshold is set to ${_pool_threshold} days"
59                         continue
60                 fi
61
62                 _status="$(zpool status ${pool} | grep scrub:)"
63                 case "${_status}" in
64                         *"scrub in progress"*)
65                                 echo "   scrubbing of pool '${pool}' already in progress, skipping:"
66                                 ;;
67                         *"none requested"*)
68                                 echo "   starting first scrub (since reboot) of pool '${pool}':"
69                                 zpool scrub ${pool}
70                                 ;;
71                         *)
72                                 echo "   starting scrub of pool '${pool}':"
73                                 zpool scrub ${pool}
74                                 ;;
75                 esac
76
77                 echo "      consult 'zpool status ${pool}' for the result"
78         done
79         ;;
80
81     *)
82         rc=0
83         ;;
84 esac
85
86 exit $rc