[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

a simple script for restoring downloaded debs

  the other night i accidentally deleted the debs i
normally keep in the /var/cache/apt/archives directory
and while i do keep a backup of them in another
directory it is along with all the previous versions
too, so it isn't as easy as just copying them.

  instead i have to figure out which is the installed
version (because that is the one i am most interested
in having in there in case i do have to do a reinstall).

  so i started playing with my favorite scripting
language (no, not perl, i've just never been able
to get into perl - i guess i'm stuck in my ways :) ).

  so here is round 1 of the script (which gets 90% of
the packages back for me from my backup archive at
/myarchive/debian/jessie).  oops, i see one thing i
should do for the next version already (make that
archive name a variable :) ).


pkglist=`dpkg -l | egrep '^ii  ' | cut --delimiter=' ' -f3,3 | cut --delimiter=':' -f1,1`
versionlist=`dpkg -l | egrep '^ii  ' | sed -e 's/  */ /g' | cut -d ' ' -f3,3`
for pkgname in $pkglist; do
  version=`echo $versionlist | cut -d ' ' -f1,1`
  versionlist=`echo $versionlist | cut -d ' ' -f2-`
  debname=`apt-cache show $pkgname=$version | egrep '^Filename: ' | sed -e 's/^Filename: //' | awk --field-separator='/' -e '{print \$NF}'`
  if test -f "/myarchive/debian/jessie/$debname" ; then

    echo "/myarchive/debian/jessie/$debname /var/cache/apt/archives" > /dev/null
    cp -a "/myarchive/debian/jessie/$debname" /var/cache/apt/archives

    echo "$debname missing?"
chown root /var/cache/apt/archives/*
chgrp root /var/cache/apt/archives/*

  looks like the problem for all but a few remaining debs is
the use of a ":" in the name, but perhaps it is some other
character too (for just a few cases).

  to explore it in more detail in one example look at what 
apt-cache show reports for gawk, what dpkg -l says and what
the actual filename in the directory looks like:

-----  apt-cache show
Package: gawk
Version: 1:4.1.1+dfsg-1
Filename: pool/main/g/gawk/gawk_4.1.1+dfsg-1_i386.deb

----- dpkg -l | grep gawk
ii  gawk                                        1:4.1.1+dfsg-1                    i386         GNU awk, a pattern scanning and processing language

----- ls -l gawk*
-rwxr-xr-x 1 me me 932638 Mar 16  2013 gawk_1%3a4.0.1+dfsg-2.1_i386.deb
-rwxr-xr-x 1 me me 427810 Apr  8 07:44 gawk_1%3a4.0.1+dfsg-3_i386.deb
-rwxr-xr-x 1 me me 495938 Apr 21 08:25 gawk_1%3a4.1.1+dfsg-1_i386.deb

  how to reconcile?

  my LANG environment is set to:


  fixing this would get me another 256-260 debs (of 2377 
that i have installed) (with 4 more i'm not sure yet what's 
going on there)...  those last 4 give me a completely 
missing name for some reason i've not dug into yet.

  90% of an answer with a few minutes of tinkering with
a script is better than doing it manually for sure!

  happy hacking and thanks for any help  :)


Reply to: