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

Re: a simple script for restoring downloaded debs

Jörg-Volker Peetz wrote:
> songbird wrote on 07/19/2014 16:23:
>> Jörg-Volker Peetz wrote:
>>> Why not copy all files back and order something like "apt-get autoclean" to get
>>> rid of the older packages?
>>   it's a rather huge archive (i think about 18000 
>> debs), but yes that would be another way of doing
>> it.  i think the flaw is that you would lose the
>> local, obsolete or debs from unofficial sources
>> that you no longer keep in the sources.list (but
>> i would have to verify this).
> Yes, you would lose them.

  i've set up a archive for those too.

>>   the script looks to have gotten the job mostly
>> done.  i've added a bit to copy my local, obsolete
>> and unofficial debs back from another archive and
>> i think what remains are those debs that i somehow
>> missed archiving.  sometimes i have to get files
>> via the library and USB stick.
> A note on your scipt:
> If you build a pipe of grep, sed, and awk, it can often be done just with awk.
> E.g., your line
>   debname=`apt-cache show $pkgname=$version | egrep '^Filename: ' | sed -e
> 's/^Filename: //' | awk --field-separator='/' -e '{print \$NF}'`
> could be written as something like
>   debname=$(apt-cache show $pkgname="$version" | awk '/^Filename: /
> {sub(".*/","", $2) ; print $2}')
> saving the call of egrep and sed.

  sure, i could do that, usually i build up my 
statements a bit at a time to make sure each step 
works before adding the next chunk.  this isn't a 
script run that often i wouldn't worry about 
optimizing it.

  the last bug i had to figure out was that some debs
are treated differently in their version names as
they translate to the filename stored in the pool
(console-data, libreoffice and several others).
i'm not sure why or where the difference exists but it 
was 42 debs (could it be the ghost of Douglas Adams 
having a bit of a prank? :) ).  i added a check for 
the alternate version.

  there may be other bugs yet to find, but for my
own apt archive of 2375 installed packages selecting
from the backed up archive of 10980 debs it now is
complete (my wheezy archive was at 18000+ debs).

  the current script version now looks like:

# run as root
# this script attempts to restore the debs to 
# /var/cache/apt/archives for all of the packages 
# you currently have installed from a local archive
# (see localarch).
# if you have obsolete, local or unofficial debs from
# other sources they can be kept in another place as a
# backup and also copied by this script (see deblocal).
# when finished if there is a downloadlist file in the
# current directory those were the debs missing from 
# the local archive (or something strange is going on 
# with the version numbers).

# your local archive

# for local, obsolete or unofficial debs that are 
# still used apt-cache show will not have a filename.

# the archive of debs downloaded 

# the apt archive

# copy the local, obsolete or unofficial debs
cp --preserve=all $deblocal/*.deb $dest

# we will generate a list of wget statements to fill in the gaps
rm -f downloadlist 2>&1 /dev/null

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 -z "$debname" ; then
    echo "Package $pkgname Version -->$version<-- may be obsolete, local or from a non-official source."

  versionplay=`echo $version | grep ':' | wc -c`
  if test ! "$versionplay" = "0" ; then

    #for some debs we need an alternate version
    altdebname=`echo $debname | sed -e 's/.://'`

    #echo "We have a funny version number, let's fix it in the filename"
    versionhead=`echo $version | cut -d ':' -f1,1 | sed -e 's/$/%3a/'`
    #echo "$pkgname $version $versionhead $debname"
    debname=`echo $debname | sed -e "s/_/_$versionhead/"`
    #echo "$pkgname $version $versionhead $debname"


  if test -e "$debarch/$debname" ; then

    #echo "$debarch/$debname $dest"
    if test ! -e "$dest/$debname" ; then
      cp --preserve=all "$debarch/$debname" $dest

  elif test -e "$debarch/$altdebname" ; then

    #echo "$debarch/$altdebname $dest"
    if test ! -e "$dest/$altdebname" ; then
      cp --preserve=all "$debarch/$altdebname" $dest

    filename=`apt-cache show $pkgname=$version | egrep '^Filename: ' | sed -e 's/^Filename: //'`
    echo "wget -c http://ftp.us.debian.org/debian/$filename"; >> downloadlist
    echo "$pkgname $debname missing?  version ->$version<-"

chown root $dest/*
chgrp root $dest/*


Reply to: