xref: /freebsd-13-stable/usr.sbin/periodic/etc/daily/310.accounting (revision 023fc80ee38a117fa65b2ccb2abf8bdc7dbd6fd9)
1#!/bin/sh
2#
3#
4
5# If there is a global system configuration file, suck it in.
6#
7if [ -r /etc/defaults/periodic.conf ]
8then
9    . /etc/defaults/periodic.conf
10    source_periodic_confs
11fi
12
13case "$daily_accounting_enable" in
14    [Yy][Ee][Ss])
15	if [ ! -f /var/account/acct ]
16	then
17	    echo '$daily_accounting_enable is set but /var/account/acct' \
18		"doesn't exist"
19	    rc=2
20	elif [ -z "$daily_accounting_save" ]
21	then
22	    echo '$daily_accounting_enable is set but ' \
23		'$daily_accounting_save is not'
24	    rc=2
25	else
26	    echo ""
27	    echo "Rotating accounting logs and gathering statistics:"
28
29	    cd /var/account
30	    rc=0
31
32	    n=$(( $daily_accounting_save - 1 ))
33	    for f in acct.*; do
34	    	case "$f" in acct.\*) continue ;; esac	# No files match
35	    	m=${f%.gz} ; m=${m#acct.}
36		[ $m -ge $n ] && { rm $f || rc=3; }
37	    done
38
39	    m=$n
40	    n=$(($n - 1))
41	    while [ $n -ge 0 ]
42	    do
43		[ -f acct.$n.gz ] && { mv -f acct.$n.gz acct.$m.gz || rc=3; }
44		[ -f acct.$n ] &&    { mv -f acct.$n acct.$m || rc=3; }
45		m=$n
46		n=$(($n - 1))
47	    done
48
49	    /etc/rc.d/accounting onerotate_log || rc=3
50
51	    rm -f acct.merge && cp acct.0 acct.merge || rc=3
52	    sa -s $daily_accounting_flags /var/account/acct.merge || rc=3
53	    rm acct.merge
54
55	    case "$daily_accounting_compress" in
56		[Yy][Ee][Ss])
57		    gzip -f acct.0 || rc=3;;
58	    esac
59	fi;;
60
61    *)  rc=0;;
62esac
63
64exit $rc
65