1#!/bin/sh
2#
3# $FreeBSD: releng/10.2/etc/periodic/daily/220.backup-pkgdb 226470 2011-10-17 14:33:41Z se $
4#
5
6# If there is a global system configuration file, suck it in.
7#
8if [ -r /etc/defaults/periodic.conf ]
9then
10    . /etc/defaults/periodic.conf
11    source_periodic_confs
12fi
13
14rc=0
15
16case "$daily_backup_pkgdb_enable" in
17    [Yy][Ee][Ss])
18	bak="${daily_backup_pkgdb_dir:-/var/backups}"
19	bak_file="${bak}/pkgdb.bak.tbz"
20
21	pkg_dbdir=`make -f/usr/share/mk/bsd.port.mk -V PKG_DBDIR 2>/dev/null` ||
22	  pkg_dbdir=/var/db/pkg
23
24	if [ ! -d "$bak" ]
25	then
26	    install -d -o root -g wheel -m 750 $bak || {
27		echo '$daily_backup_pkgdb_enable is enabled but' \
28		    "$daily_backup_pkgdb_dir doesn't exist" ;
29		exit 2 ; }
30	fi
31
32	echo ''
33	echo 'Backing up package db directory:'
34
35	new_bak_file=`mktemp ${bak_file}-XXXXX`
36
37	if tar -cjHf "${new_bak_file}" "$pkg_dbdir" 2>/dev/null; then
38	    chmod 644 "${new_bak_file}"
39
40	    if [ -e "${bak_file}.2" -a -e "${bak_file}" ]; then
41		unlink "${bak_file}.2"
42		mv "${bak_file}" "${bak_file}.2"
43	    fi
44	    [ -e "${bak_file}" ] && mv "${bak_file}" "${bak_file}.2"
45	    mv "${new_bak_file}" "${bak_file}"
46	else
47	    rc=3
48	fi ;;
49esac
50
51exit $rc
52