获取使用 apt 安装的原始文件和当前文件之间的差异更改

获取使用 apt 安装的原始文件和当前文件之间的差异更改

php5-fpm使用安装包apt;然后我对 PHP 配置文件做了一些更改。

现在我将获得原始文件版本(安装的软件包的版本)和当前版本(由我修改的版本)之间的差异。怎么做?

答案1

尝试这样的事情:

# exit on failure
set -e

package=php5-fpm
mkdir $package
cd $package

# you could also get the file from a package mirror if you have
#  an older version of apt-get that doesn't support 'download' 
#  or if you would like more control over what package version
#  you are downloading.
# (e.g. http://archive.ubuntu.com/ubuntu/pool/main/)
apt-get download $package

# deb package files are ar archives
ar vx ${package}*.deb
# containing some compressed tar archives
tar xzf data.tar.gz
# now you have the files

# you can get diffs for all of the files in etc if you would like
find etc -type f |
while read file ; do
    diff $file /$file
done

正如其他人所建议的,一定要将您的配置文件置于修订控制之下。这样,您就可以准确地看到您更改的内容以及更改的时间。

答案2

等目录

为了跟踪/etc目录的更改,您可以按照 @Anthon 的建议进行操作,并使用 git、subversion、mercurial 等来对该目录进行版本控制。您还可以使用诸如等等管理员。有一个教程这里这里

etckeeper 是一个工具集合,可让 /etc 存储在 git、mercurial、bazaar 或 darcs 存储库中。它与 apt 挂钩,在软件包升级期间自动提交对 /etc 所做的更改。它跟踪 git 通常不支持的文件元数据,但这对于 /etc 很重要,例如/etc/shadow.它非常模块化和可配置,而且如果您了解版本控制的基础知识,那么使用起来也很简单。

包文件

据我所知,apt没有办法检查磁盘上的文件与实际中的文件。实际上用于管理文件的工具.deb也没有。dpkgapt

但是您可以使用诸如debsums为了比较您已安装的某些文件,它仅查看文件中内容.deb与系统磁盘上内容的校验和 (md5sum)。

看到这个服务器故障问题有关校验debsum和的更多详细信息dpkg,以及此阿斯库本图问题

debsum例子

% debsums openssh-server
/usr/lib/openssh/sftp-server                                                  OK
/usr/sbin/sshd                                                                OK
/usr/share/lintian/overrides/openssh-server                                   OK
/usr/share/man/man5/sshd_config.5.gz                                          OK
/usr/share/man/man8/sshd.8.gz                                                 OK
/usr/share/man/man8/sftp-server.8.gz                                          OK

答案3

我编写了以下简单的脚本来自动从正确的 Debian 软件包中检索原始文件,并将当前文件与其进行比较:https://a3nm.net/git/mybin/tree/debdiffconf

使用方法如下:debdiffconf FILE

#!/bin/bash

# Usage: debdiffconf.sh FILE
# Produce on stdout diff of FILE against the first installed Debian package
# found that provides it.
# Returns the exit code of diff if everything worked, 3 or 4 otherwise.

# https://stackoverflow.com/a/4785518
command -v apt >/dev/null 2>&1 || {
  echo "apt not found, this is probably not a Debian system. Aborting." >&2;
  exit 4; }
command -v apt-file >/dev/null 2>&1 || {
  echo "Please install apt-file: sudo apt install apt-file. Aborting." >&2;
  exit 4; }
command -v realpath >/dev/null 2>&1 || {
  echo "Please install realpath: sudo apt install realpath. Aborting." >&2;
  exit 4; }

FILE=$(realpath -m "$1")
while read PACKAGE
do
  # verify from first installed package
  if dpkg-query -W --showformat='${Status}\n' | grep installed > /dev/null
  then
    DIR=$(mktemp -d)
    cd "$DIR"
    echo "Trying $PACKAGE..." >&2
    apt download "$PACKAGE" >&2
    # downloaded archive is the only file present...
    ARCHIVE=$(ls)
    mkdir contents
    # extract entire archive
    dpkg-deb -x "$ARCHIVE" contents/ >&2
    if [ -f "contents$FILE" ]
    then
      # package contained required file
      diff "contents$FILE" "$FILE"
      RET=$?
      # cleanup
      cd
      rm -Rf "$DIR"
      # exit entire script as this is the main shell
      # with the return code from diff
      exit $RET
    else
      # cleanup
      cd
      rm -Rf "$DIR"
    fi
  fi
done < <(apt-file -l search "$FILE")
# if we are here, it means we have found no suitable package
echo "Could not find original package for $FILE" >&2
exit 3

答案4

如果您想查看原始文件和安装文件之间的差异php.ini,请使用

diff -W COLUMNS --suppress-common-lines -y /usr/share/php5/php.ini-development /etc/php5/apache2/php.ini -W $COLUMNS

如果你不关心注释行,请将其插入

| egrep -v '^;.*<$|\s*>.;.*|;.*\|.;'

相关内容