# recover installed package list based on file list content of /usr/local/bin



## Alain De Vos (Aug 5, 2020)

Contents of /var was lost.
Is the question of the title possible ?


----------



## Alain De Vos (Aug 5, 2020)

I found as alternative "pkg backup" , but as /var was completely lost this cannot be used.


----------



## SirDice (Aug 5, 2020)

I'm going to say, you're screwed. There's pkg-which(8) which might be useful, but that too relies on information stored in /var. So you're left with a lot of guesswork. It's usually quite obvious where things came from but it's going to take a lot of work and guessing to figure it out.


----------



## Crivens (Aug 5, 2020)

You may compare the content with the pkg-plist files in the ports, that should give you a good idea. Kernel modules will not be found that way. Some shell-fu with a |sort|uniq will give you a starting point.
My shell skills are not up to speed any my linear thinking mode is offline since I'm busy since oh-dark-stupid (0430 to be precise).


----------



## Alain De Vos (Aug 5, 2020)

Lol. Luckely i have found a two months old "pkg info" output .
In fact I'll write a daily "pkg info" script in crontab. Lessons learned.


----------



## olli@ (Aug 5, 2020)

Alain De Vos said:


> Lol. Luckely i have found a two months old "pkg info" output .
> In fact I'll write a daily "pkg info" script in crontab. Lessons learned.


Don’t you have a regular backup of that machine? You can simply recover /var/db/pkg from your latest backup.


----------



## ralphbsz (Aug 5, 2020)

Theoretically, this should work: Set up a test system, and install *all* packages on it. Then, for every thing you find in /usr/local/bin run "pkg which <thing>", and collate the output. Let's assume you have a complete list of file names in old_system.list, then seomthing like this should work (not tested):

```
( cat old_system.list | \
  while  read old_file
  do
    pkg which $old_file | \
    awk '{print $NF}'
  done ) | \
sort | uniq -c
```
That should give a list of packages, sorted, with a count of how many files were found for each. This is sort of the thing that crivens suggested above, just using a sacrificial test system instead of parsing the package plists.


----------



## Mjölnir (Aug 5, 2020)

olli@ said:


> Don’t you have a regular backup of that machine? You can simply recover /var/db/pkg from your latest backup.


Well, then he wouldn't have asked...


----------



## Mjölnir (Aug 5, 2020)

Alain De Vos when do you switch to ZFS? There you have snapshots to recover from human mistakes.


----------



## Crivens (Aug 5, 2020)

mjollnir said:


> Alain De Vos when do you switch to ZFS? There you have snapshots to recover from human mistakes.


... when you make them 
But we could define not making them a human mistake, but it would not being recoverable.


----------



## Phishfry (Aug 5, 2020)

Have you looked at your user history file  for possible pkg clues?
I typically increase history threshold to "set history = 10000" in .cshrc for exactly this reason.

`pkg install &{up key}`

Obviously this would only work for `pkg` based installs.


----------



## jmos (Aug 5, 2020)

Alain De Vos said:


> In fact I'll write a daily "pkg info" script in crontab.


Loosely based on my backup:

`pkg info > /path/to/mount/packages_all.txt
pkg query -e '%a = 0' %o > /path/to/mount/packages_base.txt
pkg query -e '%#r == 0' '%n-%v' > /path/to/mount/packages_nodeps.txt`

`pkg info` is the least important - would only result in having packages restored as manually installed while they were automatic before. Only makes sense for a final comparison. But the other two are also great to set up new machines


----------



## Crivens (Aug 5, 2020)

My root user has an "install.sh" script which contains all the commands I use for installing. One only needs to add to it. It also takes care of ports with changed flags or versions, like lame and drm-kmod.
Just my 2c.


----------



## Alain De Vos (Aug 5, 2020)

In such a script I would backup /usr/local/etc and remove /usr/local


----------



## Mjölnir (Aug 6, 2020)

jmos said:


> Loosely based on my backup:
> 
> `pkg info > /path/to/mount/packages_all.txt
> pkg query -e '%a = 0' %o > /path/to/mount/packages_base.txt
> ...


I find descriptive aliases for _leaf_ & _prime-*_ in `pkg alias`.  IMHO better use these, especially in a posting, because we can quickly guess what these commands do, instead of looking it up by `pkg help query`.


Crivens said:


> My root user has an "install.sh" script which contains all the commands I use for installing. One only needs to add to it. It also takes care of ports with changed flags or versions, like lame and drm-kmod.
> Just my 2c.


Please post that script to _Usefull Scripts_?


----------

