From owner-freebsd-questions@FreeBSD.ORG Thu Feb 11 19:31:30 2010 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id E8469106566B for ; Thu, 11 Feb 2010 19:31:29 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from poseidon.ceid.upatras.gr (poseidon.ceid.upatras.gr [150.140.141.169]) by mx1.freebsd.org (Postfix) with ESMTP id 600998FC0A for ; Thu, 11 Feb 2010 19:31:29 +0000 (UTC) Received: from mail.ceid.upatras.gr (unknown [10.1.0.143]) by poseidon.ceid.upatras.gr (Postfix) with ESMTP id 5DE75EB490D; Thu, 11 Feb 2010 21:31:28 +0200 (EET) Received: from localhost (europa.ceid.upatras.gr [127.0.0.1]) by mail.ceid.upatras.gr (Postfix) with ESMTP id 514BB160CD3; Thu, 11 Feb 2010 21:31:28 +0200 (EET) X-Virus-Scanned: amavisd-new at ceid.upatras.gr Received: from mail.ceid.upatras.gr ([127.0.0.1]) by localhost (europa.ceid.upatras.gr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id tOjYMwrLzQAt; Thu, 11 Feb 2010 21:31:28 +0200 (EET) Received: from kobe.laptop (ppp-94-64-211-38.home.otenet.gr [94.64.211.38]) by mail.ceid.upatras.gr (Postfix) with ESMTP id 055D3160CBC; Thu, 11 Feb 2010 21:31:27 +0200 (EET) Received: from kobe.laptop (kobe.laptop [127.0.0.1]) by kobe.laptop (8.14.4/8.14.4) with ESMTP id o1BJVQBF038466 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 11 Feb 2010 21:31:27 +0200 (EET) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost) by kobe.laptop (8.14.4/8.14.4/Submit) id o1BJVQU3038463; Thu, 11 Feb 2010 21:31:26 +0200 (EET) (envelope-from keramida@ceid.upatras.gr) From: Giorgos Keramidas To: Adam Vande More References: <6201873e1002110815l312da12fr5388956f32465516@mail.gmail.com> Date: Thu, 11 Feb 2010 21:31:26 +0200 In-Reply-To: <6201873e1002110815l312da12fr5388956f32465516@mail.gmail.com> (Adam Vande More's message of "Thu, 11 Feb 2010 10:15:12 -0600") Message-ID: <87d40btvqp.fsf@kobe.laptop> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.92 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: freebsd-questions@freebsd.org Subject: Re: python script to backup installed packages X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 11 Feb 2010 19:31:30 -0000 On Thu, 11 Feb 2010 10:15:12 -0600, Adam Vande More wrote: > 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 > Information',shell=True,stdout=PIPE).communicate()[0] > > pkg_location = '/data/packages' > > packages = [] > for line in s.split('\n'): > info = line.replace('Information for ', '').replace(':','') > packages.append(info) > > os.chdir(pkg_location) > > for package in packages: > s = Popen('pkg_create -b ' + > package,shell=True,stdout=PIPE).communicate()[0] Nice script! My own version was initially written in sh(1) to avoid having a Python dependency. Then I rewrote it in Python too. FWIW, you can probably save a few replace() calls and avoid the need for a full array of all the packages by yielding the package names: from subprocess import PIPE, Popen as popen def listpackages(): for l in iter(popen("pkg_info", shell=True, stdout=PIPE).stdout.readline, ""): yield l.split()[0] for p in listpackages(): dostuff(p) My own version groks an os.environ['EXTRA_PKG_CREATE_ARGS'] option too and inserts the extra options before the ["-b", "package"] arguments of pkg_create, so that I can run the script for example with: env EXTRA_PKG_CREATE_ARGS='-Rvn' ./savepkg.py This way package dependencies are saved too (-R option), the output of the `pkg_create -b' command is slightly more verbose, and saving the same package multiple times doesn't overwrite existing packages of the same version (-n option).