Pacman: error: duplicated database entry

On Arch Linux, I’ve backed up my system with rsync and restored
it again, but it seems that my way of doing it (which I did get from the Arch
Wiki but must be wrong?) has kept old files deleted
by Pacman. This results in the error duplicated database entry
when I try to upgrade my system with pacman -Syu. What should I do?

Asked By: Toothrot

||

I suggest that you read the information in the links here and here.

Basically, you need to remove the duplicates (manually or using a script) from /var/lib/pacman/local/.

Answered By: maulinglawns

I just went through this. You need to us the rsync --delete option. The Arch wiki is wrong about the --delete option altering anything in the source system. It will only delete files in destination if they have been deleted or are not in source. This will also prevent a bunch of unwanted files building up if you run the backup on a regular basis.

Source1

Source2

Answered By: Schooled

I tried @Schooled and @maulinglawns answers but both basically bricked my system.

The reason is that source was running linux-5.4.71 and dest was running linux-5.4.72, released just a day apart.

When rsync --delete ran, it deleted all of the kernel modules on dest/lib/5.4.72/, which basically led to a bricked system that was incapable of mounting critical file systems, among other things.

After restoring from my btrfs snapshot (thank God!!), I figured out the following solution:

# From https://github.com/hopeseekr/BashScripts/blob/master/arch-pacman-dupe-cleaner
# Copyright © 2020 Theodore R. Smith <theodore@phpexperts.pro>
# GPG: 4BF8 2613 1C34 87AC D28F  2AD8 EB24 A91D D612 5690
# License: Creative Commons Attribution v4.0 International
# @see https://unix.stackexchange.com/a/615578/15780

# 0. Login as root.
sudo -s

# 1. Remove the latest dupes from your Pacman DB.
pacman -Syu 2>&1 | grep "duplicated database entry" > /tmp/dupes
for latest in $(for dupe in $(cat /tmp/dupes | awk '{print $5}'); do
    ls /var/lib/pacman/local/$dupe* -d | tail -n1; done); do
    rm -rvf $latest;
done
rm /tmp/dupes

# 2. Remove system of dupe files.
pacman -Syu 2>&1 > /tmp/update-dupes
cat /tmp/update-dupes | grep "exists in file system" | awk '{print $2}' | xargs rm -vf

# 3. Reinstall everything (this will restore anything deleted in #2.).
pacman -Syu

Since this is quite a lot, I have added it to my BashScripts repository as arch-pacman-dupe-cleaner.

Answered By: Theodore R. Smith

This command worked for me after changed directory to /var/lib/pacman/local:

ls | sort | awk -v re='(.*)-[^-]*-[^-]*$' 'match($0, re, a) { if (!(a[1] in p)){p[a[1]]} else {print} }' | xargs sudo rm -rf
Answered By: Kristian
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.