arch: Restrict kopensolaris ports to amd64 and i386
[dpkg.git] / src / dpkg-db-backup.sh
blob619f489ea5341b1c24cc8ebebfde00ad0a7521f9
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}"
25 TAR="${TAR:-tar}"
27 # shellcheck source=src/sh/dpkg-error.sh
28 . "$PKGDATADIR/sh/dpkg-error.sh"
30 setup_colors
32 while [ $# -ne 0 ]; do
33 case "$1" in
34 --rotate=*)
35 ROTATE="${1#--rotate=}"
37 esac
38 shift
39 done
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'"
46 done
48 dbdir="$ADMINDIR"
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.
54 dbchanged=no
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.
59 continue
60 elif ! cmp -s "dpkg.${db}.0" "$dbdir/$db"; then
61 dbchanged=yes
62 break
64 done
65 if [ "$dbchanged" = "yes" ] ; then
66 for db in $dbfiles ; do
67 if [ -e "$dbdir/$db" ]; then
68 cp -p "$dbdir/$db" "dpkg.$db"
69 else
70 touch "dpkg.$db"
72 savelog -c "$ROTATE" "dpkg.$db" >/dev/null
73 done
76 # The alternatives database is independent from the dpkg database.
77 dbalt=alternatives
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 ;
83 then
84 $TAR -cf ${dbalt}.tar -C $dbdir $dbalt >/dev/null 2>&1
85 savelog -c "$ROTATE" ${dbalt}.tar >/dev/null