python script to backup installed packages

Adam Vande More amvandemore at
Thu Feb 11 16:15:14 UTC 2010

Sometimes you have need to backup installed packages.  I realize most port
management tools do this automatically, but if you're on a system with a lot
of packages installed and one port management tool fails and you use another
to fix it, /usr/ports/packages can become jumbled.  Anyways, I've written a
simple python script which will create a fresh snapshot of all installed
packages.  These are convenient for backups or installing on a new system.

For anyone interested:

from subprocess import Popen, PIPE
import os
s = Popen('pkg_info -a | grep : | grep

pkg_location = '/data/packages'

packages = []
for line in s.split('\n'):
    info = line.replace('Information for ', '').replace(':','')


for package in packages:
    s = Popen('pkg_create -b ' +

Adam Vande More

More information about the freebsd-questions mailing list