In an earlier post I wrote how to output a list the currently installed applications under Debian (and distributions based upon it). This will now be extended into a script that can be run each hour to record changes to the installed applications. Now, it should be said from the beginning that this information may already be stored in the file /var/log/apt/term.log but it can be a challenge to follow what it happening.
The following script stores a snapshot of the installed applications and then makes a diff against that on a period basis (e.g. by running it as an hourly cron job). If there is a difference, the changes are saved to a time stamped file and a new snapshot is taken. All files are placed in the same log directory as apt normally uses (i.e. /var/log/apt). The downside of this method is that changes will be recorded with a granularity of an hour but usually that is not an issue as the reason for writing this was to keep an automated record of changes to the system.
#!/bin/bash
folder='/var/log/apt/'
installed=$folder'current'
if [ ! -e $installed ]; then
echo "Creating initial file"
dpkg-query -W -f='${Package}\n' > $installed
cp $installed $folder'initial'
fi
# Compare package list against current
dpkg-query -W -f='${Package}\n' | diff $installed - \
| grep -e '^[(<|>)]' > /dev/null
if [ $? -eq 0 ]; then
# The set of installed packages has changed. Save the delta to a
# file and save the new snapshot
filename=$folder`date +%Y-%m-%d_%H-%M-%S`
dpkg-query -W -f='${Package}\n' | diff $installed - \
| grep -e '^[(<|>)]' > $filename
dpkg-query -W -f='${Package}\n' > $installed
fi;
exit 0