Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 8 Aug 2009 09:32:27 -0800
From:      Mel Flynn <mel.flynn+fbsd.questions@mailing.thruhere.net>
To:        freebsd-questions@freebsd.org
Cc:        Volodymyr Kostyrko <c.kworr@gmail.com>, "b. f." <bf1783@googlemail.com>
Subject:   Re: Recovering loss of /var/db/pkg ?
Message-ID:  <200908080932.27489.mel.flynn%2Bfbsd.questions@mailing.thruhere.net>
In-Reply-To: <d873d5be0908080402n47e8ab48o4ec5c1c096005f0c@mail.gmail.com>
References:  <d873d5be0908080402n47e8ab48o4ec5c1c096005f0c@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Saturday 08 August 2009 03:02:05 b. f. wrote:

> 2) write a script to get the names of all files that belonged to ports
> and swing through a ports tree, associating the files with ports via
> the pkg-plist and PLIST_FILES variables; or

This is quite complex, time consuming and prone to error the more ports tree 
and installed ports are out of sync. Either way, you will want to compare 
files against the generate-plist target (and the resulting contents of 
$TMPPLIST), as more and more ports use dynamic package list features.

To prevent this from happening in the future, I've written a small periodic 
script that you can put in /usr/local/etc/periodic/daily and backs up the list 
of origins of installed ports.

On the first run (or more to the point, if /var/backup/pkglist.prev doesn't 
exist), it will dump the current list. Otherwise, it will compare with the 
previous run and provide a diff if anything changed. So:
- if you set $daily_backup_pkglist_enable to YES in /etc/periodic.conf
- if you have daily reports mailed to an off-machine address
- and if you keep the first run and all diffs

You can recreate an accurate list of installed ports, when applying all diffs 
in sequence, even if you newfs'd /var on the machine.
-- 
Mel

#!/bin/sh
#
# $Coar: periodic/daily/203.backup-pkglist.sh,v 1.3 2009/08/08 17:04:41 mel 
Exp $
#

# If there is a global system configuration file, suck it in.
#
if [ -r /etc/defaults/periodic.conf ]
then
    . /etc/defaults/periodic.conf
    source_periodic_confs
fi

daily_backup_pkglist_enable=${daily_backup_pkglist_enable:-"NO"}
daily_backup_pkglist_dbdir=${daily_backup_pkglist_dbdir:-"/var/db/pkg"}

create_pkglist()
{
	local f
	f=$1

	for CFILE in ${daily_backup_pkglist_dbdir}/*/+CONTENTS; do
		sed -ne 's,^@comment ORIGIN:,,p' ${CFILE}
	done | sort > ${f}
}

case "$daily_backup_pkglist_enable" in
    [Yy][Ee][Ss])
	if [ ! -d ${daily_backup_pkglist_dbdir} ]
	then
	    echo '$daily_backup_pkglist_enable is enabled but' \
		"${daily_backup_pkglist_dbdir} doesn't exist"
	    rc=2
	else
	    bak=/var/backups
	    rc=0

	    echo ""
	    echo "Backing up list of package origins:"

	    create_pkglist $bak/pkglist.cur
	    if [ ! -f $bak/pkglist.prev ]
	    then
		echo "no $bak/pkglist.prev. Dumping full list for prosperity:"
		cat $bak/pkglist.cur
		cp -p $bak/pkglist.cur $bak/pkglist.prev
	    fi

	    if ! cmp -s $bak/pkglist.prev $bak/pkglist.cur
	    then
		[ $rc -lt 1 ] && rc=1
		echo "$host pkglist diffs:"
		diff -u $bak/pkglist.prev $bak/pkglist.cur
		mv $bak/pkglist.cur $bak/pkglist.prev
	    fi
	fi;;

    *)  rc=0;;
esac

exit $rc




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200908080932.27489.mel.flynn%2Bfbsd.questions>