Find yourself with a corrupt postfix virtual file? Have a working, good virtual.db? Follow these instructions to save the day…
Start by dumping the current known good virtual.db file:
[/path/to/postfix] % db_dump -p virtual.db > outfile
edit the resultant outfile, deleting this part (top of the file):
VERSION=3
format=print
type=hash
h_nelem=1015
db_pagesize=16384
HEADER=ENDand this part (bottom of the file):
DATA=ENDThen massage your data. I did it in Microsoft Word (I know, I know, but it’s really easy in Word…) by doing the following:
- open outfile in Word.
- find and replace all ‘space’ characters with nothing(delete the spaces in the text)
- find and replace all occurences of ’0′ with nothing (delete the 0 from each line end)
- select all, Table–>Convert–>Convert Text to Table…
- change to 2 columns
- Table–>Convert–>Convert Table to Text…, separate text with tabs
- save file
move outfile back to your server, rename it, and hash the new file.
[/path/to/postfix] % mv outfile virtual
[/path/to/postfix] % postmap hash:virtual
If you want to double-check yourself before doing the postmap that actually re-writes your known good virtual.db, do this:
[/path/to/postfix] % db_dump -p virtual.db > knowngood(same as we did in the beginning, dump the known good db file)[/path/to/postfix] % cp virtual newvirtual(copy the virtual you just recreated, so you can hash it and not overwrite your original)[/path/to/postfix] % postmap hash:newvirtual(create the new berkeley db hash file)[/path/to/postfix] % db_dump -p newvirtual.db > newoutfile(dump the new db file)[/path/to/postfix] % diff knowngood newoutfile(run diff against the old db_dump output vs. the new db_dump output)
If everything is copasetic, diff won’t return anything, as the newvirtual.db should match the known good virtual.db!
Wow, that was pretty easy. Of course, you can use whatever text editing program you’d like…
Hope this helps someone out…
db_dump didn’t seem to exist on my computer,
but I did manage to recover the aliases
file I needed from aliases.db with the following command:
postalias -s aliases >aliases
ha.
-s Retrieve all database elements, and write one line of key: value
output for each element. The elements are printed in database
order, which is not necessarily the same as the original input
order. This feature is available in Postfix version 2.2 and
later, and is not available for all database types.
excellent Jon. Hopefully I’ll never need to use it, but good knowledge to have.