Recover/Extract a postfix virtual file from a known good virtual.db

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=END

and this part (bottom of the file):

    DATA=END

Then massage your data. I did it in Microsoft Word (I know, I know, but it’s really easy in Word…) by doing the following:

  1. open outfile in Word.
  2. find and replace all ‘space’ characters with nothing(delete the spaces in the text)
  3. find and replace all occurences of ‘0’ with nothing (delete the 0 from each line end)
  4. select all, Table–>Convert–>Convert Text to Table…
  5. change to 2 columns
  6. Table–>Convert–>Convert Table to Text…, separate text with tabs
  7. 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:

  1. [/path/to/postfix] % db_dump -p virtual.db > knowngood (same as we did in the beginning, dump the known good db file)
  2. [/path/to/postfix] % cp virtual newvirtual (copy the virtual you just recreated, so you can hash it and not overwrite your original)
  3. [/path/to/postfix] % postmap hash:newvirtual (create the new berkeley db hash file)
  4. [/path/to/postfix] % db_dump -p newvirtual.db > newoutfile (dump the new db file)
  5. [/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…

Advertisements

2 thoughts on “Recover/Extract a postfix virtual file from a known good virtual.db

  1. 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

  2. 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.

Comments are closed.