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}"
26 # shellcheck source=src/sh/dpkg-error.sh
27 .
"$PKGDATADIR/sh/dpkg-error.sh"
31 while [ $# -ne 0 ]; do
34 ROTATE
="${1#--rotate=}"
40 # Check for required commands availability.
41 for cmd
in tar savelog
; do
42 if ! command -v $cmd >/dev
/null
; then
43 error
"cannot find required program '$cmd'"
49 # Backup the N last versions of dpkg databases containing user data.
50 if cd $BACKUPSDIR ; then
51 # We backup all relevant database files if any has changed, so that
52 # the rotation number always contains an internally consistent set.
54 dbfiles
="arch status diversions statoverride"
55 for db
in $dbfiles ; do
56 if ! [ -s "dpkg.${db}.0" ] && ! [ -s "$dbdir/$db" ]; then
57 # Special case the files not existing or being empty as being equal.
59 elif ! cmp -s "dpkg.${db}.0" "$dbdir/$db"; then
64 if [ "$dbchanged" = "yes" ] ; then
65 for db
in $dbfiles ; do
66 if [ -e "$dbdir/$db" ]; then
67 cp -p "$dbdir/$db" "dpkg.$db"
71 savelog
-c "$ROTATE" "dpkg.$db" >/dev
/null
75 # The alternatives database is independent from the dpkg database.
78 # XXX: Ideally we'd use --warning=none instead of discarding stderr, but
79 # as of GNU tar 1.27.1, it does not seem to work reliably (see #749307).
80 if ! test -e ${dbalt}.
tar.0 ||
81 ! tar -df ${dbalt}.
tar.0 -C $dbdir $dbalt >/dev
/null
2>&1 ;
83 tar -cf ${dbalt}.
tar -C $dbdir $dbalt >/dev
/null
2>&1
84 savelog
-c "$ROTATE" ${dbalt}.
tar >/dev
/null