]> git.saurik.com Git - apt.git/blob - debian/apt.cron.daily
merged from lp:~mvo/apt/mvo
[apt.git] / debian / apt.cron.daily
1 #!/bin/sh
2 #
3
4 #set -e
5 #
6 # This file understands the following apt configuration variables:
7 # Values here are the default.
8 # Create /etc/apt/apt.conf.d/02periodic file to set your preference.
9 #
10 # Dir "/";
11 # - RootDir for all configuration files
12 #
13 # Dir::Cache "var/apt/cache/";
14 # - Set apt package cache directory
15 #
16 # Dir::Cache::Archive "archives/";
17 # - Set package archive directory
18 #
19 # APT::Periodic::Enable "1";
20 # - Enable the update/upgrade script (0=disable)
21 #
22 # APT::Periodic::BackupArchiveInterval "0";
23 # - Backup after n-days if archive contents changed.(0=disable)
24 #
25 # APT::Periodic::BackupLevel "3";
26 # - Backup level.(0=disable), 1 is invalid.
27 #
28 # Dir::Cache::Backup "backup/";
29 # - Set periodic package backup directory
30 #
31 # APT::Archives::MaxAge "0"; (old, deprecated)
32 # APT::Periodic::MaxAge "0"; (new)
33 # - Set maximum allowed age of a cache package file. If a cache
34 # package file is older it is deleted (0=disable)
35 #
36 # APT::Archives::MinAge "2"; (old, deprecated)
37 # APT::Periodic::MinAge "2"; (new)
38 # - Set minimum age of a package file. If a file is younger it
39 # will not be deleted (0=disable). Usefull to prevent races
40 # and to keep backups of the packages for emergency.
41 #
42 # APT::Archives::MaxSize "0"; (old, deprecated)
43 # APT::Periodic::MaxSize "0"; (new)
44 # - Set maximum size of the cache in MB (0=disable). If the cache
45 # is bigger, cached package files are deleted until the size
46 # requirement is met (the biggest packages will be deleted
47 # first).
48 #
49 # APT::Periodic::Update-Package-Lists "0";
50 # - Do "apt-get update" automatically every n-days (0=disable)
51 #
52 # "APT::Periodic::Download-Upgradeable-Packages=0",
53 # - Do "apt-get upgrade --download-only" every n-days (0=disable)
54 #
55 # APT::Periodic::Download-Upgradeable-Packages-Debdelta "1";
56 # - Use debdelta-upgrade to download updates if available (0=disable)
57 #
58 # APT::Periodic::Unattended-Upgrade "0";
59 # - Run the "unattended-upgrade" security upgrade script
60 # every n-days (0=disabled)
61 # Requires the package "unattended-upgrades" and will write
62 # a log in /var/log/unattended-upgrades
63 #
64 # "APT::Archives::MaxAge",
65 # - Set maximum allowed age of a cache package file. If a cache
66 # package file is older it is deleted (0=disable)
67 #
68 # "APT::Archives::MaxSize",
69 # - Set maximum size of the cache in MB (0=disable). If the cache
70 # is bigger, cached package files are deleted until the size
71 # requirement is met (the biggest packages will be deleted
72 # first).
73 #
74 # "APT::Archives::MinAge"
75 # - Set minimum age of a package file. If a file is younger it
76 # will not be deleted (0=disable). Usefull to prevent races
77 # and to keep backups of the packages for emergency.
78 #
79
80 check_stamp()
81 {
82 stamp="$1"
83 interval="$2"
84
85 if [ $interval -eq 0 ]; then
86 debug_echo "check_stamp: interval=0"
87 # treat as no time has passed
88 return 1
89 fi
90
91 if [ ! -f $stamp ]; then
92 debug_echo "check_stamp: missing time stamp file: $stamp."
93 # treat as enough time has passed
94 return 0
95 fi
96
97 # compare midnight today to midnight the day the stamp was updated
98 stamp_file="$stamp"
99 stamp=$(date --date=$(date -r $stamp_file --iso-8601) +%s 2>/dev/null)
100 if [ "$?" != "0" ]; then
101 # Due to some timezones returning 'invalid date' for midnight on
102 # certain dates (eg America/Sao_Paulo), if date returns with error
103 # remove the stamp file and return 0. See coreutils bug:
104 # http://lists.gnu.org/archive/html/bug-coreutils/2007-09/msg00176.html
105 rm -f "$stamp_file"
106 return 0
107 fi
108
109 now=$(date --date=$(date --iso-8601) +%s 2>/dev/null)
110 if [ "$?" != "0" ]; then
111 # As above, due to some timezones returning 'invalid date' for midnight
112 # on certain dates (eg America/Sao_Paulo), if date returns with error
113 # return 0.
114 return 0
115 fi
116
117 delta=$(($now-$stamp))
118
119 # intervall is in days,
120 interval=$(($interval*60*60*24))
121 #echo "stampfile: $1"
122 #echo "interval=$interval, now=$now, stamp=$stamp, delta=$delta"
123
124 # remove timestamps a day (or more) in the future and force re-check
125 if [ $stamp -gt $(($now+86400)) ]; then
126 echo "WARNING: file $stamp_file has a timestamp in the future: $stamp"
127 rm -f "$stamp_file"
128 return 0
129 fi
130
131 # remove timestamps a day (or more) in the future and force re-check
132 if [ $stamp -gt $(($now+86400)) ]; then
133 echo "WARNING: file $stamp_file has a timestamp in the future: $stamp"
134 rm -f "$stamp_file"
135 return 0
136 fi
137
138 if [ $delta -ge $interval ]; then
139 return 0
140 fi
141
142 return 1
143 }
144
145 update_stamp()
146 {
147 stamp="$1"
148
149 touch $stamp
150 }
151
152 # we check here if autoclean was enough sizewise
153 check_size_constraints()
154 {
155 MaxAge=0
156 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
157 eval $(apt-config shell MaxAge APT::Periodic::MaxAge)
158
159 MinAge=2
160 eval $(apt-config shell MinAge APT::Archives::MinAge)
161 eval $(apt-config shell MinAge APT::Periodic::MinAge)
162
163 MaxSize=0
164 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
165 eval $(apt-config shell MaxSize APT::Periodic::MaxSize)
166
167 CacheDir="var/cache/apt/"
168 eval $(apt-config shell CacheDir Dir::Cache)
169 CacheDir=${CacheDir%/}
170
171 CacheArchive="archives/"
172 eval $(apt-config shell CacheArchive Dir::Cache::archives)
173 CacheArchive=${CacheArchive%/}
174
175 # sanity check
176 if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
177 echo "empty Dir::Cache or Dir::Cache::archives, exiting"
178 exit
179 fi
180
181 Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
182
183 # check age
184 if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
185 debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge and ctime>$MinAge and mtime>$MinAge"
186 find $Cache -name "*.deb" \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
187 elif [ ! $MaxAge -eq 0 ]; then
188 debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge only"
189 find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
190 else
191 debug_echo "skip aging since MaxAge is 0"
192 fi
193
194 # check size
195 if [ ! $MaxSize -eq 0 ]; then
196 # maxSize is in MB
197 MaxSize=$(($MaxSize*1024))
198
199 #get current time
200 now=$(date --date=$(date --iso-8601) +%s)
201 MinAge=$(($MinAge*24*60*60))
202
203 # reverse-sort by mtime
204 for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
205 du=$(du -s $Cache)
206 size=${du%%/*}
207 # check if the cache is small enough
208 if [ $size -lt $MaxSize ]; then
209 debug_echo "end remove by archive size: size=$size < $MaxSize"
210 break
211 fi
212
213 # check for MinAge of the file
214 if [ $MinAge -ne 0 ]; then
215 # check both ctime and mtime
216 mtime=$(stat -c %Y $file)
217 ctime=$(stat -c %Z $file)
218 if [ $mtime -gt $ctime ]; then
219 delta=$(($now-$mtime))
220 else
221 delta=$(($now-$ctime))
222 fi
223 if [ $delta -le $MinAge ]; then
224 debug_echo "skip remove by archive size: $file, delta=$delta < $MinAgeSec"
225 break
226 else
227 # delete oldest file
228 debug_echo "remove by archive size: $file, delta=$delta >= $MinAgeSec (sec), size=$size >= $MaxSize"
229 rm -f $file
230 fi
231 fi
232 done
233 fi
234 }
235
236 # deal with the Apt::Periodic::BackupArchiveInterval
237 do_cache_backup()
238 {
239 BackupArchiveInterval="$1"
240 if [ $BackupArchiveInterval -eq 0 ]; then
241 return
242 fi
243
244 # Set default values and normalize
245 Dir="/"
246 eval $(apt-config shell Dir Dir)
247 Dir=${Dir%/}
248
249 CacheDir="var/cache/apt/"
250 eval $(apt-config shell CacheDir Dir::Cache)
251 CacheDir=${CacheDir%/}
252 if [ -z "$CacheDir" ]; then
253 debug_echo "practically empty Dir::Cache, exiting"
254 return 0
255 fi
256
257 CacheArchive="archives/"
258 eval $(apt-config shell CacheArchive Dir::Cache::Archives)
259 CacheArchive=${CacheArchive%/}
260 if [ -z "$CacheArchive" ]; then
261 debug_echo "practically empty Dir::Cache::archives, exiting"
262 return 0
263 fi
264
265 BackupLevel=3
266 eval $(apt-config shell BackupLevel APT::Periodic::BackupLevel)
267 if [ $BackupLevel -le 1 ]; then
268 BackupLevel=2 ;
269 fi
270
271 CacheBackup="backup/"
272 eval $(apt-config shell CacheBackup Dir::Cache::Backup)
273 CacheBackup=${CacheBackup%/}
274 if [ -z "$CacheBackup" ]; then
275 echo "practically empty Dir::Cache::Backup, exiting" 1>&2
276 return
277 fi
278
279 Cache="${Dir}/${CacheDir}/${CacheArchive}/"
280 Back="${Dir}/${CacheDir}/${CacheBackup}/"
281 BackX="${Back}${CacheArchive}/"
282 for x in $(seq 0 1 $((${BackupLevel}-1))); do
283 eval "Back${x}=${Back}${x}/"
284 done
285
286 # backup after n-days if archive contents changed.
287 # (This uses hardlink to save disk space)
288 BACKUP_ARCHIVE_STAMP=/var/lib/apt/periodic/backup-archive-stamp
289 if check_stamp $BACKUP_ARCHIVE_STAMP $BackupArchiveInterval; then
290 if [ $({(cd $Cache 2>/dev/null; find . -name "*.deb"); (cd $Back0 2>/dev/null;find . -name "*.deb") ;}| sort|uniq -u|wc -l) -ne 0 ]; then
291 mkdir -p $Back
292 rm -rf $Back$((${BackupLevel}-1))
293 for y in $(seq $((${BackupLevel}-1)) -1 1); do
294 eval BackY=${Back}$y
295 eval BackZ=${Back}$(($y-1))
296 if [ -e $BackZ ]; then
297 mv -f $BackZ $BackY ;
298 fi
299 done
300 cp -la $Cache $Back ; mv -f $BackX $Back0
301 update_stamp $BACKUP_ARCHIVE_STAMP
302 debug_echo "backup with hardlinks. (success)"
303 else
304 debug_echo "skip backup since same content."
305 fi
306 else
307 debug_echo "skip backup since too new."
308 fi
309 }
310
311 # sleep for a random interval of time (default 30min)
312 # (some code taken from cron-apt, thanks)
313 random_sleep()
314 {
315 RandomSleep=1800
316 eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
317 if [ $RandomSleep -eq 0 ]; then
318 return
319 fi
320 if [ -z "$RANDOM" ] ; then
321 # A fix for shells that do not have this bash feature.
322 RANDOM=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -c"1-5")
323 fi
324 TIME=$(($RANDOM % $RandomSleep))
325 debug_echo "sleeping for $TIME seconds"
326 sleep $TIME
327 }
328
329
330 debug_echo()
331 {
332 # Display message if $VERBOSE >= 1
333 if [ "$VERBOSE" -ge 1 ]; then
334 echo $1 1>&2
335 fi
336 }
337
338 # ------------------------ main ----------------------------
339
340 # check apt-config exstance
341 if ! which apt-config >/dev/null ; then
342 exit 0
343 fi
344
345 # check if the user really wants to do something
346 AutoAptEnable=1 # default is yes
347 eval $(apt-config shell AutoAptEnable APT::Periodic::Enable)
348
349 if [ $AutoAptEnable -eq 0 ]; then
350 exit 0
351 fi
352
353 # Set VERBOSE mode from apt-config (or inherit from environment)
354 VERBOSE=0
355 eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
356 debug_echo "verbose level $VERBOSE"
357 if [ "$VERBOSE" -le 2 ]; then
358 # quiet for 0,1,2
359 XSTDOUT=">/dev/null"
360 XSTDERR="2>/dev/null"
361 XAPTOPT="-qq"
362 XUUPOPT=""
363 else
364 XSTDOUT=""
365 XSTDERR=""
366 XAPTOPT=""
367 XUUPOPT="-d"
368 fi
369 if [ "$VERBOSE" -ge 3 ]; then
370 # trace output
371 set -x
372 fi
373
374 # laptop check, on_ac_power returns:
375 # 0 (true) System is on main power
376 # 1 (false) System is not on main power
377 # 255 (false) Power status could not be determined
378 # Desktop systems always return 255 it seems
379 if which on_ac_power >/dev/null; then
380 on_ac_power
381 POWER=$?
382 if [ $POWER -eq 1 ]; then
383 debug_echo "exit: system NOT on main power"
384 exit 0
385 elif [ $POWER -ne 0 ]; then
386 debug_echo "power status ($POWER) undetermined, continuing"
387 fi
388 debug_echo "system is on main power."
389 fi
390
391 # check if we can lock the cache and if the cache is clean
392 if which apt-get >/dev/null && ! eval apt-get check -f $XAPTOPT $XSTDERR ; then
393 debug_echo "error encountered in cron job with \"apt-get check\"."
394 exit 0
395 fi
396
397 # Global current time in seconds since 1970-01-01 00:00:00 UTC
398 now=$(date +%s)
399
400 # Support old Archive for compatibility.
401 # Document only Periodic for all controling parameters of this script.
402
403 UpdateInterval=0
404 DownloadUpgradeableInterval=0
405 eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
406 AutocleanInterval=$DownloadUpgradeableInterval
407 eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
408 UnattendedUpgradeInterval=0
409 eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
410
411 AutocleanInterval=0
412 eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
413
414 BackupArchiveInterval=0
415 eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
416
417 Debdelta=1
418 eval $(apt-config shell Debdelta APT::Periodic::Download-Upgradeable-Packages-Debdelta)
419
420 # check if we actually have to do anything that requires locking the cache
421 if [ $UpdateInterval -eq 0 ] &&
422 [ $DownloadUpgradeableInterval -eq 0 ] &&
423 [ $UnattendedUpgradeInterval -eq 0 ] &&
424 [ $AutocleanInterval -eq 0 ]; then
425
426 # check cache size
427 check_size_constraints
428
429 exit 0
430 fi
431
432 # set the proxy based on the admin users gconf settings
433 admin_user=$(getent group admin|cut -d: -f4|cut -d, -f1)
434 if [ -n "$admin_user" ] && [ -x /usr/bin/sudo ] && [ -z "$http_proxy" ] && [ -x /usr/bin/gconftool ]; then
435 use=$(sudo -u "$admin_user" gconftool --get /system/http_proxy/use_http_proxy 2>/dev/null)
436 host=$(sudo -u "$admin_user" gconftool --get /system/http_proxy/host 2>/dev/null)
437 port=$(sudo -u "$admin_user" gconftool --get /system/http_proxy/port 2>/dev/null)
438 if [ "$use" = "true" ] && [ -n "$host" ] && [ -n "$port" ]; then
439 export http_proxy="http://$host:$port/"
440 fi
441 fi
442
443
444 # deal with BackupArchiveInterval
445 do_cache_backup $BackupArchiveInterval
446
447 # sleep random amount of time to avoid hitting the
448 # mirrors at the same time
449 random_sleep
450
451 # update package lists
452 UPDATED=0
453 UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
454 if check_stamp $UPDATE_STAMP $UpdateInterval; then
455 # check for a new archive signing key (against the master keyring)
456 if eval apt-key net-update $XSTDERR; then
457 debug_echo "apt-key net-update (success)"
458 else
459 debug_echo "apt-key net-update (failure)"
460 fi
461 if eval apt-get $XAPTOPT -y update -o APT::Update::Auth-Failure::=\"cp /usr/share/apt/apt-auth-failure.note /var/lib/update-notifier/user.d/\" $XSTDERR; then
462 debug_echo "download updated metadata (success)."
463 if which dbus-send >/dev/null && pidof dbus-daemon >/dev/null; then
464 if dbus-send --system / app.apt.dbus.updated boolean:true ; then
465 debug_echo "send dbus signal (success)"
466 else
467 debug_echo "send dbus signal (error)"
468 fi
469 else
470 debug_echo "dbus signal not send (command not available)"
471 fi
472 update_stamp $UPDATE_STAMP
473 UPDATED=1
474 # now run apt-xapian-index if it is installed to ensure the index
475 # is up-to-date
476 if [ -x /usr/sbin/update-apt-xapian-index ]; then
477 ionice -c3 update-apt-xapian-index -q
478 fi
479 else
480 debug_echo "download updated metadata (error)"
481 fi
482 else
483 debug_echo "download updated metadata (not run)."
484 fi
485
486 # download all upgradeable packages (if it is requested)
487 DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
488 if [ $UPDATED -eq 1 ] && check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
489 if [ $Debdelta -eq 1 ]; then
490 debdelta-upgrade >/dev/null 2>&1 || true
491 fi
492 if eval apt-get $XAPTOPT -y -d dist-upgrade $XSTDERR; then
493 update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
494 debug_echo "download upgradable (success)"
495 else
496 debug_echo "download upgradable (error)"
497 fi
498 else
499 debug_echo "download upgradable (not run)"
500 fi
501
502 # auto upgrade all upgradeable packages
503 UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
504 if [ $UPDATED -eq 1 ] && which unattended-upgrade >/dev/null && check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
505 if unattended-upgrade $XUUPOPT; then
506 update_stamp $UPGRADE_STAMP
507 debug_echo "unattended-upgrade (success)"
508 else
509 debug_echo "unattended-upgrade (error)"
510 fi
511 else
512 debug_echo "unattended-upgrade (not run)"
513 fi
514
515 # autoclean package archive
516 AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
517 if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
518 if eval apt-get $XAPTOPT -y autoclean $XSTDERR; then
519 debug_echo "autoclean (success)."
520 update_stamp $AUTOCLEAN_STAMP
521 else
522 debug_echo "autoclean (error)"
523 fi
524 else
525 debug_echo "autoclean (not run)"
526 fi
527
528 # check cache size
529 check_size_constraints
530
531 #
532 # vim: set sts=4 ai :
533 #
534