How can I backup the RPM database?
The Red Hat Package Manager (RPM) database is how Red Hat Enterprise Linux knows the state of installed packages. The database is accessed whenever the rpm command is issued. The database is modified whenever a package is installed or removed.
For RHEL9 and below, The RPM database is located in /var/lib/rpm directory. Files beneath this directory hold the installed package information. These files can be backed up safely using any method that is comfortable. Below is an example on how to back up this directory using the tar command.
In RHEL 10, the RPM database has been relocated from the /var/lib/rpm directory to the /usr/lib/sysimage/rpm directory.
Before backing up any file in this directory, confirm that no process is currently using any file within this directory.
This can be achieved with the lsof command.
For RHEL9 and below
# lsof | egrep /var/lib/rpm
For RHEL10
# lsof | egrep "/var/lib/rpm|/usr/lib/sysimage/rpm"
Any process accessing files within this directory should be stopped before continuing.
The next step is to back up the /var/lib/rpm directory using the tar command:
For RHEL 7 and below
# tar cjf $HOME/rpmdatabase.tar.bz2 /var/lib/{rpm,yum}
For RHEL 8 and RHEL 9
# tar cjf /tmp/rpmdatabase.tar.bz2 /var/lib/{rpm,dnf} /etc/{dnf*,os-release} /usr/lib/os-release
For RHEL 10
# tar cjf /tmp/rpmdatabase.tar.bz2 /var/lib/{rpm,dnf} /usr/lib/sysimage/rpm /etc/{dnf*,os-release} /usr/lib/os-release
The process of backing up the rpm database should be done before and after package updates. This can help ensure that if there is an rpm database failure, that the database previous state can be restored quickly and easily.