| 1 |
#!/bin/bash -e |
| 2 |
|
| 3 |
# A complex script that creates a repository of deltas, that can be |
| 4 |
# used by debdelta-upgrade for upgrading packages. |
| 5 |
# See also --help below. |
| 6 |
|
| 7 |
# WARNING : after the release of Squeeze, this script is deprecated: |
| 8 |
# indeed the new archive signatures need debmirror >= 2.6, |
| 9 |
# and I did not prepare a 'trash' patch for it, but instead wrote |
| 10 |
# a new script to use the new 'debmirror --marshal' option |
| 11 |
|
| 12 |
# Copyright (C) 2006-11 Andrea Mennucci. |
| 13 |
# License: GNU Library General Public License, version 2 or later |
| 14 |
|
| 15 |
|
| 16 |
########### customize under here vvvvvvvvvvvvv |
| 17 |
|
| 18 |
#where is the full mirror of debian |
| 19 |
debmir=/mirror/debian |
| 20 |
|
| 21 |
#where to mirror from |
| 22 |
host=ftp.debian.org |
| 23 |
|
| 24 |
#where to store deltas |
| 25 |
deltamir=/mirror/debian-deltas |
| 26 |
#delete old debs from trash if the space here drops below kB : |
| 27 |
minfreespace=2000000 |
| 28 |
#delete deltas and old_debs that are older than days (regardless of space) |
| 29 |
old_delete_days=50 |
| 30 |
|
| 31 |
#where to keep snapshots of the older indexes of the archives (for the --snapshot option) |
| 32 |
dists_snapshots=/mirror/debian-snapshots |
| 33 |
|
| 34 |
#where the gnupg stuff specific to debdelta resides |
| 35 |
# and in particular the public and private keyrings |
| 36 |
GNUPGHOME=/var/lib/debdelta/gnupg |
| 37 |
|
| 38 |
#where the gnupg agent variable is stored |
| 39 |
#(unset this if you do not want to use the agent) |
| 40 |
# note that this needs gnupg2 >= 2.0.11 (that is not lenny) |
| 41 |
GNUPGAGENTINFO="${GNUPGHOME}/debdeltas-gpg-agent-info" |
| 42 |
|
| 43 |
#the secret key to sign the deltas |
| 44 |
GNUPGSEC=0xTHESECKEY |
| 45 |
|
| 46 |
#note: if you export this , then it will affect also debmirror: |
| 47 |
# make sure the deb repository key is copied there |
| 48 |
#export GNUPGHOME |
| 49 |
|
| 50 |
#where is the debdeltas program |
| 51 |
debdeltas=/usr/bin/debdeltas |
| 52 |
#options to your taste |
| 53 |
debdelta_opt="-v -d --test --delta-algo xdelta3 --gpg-home $GNUPGHOME --sign $GNUPGSEC " |
| 54 |
|
| 55 |
##if you apply the 'trash patch' in contrib to debmirror, then you may use this: |
| 56 |
debmirror_opt="--trash $deltamir/old_debs --method=http --diff=none" |
| 57 |
trash=$deltamir/old_debs |
| 58 |
debmirror=/usr/local/bin/debmirror_2.4_mine |
| 59 |
|
| 60 |
##otherwise this should work as well |
| 61 |
#debmirror_opt='--method=http' |
| 62 |
#trash='' |
| 63 |
#debmirror=/usr/bin/debmirror |
| 64 |
|
| 65 |
#note: files in $trash will be deleted by this script! use a dedicated dir! |
| 66 |
|
| 67 |
#this should be in the same partition as the debian mirror $debmir |
| 68 |
# (must be, if the trash is not used) |
| 69 |
TMPDIR=/mirror/tmp |
| 70 |
export TMPDIR |
| 71 |
|
| 72 |
|
| 73 |
ARCHc='i386,amd64' |
| 74 |
ARCHs='i386 amd64' |
| 75 |
|
| 76 |
DISTc='lenny,squeeze,sid,experimental' |
| 77 |
DISTs='lenny squeeze sid experimental' |
| 78 |
|
| 79 |
########### customize over here ^^^^^^^^^^^^^^ |
| 80 |
|
| 81 |
|
| 82 |
#since the output is automatically parsed, do it in English |
| 83 |
unset LANG |
| 84 |
unset LC_ALL |
| 85 |
unset LC_NUMERIC |
| 86 |
|
| 87 |
startgpgagent () { |
| 88 |
gpg-agent --homedir "${GNUPGHOME}" --daemon --write-env-file "$GNUPGAGENTINFO" ; |
| 89 |
} |
| 90 |
|
| 91 |
##enable this after catastrofic failures, to try to create all leftover deltas |
| 92 |
## parsing all olddists once again |
| 93 |
RECOVER='' |
| 94 |
|
| 95 |
DEBUG='' |
| 96 |
VERBOSE='' |
| 97 |
RM='rm' |
| 98 |
DO_MIRROR=true |
| 99 |
DO_CLEAN_DELTAS='' |
| 100 |
DO_SNAPSHOT='' |
| 101 |
while [ "$1" ] ; do |
| 102 |
case "$1" in |
| 103 |
-h|--help) |
| 104 |
cat <<EOF |
| 105 |
Usage: debmirror-deltas [options] |
| 106 |
|
| 107 |
This script (that contains many variables that must be customized!) |
| 108 |
keeps a full mirror of the Debian repository (using 'debmirror'), |
| 109 |
and it uses it to build a repository of deltas (using 'debdeltas'). |
| 110 |
|
| 111 |
Note that this script assumes that the current Debian |
| 112 |
distributions are $DISTs . |
| 113 |
It will generate deltas for the upgrades in those, |
| 114 |
on arches $ARCHs . |
| 115 |
|
| 116 |
Options: |
| 117 |
|
| 118 |
-v verbose (passed to debmirror and debdeltas) |
| 119 |
-d debug (passed to debmirror and debdeltas), |
| 120 |
also, do not 'rm' stuff, just tell |
| 121 |
--no-mirror do not download debs to the local mirror |
| 122 |
--recover if it crashed, resume the unfinished jobs |
| 123 |
--clean-deltas clean useless deltas (see debdeltas man page) |
| 124 |
--snapshot capture a snapshot of the archive (use daily) |
| 125 |
--start-gpg-agent start gpg agent, feed |
| 126 |
the passwd for the secret gpg key in it |
| 127 |
EOF |
| 128 |
exit ;; |
| 129 |
-v) VERBOSE="$VERBOSE -v" ;; |
| 130 |
-d) DEBUG=--debug ; RM='echo -- would rm ' ;; |
| 131 |
#used to finish interrupted runs |
| 132 |
--no-mirror) DO_MIRROR='' ;; |
| 133 |
--recover) RECOVER=true ;; |
| 134 |
--clean-deltas) DO_CLEAN_DELTAS=true ;; |
| 135 |
--snapshot) DO_SNAPSHOT=true ;; |
| 136 |
--start-gpg-agent) |
| 137 |
echo ------ start agent |
| 138 |
startgpgagent |
| 139 |
. "$GNUPGAGENTINFO" |
| 140 |
export GPG_AGENT_INFO |
| 141 |
#force loading of the private key (I know no better way) |
| 142 |
echo ------ sign dummy file, to load the key |
| 143 |
t=`tempfile` |
| 144 |
echo pippo > $t |
| 145 |
gpg2 --quiet --homedir "${GNUPGHOME}" -o /dev/null --sign $t |
| 146 |
rm $t |
| 147 |
exit 0 |
| 148 |
;; |
| 149 |
*) echo "Unknown option $1 , try --help" ; exit 1 ;; |
| 150 |
esac |
| 151 |
shift |
| 152 |
done |
| 153 |
|
| 154 |
#################### set gpg stuff, test it |
| 155 |
|
| 156 |
|
| 157 |
gpgagentcmd="$0 --start-gpg-agent" |
| 158 |
|
| 159 |
# set gpg-agent variables, test it |
| 160 |
if test "$GNUPGAGENTINFO" ; then |
| 161 |
if test ! -r "$GNUPGAGENTINFO" ; then |
| 162 |
echo ERROR no agent info, please start the agent with |
| 163 |
echo $gpgagentcmd |
| 164 |
exit 1 |
| 165 |
else |
| 166 |
. "$GNUPGAGENTINFO" |
| 167 |
export GPG_AGENT_INFO |
| 168 |
if test ! "${GPG_AGENT_INFO}" -o ! -e "${GPG_AGENT_INFO/:*/}" -o ! -O "${GPG_AGENT_INFO/:*/}" ; then |
| 169 |
echo ERROR agent info is not OK, please run the command |
| 170 |
echo $gpgagentcmd |
| 171 |
exit 1 |
| 172 |
elif ! echo | gpg-connect-agent --homedir ${GNUPGHOME} ; then |
| 173 |
echo ERROR agent is not responding, please run the command |
| 174 |
echo $gpgagentcmd |
| 175 |
exit 1 |
| 176 |
fi |
| 177 |
fi |
| 178 |
fi |
| 179 |
|
| 180 |
|
| 181 |
#test that we can sign, possibly loading the password in the agent |
| 182 |
if test "$GNUPGSEC" ; then |
| 183 |
t=`tempfile` |
| 184 |
echo pippo > $t |
| 185 |
if ! gpg2 --quiet --batch --homedir "${GNUPGHOME}" -o /dev/null --default-key $GNUPGSEC --sign $t ; |
| 186 |
then |
| 187 |
echo signature test FAILED |
| 188 |
rm $t |
| 189 |
exit 1 |
| 190 |
fi |
| 191 |
rm $t |
| 192 |
fi |
| 193 |
|
| 194 |
###################### some useful variables |
| 195 |
|
| 196 |
#the lock used by debmirror |
| 197 |
debmirlock=$debmir/Archive-Update-in-Progress-`hostname -f` |
| 198 |
|
| 199 |
b=`basename $0` |
| 200 |
|
| 201 |
lockfile -r 10 /tmp/$b.lock || exit 1 |
| 202 |
trap "rm $VERBOSE -f /tmp/$b.lock" 0 |
| 203 |
|
| 204 |
olddists="" |
| 205 |
log=/nonexistant |
| 206 |
|
| 207 |
## profile debdeltas |
| 208 |
#debdeltas="python -m cProfile -o /tmp/debdelta-profile-$(dirname $p | tr / _ ) $debdeltas" |
| 209 |
|
| 210 |
today=`date +'%F'` |
| 211 |
yyyymm=`date +'%Y-%m'` |
| 212 |
|
| 213 |
################## routines |
| 214 |
|
| 215 |
freetrash () { |
| 216 |
test "$trash" || return |
| 217 |
age=${old_delete_days} |
| 218 |
blocksize=`stat -f -c "%s" "$trash"` |
| 219 |
newsize=0 #trick to run at least once |
| 220 |
while test $age -ge 1 -a $newsize -le $minfreespace ; do |
| 221 |
find "$trash" -name '*.deb' -type f -mtime +$age | xargs -r $RM $VERBOSE || true |
| 222 |
freeblocks=`stat -f -c "%a" "$trash"` |
| 223 |
newsize=`expr $freeblocks \* $blocksize / 1024` |
| 224 |
age=`expr $age - 5` |
| 225 |
done |
| 226 |
} |
| 227 |
|
| 228 |
cleanuppool () { |
| 229 |
find $deltamir/pool \ |
| 230 |
\( -name '*debdelta-fails' -or -name '*debdelta-too-big' \ |
| 231 |
-or -name '*debdelta' \) -mtime +${old_delete_days} -type f |\ |
| 232 |
xargs -r $RM $VERBOSE || true |
| 233 |
|
| 234 |
find $deltamir/pool/ -type d -empty | xargs -r rmdir $VERBOSE || true |
| 235 |
|
| 236 |
if test "$trash" ; then |
| 237 |
find $trash -type f -not -name '*.deb' | xargs -r $RM $VERBOSE || true |
| 238 |
fi |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
run_debmirror () { |
| 244 |
if test -e $debmirlock ; then |
| 245 |
echo Archive $debmir is locked |
| 246 |
exit 1 |
| 247 |
fi |
| 248 |
|
| 249 |
olddists=${TMPDIR:-/tmp}/olddists-`date +'%F_%H-%M-%S'` |
| 250 |
mkdir $olddists |
| 251 |
|
| 252 |
tm=$olddists/debmirror-stdout |
| 253 |
tme=$olddists/debmirror-stderr |
| 254 |
|
| 255 |
cp -a $debmir/dists $olddists |
| 256 |
|
| 257 |
## if using trash, this is not needed : debdeltas will |
| 258 |
##go fishing in the trash dir to find all deleted deltas |
| 259 |
if ! test "trash" ; then |
| 260 |
cp -al $debmir/pool $olddists |
| 261 |
fi |
| 262 |
|
| 263 |
$debmirror $debmir --nosource \ |
| 264 |
$debmirror_opt --arch=$ARCHc \ |
| 265 |
--section=main,contrib,non-free \ |
| 266 |
-v $DEBUG -h $host -d $DISTc \ |
| 267 |
> $tm 2> $tme ; dme=$? |
| 268 |
|
| 269 |
if test "$dme" != 0 ; then |
| 270 |
echo debmirror failed , exit code $dme , stdout $tm |
| 271 |
awk '{ print "> " $0 }' $tm |
| 272 |
echo debmirror failed stderr $tme |
| 273 |
awk '{ print "> " $0 }' $tme |
| 274 |
$RM -f $debmirlock || true |
| 275 |
#sometimes the temporary directory of debmirror is messed up |
| 276 |
#rm -r $debmir/.temp |
| 277 |
cleanuppool |
| 278 |
freetrash |
| 279 |
exit 1 |
| 280 |
else |
| 281 |
$RM $VERBOSE $tm $tme |
| 282 |
fi |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
findmelog () { |
| 288 |
date=`date +'%F-%H'` |
| 289 |
log=$deltamir/log/$yyyymm/${date}.log |
| 290 |
err=$deltamir/log/$yyyymm/${date}.err |
| 291 |
if test -r $log -o -r $log.gz -o -r $err -o -r $err.gz ; then |
| 292 |
for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z ; do |
| 293 |
if test -r $log -o -r $log.gz -o -r $err -o -r $err.gz ; then |
| 294 |
log=$deltamir/log/$yyyymm/${date}.$i.log |
| 295 |
err=$deltamir/log/$yyyymm/${date}.$i.err |
| 296 |
fi |
| 297 |
done |
| 298 |
fi |
| 299 |
} |
| 300 |
|
| 301 |
created_deltas="" |
| 302 |
|
| 303 |
run_debdeltas () { |
| 304 |
if ! test "$olddists" -a -r "$olddists/dists" ; then |
| 305 |
echo "run_debdelta : bad $olddists" |
| 306 |
return |
| 307 |
fi |
| 308 |
findmelog |
| 309 |
( |
| 310 |
exec >> $log |
| 311 |
exec 2>> $err |
| 312 |
echo -n --------------oldnew pass---- ; date ; |
| 313 |
echo "------ tmpdir was in $olddists" |
| 314 |
echo -n ---- options to debdelta ; echo -- $debdelta_opt |
| 315 |
|
| 316 |
for dist in $DISTs ; do |
| 317 |
echo -------------- $dist ---- |
| 318 |
#lenny contains debdelta 0.27, that does not understand lzma |
| 319 |
if test "$dist" = lenny ; then x="--disable-feature lzma" ; else x="" ; fi |
| 320 |
for p in $( cd $debmir ; find dists/$dist/ -name Packages.gz) ; do |
| 321 |
if test -r ${dists_snapshots}/before.4/$p ; then o="--old ${dists_snapshots}/before.4/$p" ; else o='' ; fi |
| 322 |
echo ------ debdelta $x $o --old $olddists/$p $p |
| 323 |
$debdeltas $VERBOSE $debdelta_opt \ |
| 324 |
--alt $trash $x $o \ |
| 325 |
--old $olddists/$p \ |
| 326 |
--dir $deltamir// $debmir/$p |
| 327 |
done |
| 328 |
done |
| 329 |
) |
| 330 |
|
| 331 |
#uncomment for added verbosity |
| 332 |
#egrep -3 -i 'error|warning' $log || true |
| 333 |
|
| 334 |
if test -s $err || grep -iq 'error' $log ; then |
| 335 |
#some error occurred |
| 336 |
test ! -r "$log".gz && { gzip -9 $log ; log=${log}.gz ; } |
| 337 |
echo "-----ERRORS--- full log is in $log or ...err.gz , data in $olddists " |
| 338 |
else |
| 339 |
rm $err # it is empty |
| 340 |
#no error occurred, clean up |
| 341 |
if test "$olddists" ; then |
| 342 |
if test -r "$olddists/dists" ; then |
| 343 |
$RM -r "$olddists/dists" |
| 344 |
fi |
| 345 |
if test -r "$olddists/pool" ; then |
| 346 |
$RM -r "$olddists/pool" |
| 347 |
fi |
| 348 |
#this is a bit exotic , but unfortunately 'test' does not test empty dirs |
| 349 |
if find "$olddists" -maxdepth 0 -type d -empty | grep -q "$olddists" ; then |
| 350 |
rmdir "$olddists" ; else echo Not empty "$olddists" ; |
| 351 |
fi |
| 352 |
fi |
| 353 |
if grep -v '^---' $log | grep -v '^ Total running time' | grep -q '.' ; then |
| 354 |
test ! -r "$log".gz && gzip -9 $log |
| 355 |
created_deltas=1 |
| 356 |
else |
| 357 |
$RM $log |
| 358 |
fi |
| 359 |
fi |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
run_debdeltas_clean () { |
| 364 |
echo -------------- cleanup delta pool |
| 365 |
$debdeltas --clean-deltas -n 0 \ |
| 366 |
--dir $deltamir// \ |
| 367 |
$( for arch in $ARCHs ; do |
| 368 |
for sec in main contrib non-free ; do |
| 369 |
for dist in $DISTs ; do |
| 370 |
echo $debmir/dists/$dist/$sec/binary-$arch/Packages.gz |
| 371 |
done ; done ; done ) |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
########################################### code |
| 376 |
|
| 377 |
#always free space from the trash |
| 378 |
freetrash |
| 379 |
|
| 380 |
if test `stat -f -c "%a" "$deltamir"` -le 32 ; then |
| 381 |
echo "------- emergency pool cleanup , very low disk space in delta mirror" |
| 382 |
run_debdeltas_clean |
| 383 |
cleanuppool |
| 384 |
elif test "${DO_CLEAN_DELTAS}" ; then |
| 385 |
run_debdeltas_clean |
| 386 |
cleanuppool |
| 387 |
fi |
| 388 |
|
| 389 |
test -d $deltamir/log/$yyyymm || mkdir $deltamir/log/$yyyymm |
| 390 |
|
| 391 |
if test "${DO_SNAPSHOT}" ; then |
| 392 |
if test -e $debmirlock ; then |
| 393 |
echo Archive $debmir is locked, cannot --snapshot |
| 394 |
elif test -w ${dists_snapshots} -a -d ${dists_snapshots} ; then |
| 395 |
test -e ${dists_snapshots}/before.4 && rm -r ${dists_snapshots}/before.4 |
| 396 |
test -e ${dists_snapshots}/before.3 && mv ${dists_snapshots}/before.3 ${dists_snapshots}/before.4 |
| 397 |
test -e ${dists_snapshots}/before.2 && mv ${dists_snapshots}/before.2 ${dists_snapshots}/before.3 |
| 398 |
test -e ${dists_snapshots}/before.1 && mv ${dists_snapshots}/before.1 ${dists_snapshots}/before.2 |
| 399 |
mkdir ${dists_snapshots}/before.1 |
| 400 |
cp -a $debmir/dists ${dists_snapshots}/before.1/. |
| 401 |
else |
| 402 |
echo Please set dists_snapshots to a directory where I can write, not ${dists_snapshots} 1>&2 |
| 403 |
fi |
| 404 |
fi |
| 405 |
|
| 406 |
if [ "${DO_MIRROR}" ] ; then |
| 407 |
run_debmirror |
| 408 |
else |
| 409 |
echo ---------- skipped mirror step |
| 410 |
fi |
| 411 |
|
| 412 |
if [ "$olddists" -a -r "$olddists" ] ; then |
| 413 |
#the mirror was updated, so we check the difference |
| 414 |
run_debdeltas |
| 415 |
else |
| 416 |
test "$DEBUG" && echo olddists is not defined or is not readable : "$olddists" |
| 417 |
fi |
| 418 |
|
| 419 |
##enable this to try to create all leftover deltas |
| 420 |
if test "$RECOVER" ; then |
| 421 |
for olddists in ${TMPDIR:-/tmp}/olddists* ; do |
| 422 |
if [ "$olddists" -a -r "$olddists" ] ; then |
| 423 |
echo ----------------------------------- creating deltas wrt $olddists |
| 424 |
run_debdeltas |
| 425 |
fi |
| 426 |
done |
| 427 |
fi |