Not enough reputation to comment on Gussoh's answer, so I have to write my solution (which is based on his) here: I had to do it like this:
mosquitto_sub -h 192.168.1.20 -t "#" -v --retained-only | while read line; do echo "$line" | awk '{print $1}'; done > retained.txt
<retained.txt xargs -I % mosquitto_pub -h 192.168.1.20 -t % -n -r -d
You can probably do it without using a file here, but this way I could check if everything was included.