scripts/mk: On dpkg-build-api >= 1 include buildtools.mk in default.mk
[dpkg.git] / src / dpkg-db-backup.sh
blobdbb0d6ca01de88b164a1580aaefddf94123fa612
1 #!/bin/sh
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
21 ROTATE=7
23 PKGDATADIR_DEFAULT=src
24 PKGDATADIR="${DPKG_DATADIR:-$PKGDATADIR_DEFAULT}"
26 # shellcheck source=src/sh/dpkg-error.sh
27 . "$PKGDATADIR/sh/dpkg-error.sh"
29 setup_colors
31 while [ $# -ne 0 ]; do
32 case "$1" in
33 --rotate=*)
34 ROTATE="${1#--rotate=}"
36 esac
37 shift
38 done
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'"
45 done
47 dbdir="$ADMINDIR"
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.
53 dbchanged=no
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.
58 continue
59 elif ! cmp -s "dpkg.${db}.0" "$dbdir/$db"; then
60 dbchanged=yes
61 break
63 done
64 if [ "$dbchanged" = "yes" ] ; then
65 for db in $dbfiles ; do
66 if [ -e "$dbdir/$db" ]; then
67 cp -p "$dbdir/$db" "dpkg.$db"
68 else
69 touch "dpkg.$db"
71 savelog -c "$ROTATE" "dpkg.$db" >/dev/null
72 done
75 # The alternatives database is independent from the dpkg database.
76 dbalt=alternatives
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 ;
82 then
83 tar -cf ${dbalt}.tar -C $dbdir $dbalt >/dev/null 2>&1
84 savelog -c "$ROTATE" ${dbalt}.tar >/dev/null