3 # Copyright © 2014, 2017-2018, 2020-2021 Guillem Jover <guillem@debian.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
18 PROGNAME
=$
(basename "$0")
19 ADMINDIR
=/var
/lib
/dpkg
20 BACKUPSDIR
=/var
/backups
23 PKGDATADIR_DEFAULT
=src
24 PKGDATADIR
="${DPKG_DATADIR:-$PKGDATADIR_DEFAULT}"
27 # shellcheck source=src/sh/dpkg-error.sh
28 .
"$PKGDATADIR/sh/dpkg-error.sh"
32 while [ $# -ne 0 ]; do
35 ROTATE
="${1#--rotate=}"
41 # Check for required commands availability.
42 for cmd
in "$TAR" savelog
; do
43 if ! command -v "$cmd" >/dev
/null
; then
44 error
"cannot find required program '$cmd'"
50 # Backup the N last versions of dpkg databases containing user data.
51 if cd $BACKUPSDIR ; then
52 # We backup all relevant database files if any has changed, so that
53 # the rotation number always contains an internally consistent set.
55 dbfiles
="arch status diversions statoverride"
56 for db
in $dbfiles ; do
57 if ! [ -s "dpkg.${db}.0" ] && ! [ -s "$dbdir/$db" ]; then
58 # Special case the files not existing or being empty as being equal.
60 elif ! cmp -s "dpkg.${db}.0" "$dbdir/$db"; then
65 if [ "$dbchanged" = "yes" ] ; then
66 for db
in $dbfiles ; do
67 if [ -e "$dbdir/$db" ]; then
68 cp -p "$dbdir/$db" "dpkg.$db"
72 savelog
-c "$ROTATE" "dpkg.$db" >/dev
/null
76 # The alternatives database is independent from the dpkg database.
79 # XXX: Ideally we'd use --warning=none instead of discarding stderr, but
80 # as of GNU tar 1.27.1, it does not seem to work reliably (see #749307).
81 if ! test -e ${dbalt}.
tar.0 ||
82 ! $TAR -df ${dbalt}.
tar.0 -C $dbdir $dbalt >/dev
/null
2>&1 ;
84 $TAR -cf ${dbalt}.
tar -C $dbdir $dbalt >/dev
/null
2>&1
85 savelog
-c "$ROTATE" ${dbalt}.
tar >/dev
/null