Download All Current Package Files with Pacman
On my home (Arch Linux) server, I use Duplicity to manage system backups. Recently I noticed that the size of the main filesystem backup set is slowly (but steadily) growing over time. Didn’t take long to find out that the Pacman local package cache was hogging about 4GB:
root@localhost /var/cache/ $ du -sch *
260K cups
280K fontconfig
4.0K gdm
88K ldconfig
3.4M man
4.0G pacman
4.0G total
root@localhost /var/cache $ ls -lh pacman/pkg
total 4.0G
120K Sep 10 11:27 .
4.0K Jun 8 15:00 ..
38K Oct 24 2013 a52dec-0.7.4-8-x86_64.pkg.tar.xz
132K Oct 23 2013 aalib-1.4rc5-10-x86_64.pkg.tar.xz
.....
80K Oct 21 2013 zlib-1.2.8-3-x86_64.pkg.tar.xz
283K May 30 2013 zvbi-0.2.33-6-x86_64.pkg.tar.xz
The solution was obvious: since this is only a local cache, it’s considered “safe” to delete.
… unless a package update tomorrow borks your system.
Since Arch is a rolling distribution, I get package updates at least daily and once in a while (infrequently as it happens) those of us living on the bleeding edge suffer a laceration.
At that point it’s helpful to isolate the offending package, and roll back to the previous version.
There is a wildly helpful script called downgrade
available through the AUR, but what if the thing that’s broken is something in your network stack and you can’t access the Arch Rollback Machine?
Thus it’s a good idea to maintain a locally cached copy of the “current” working version of any given package. That’s very easily done:
pacman -Sdw `pacman -Qqn`
This will generate a list of all currently-installed packages (ignoring “foreign” packages such as those from the AUR, or ones you built and installed locally), and it will download any that aren’t in the cache.
In my case, the goal was to ONLY wind up with these current files, so I removed everything before I began:
rm /var/cache/packman/pkg/*
pacman -Sdw `pacman -Qqn`
But this is a rather scorched-earth approach and necessitates re-downloading every installed package on the system, which was a little over 1GB, most of which (but not all) I already had. I’ve got a rather healthy Internet connection so this didn’t take long, but it’s poor Netizenry to waste bandwidth no matter how freely it flows.
In that light, a more advanced script should query the package list from pacman -Qn
and pre-process each entry, only conditionally deleting the file.
This way, the download command would only fetch files you didn’t already have instead of the kitchen sink.
But that will have to wait for another day.