]> git.saurik.com Git - apt.git/blame - debian/apt.cron.daily
merged from lp:~mvo/apt/mvo
[apt.git] / debian / apt.cron.daily
CommitLineData
0c132682
MZ
1#!/bin/sh
2#
0c132682
MZ
3
4#set -e
8a139d4d
MV
5#
6# This file understands the following apt configuration variables:
ff7c76f8
OA
7# Values here are the default.
8# Create /etc/apt/apt.conf.d/02periodic file to set your preference.
8a139d4d 9#
ff7c76f8
OA
10# Dir "/";
11# - RootDir for all configuration files
8a139d4d 12#
ff7c76f8
OA
13# Dir::Cache "var/apt/cache/";
14# - Set apt package cache directory
15#
16# Dir::Cache::Archive "archives/";
17# - Set package archive directory
18#
17443d48
JAK
19# APT::Periodic::Enable "1";
20# - Enable the update/upgrade script (0=disable)
21#
ff7c76f8
OA
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)
8a139d4d
MV
33# - Set maximum allowed age of a cache package file. If a cache
34# package file is older it is deleted (0=disable)
35#
ff7c76f8
OA
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)
8a139d4d
MV
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#
ff7c76f8 49# APT::Periodic::Update-Package-Lists "0";
8a139d4d
MV
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)
8a139d4d 54#
6b519e42
JAK
55# APT::Periodic::Download-Upgradeable-Packages-Debdelta "1";
56# - Use debdelta-upgrade to download updates if available (0=disable)
57#
ff7c76f8 58# APT::Periodic::Unattended-Upgrade "0";
fdd15654
MV
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#
8a139d4d
MV
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#
0c132682 79
05f6a46a 80check_stamp()
0c132682 81{
05f6a46a
MZ
82 stamp="$1"
83 interval="$2"
84
10946ddc 85 if [ $interval -eq 0 ]; then
53391d0f 86 debug_echo "check_stamp: interval=0"
ff7c76f8 87 # treat as no time has passed
10946ddc
MZ
88 return 1
89 fi
90
05f6a46a 91 if [ ! -f $stamp ]; then
ff7c76f8
OA
92 debug_echo "check_stamp: missing time stamp file: $stamp."
93 # treat as enough time has passed
05f6a46a 94 return 0
0c132682 95 fi
05f6a46a
MZ
96
97 # compare midnight today to midnight the day the stamp was updated
9c2d35fa
MV
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
05f6a46a 117 delta=$(($now-$stamp))
05f6a46a 118
8a139d4d
MV
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
adbce121
MV
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
6e7c6c3f
MV
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
05f6a46a
MZ
138 if [ $delta -ge $interval ]; then
139 return 0
140 fi
141
142 return 1
143}
144
145update_stamp()
146{
147 stamp="$1"
148
149 touch $stamp
0c132682
MZ
150}
151
6cce801a 152# we check here if autoclean was enough sizewise
2e8a92e5 153check_size_constraints()
6cce801a 154{
6cce801a 155 MaxAge=0
6cce801a 156 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
c98870b0
MV
157 eval $(apt-config shell MaxAge APT::Periodic::MaxAge)
158
159 MinAge=2
8a139d4d 160 eval $(apt-config shell MinAge APT::Archives::MinAge)
c98870b0
MV
161 eval $(apt-config shell MinAge APT::Periodic::MinAge)
162
163 MaxSize=0
6cce801a 164 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
c98870b0
MV
165 eval $(apt-config shell MaxSize APT::Periodic::MaxSize)
166
167 CacheDir="var/cache/apt/"
6cce801a 168 eval $(apt-config shell CacheDir Dir::Cache)
c98870b0
MV
169 CacheDir=${CacheDir%/}
170
171 CacheArchive="archives/"
6cce801a 172 eval $(apt-config shell CacheArchive Dir::Cache::archives)
c98870b0 173 CacheArchive=${CacheArchive%/}
6cce801a
MV
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
01717245
MV
180
181 Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
6cce801a
MV
182
183 # check age
8a139d4d 184 if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
c98870b0 185 debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge and ctime>$MinAge and mtime>$MinAge"
8e29e348 186 find $Cache -name "*.deb" \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
8a139d4d 187 elif [ ! $MaxAge -eq 0 ]; then
c98870b0 188 debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge only"
8e29e348 189 find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
c98870b0
MV
190 else
191 debug_echo "skip aging since MaxAge is 0"
6cce801a
MV
192 fi
193
194 # check size
195 if [ ! $MaxSize -eq 0 ]; then
8a139d4d
MV
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
6cce801a 203 # reverse-sort by mtime
3408b58c 204 for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
6cce801a
MV
205 du=$(du -s $Cache)
206 size=${du%%/*}
207 # check if the cache is small enough
208 if [ $size -lt $MaxSize ]; then
c98870b0 209 debug_echo "end remove by archive size: size=$size < $MaxSize"
6cce801a
MV
210 break
211 fi
8a139d4d
MV
212
213 # check for MinAge of the file
c98870b0 214 if [ $MinAge -ne 0 ]; then
8e29e348 215 # check both ctime and mtime
3971f8e8 216 mtime=$(stat -c %Y $file)
8e29e348
MV
217 ctime=$(stat -c %Z $file)
218 if [ $mtime -gt $ctime ]; then
219 delta=$(($now-$mtime))
220 else
221 delta=$(($now-$ctime))
222 fi
8a139d4d 223 if [ $delta -le $MinAge ]; then
c98870b0 224 debug_echo "skip remove by archive size: $file, delta=$delta < $MinAgeSec"
3408b58c 225 break
c98870b0
MV
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
8a139d4d
MV
230 fi
231 fi
6cce801a
MV
232 done
233 fi
234}
235
c98870b0
MV
236# deal with the Apt::Periodic::BackupArchiveInterval
237do_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
d047c6da 311# sleep for a random interval of time (default 30min)
604c9ee8
MV
312# (some code taken from cron-apt, thanks)
313random_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))
c98870b0 325 debug_echo "sleeping for $TIME seconds"
604c9ee8
MV
326 sleep $TIME
327}
328
604c9ee8 329
ff7c76f8 330debug_echo()
6cce801a 331{
ff7c76f8
OA
332 # Display message if $VERBOSE >= 1
333 if [ "$VERBOSE" -ge 1 ]; then
334 echo $1 1>&2
6cce801a 335 fi
ff7c76f8
OA
336}
337
c98870b0 338# ------------------------ main ----------------------------
69c28efc 339
ff7c76f8
OA
340# check apt-config exstance
341if ! which apt-config >/dev/null ; then
18d38975
OS
342 exit 0
343fi
87ddfb96 344
17443d48
JAK
345# check if the user really wants to do something
346AutoAptEnable=1 # default is yes
347eval $(apt-config shell AutoAptEnable APT::Periodic::Enable)
348
9a64707c 349if [ $AutoAptEnable -eq 0 ]; then
17443d48
JAK
350 exit 0
351fi
352
ff7c76f8 353# Set VERBOSE mode from apt-config (or inherit from environment)
6985efb3 354VERBOSE=0
ff7c76f8 355eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
c98870b0 356debug_echo "verbose level $VERBOSE"
ff7c76f8
OA
357if [ "$VERBOSE" -le 2 ]; then
358 # quiet for 0,1,2
359 XSTDOUT=">/dev/null"
360 XSTDERR="2>/dev/null"
361 XAPTOPT="-qq"
362 XUUPOPT=""
363else
364 XSTDOUT=""
365 XSTDERR=""
366 XAPTOPT=""
367 XUUPOPT="-d"
368fi
369if [ "$VERBOSE" -ge 3 ]; then
370 # trace output
371 set -x
372fi
8a139d4d 373
ff7c76f8
OA
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
379if which on_ac_power >/dev/null; then
380 on_ac_power
381 POWER=$?
382 if [ $POWER -eq 1 ]; then
c98870b0 383 debug_echo "exit: system NOT on main power"
18d38975 384 exit 0
ff7c76f8 385 elif [ $POWER -ne 0 ]; then
c98870b0 386 debug_echo "power status ($POWER) undetermined, continuing"
6cce801a 387 fi
ff7c76f8
OA
388 debug_echo "system is on main power."
389fi
8a139d4d 390
ff7c76f8 391# check if we can lock the cache and if the cache is clean
aff278bf 392if which apt-get >/dev/null && ! eval apt-get check -f $XAPTOPT $XSTDERR ; then
ff7c76f8
OA
393 debug_echo "error encountered in cron job with \"apt-get check\"."
394 exit 0
18d38975 395fi
87ddfb96 396
ff7c76f8
OA
397# Global current time in seconds since 1970-01-01 00:00:00 UTC
398now=$(date +%s)
6cce801a 399
ff7c76f8
OA
400# Support old Archive for compatibility.
401# Document only Periodic for all controling parameters of this script.
ff7c76f8 402
0c132682 403UpdateInterval=0
05f6a46a 404DownloadUpgradeableInterval=0
9bd1cf87
MZ
405eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
406AutocleanInterval=$DownloadUpgradeableInterval
0eb7056b 407eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
fdd15654
MV
408UnattendedUpgradeInterval=0
409eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
410
ff7c76f8
OA
411AutocleanInterval=0
412eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
413
c98870b0
MV
414BackupArchiveInterval=0
415eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
ff7c76f8 416
6b519e42
JAK
417Debdelta=1
418eval $(apt-config shell Debdelta APT::Periodic::Download-Upgradeable-Packages-Debdelta)
419
879cbcc8 420# check if we actually have to do anything that requires locking the cache
5f71505b
MV
421if [ $UpdateInterval -eq 0 ] &&
422 [ $DownloadUpgradeableInterval -eq 0 ] &&
423 [ $UnattendedUpgradeInterval -eq 0 ] &&
424 [ $AutocleanInterval -eq 0 ]; then
879cbcc8
DK
425
426 # check cache size
427 check_size_constraints
428
5f71505b
MV
429 exit 0
430fi
fdd15654 431
55fb35e8
MV
432# set the proxy based on the admin users gconf settings
433admin_user=$(getent group admin|cut -d: -f4|cut -d, -f1)
434if [ -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
0c132682
MZ
441fi
442
55fb35e8 443
c98870b0
MV
444# deal with BackupArchiveInterval
445do_cache_backup $BackupArchiveInterval
0c132682 446
5f71505b
MV
447# sleep random amount of time to avoid hitting the
448# mirrors at the same time
69c28efc
MV
449random_sleep
450
ff7c76f8 451# update package lists
aff278bf 452UPDATED=0
05f6a46a
MZ
453UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
454if check_stamp $UPDATE_STAMP $UpdateInterval; then
2ce23a2a 455 # check for a new archive signing key (against the master keyring)
55fb35e8
MV
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
fae49a71 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
ff7c76f8 462 debug_echo "download updated metadata (success)."
be993931 463 if which dbus-send >/dev/null && pidof dbus-daemon >/dev/null; then
ff7c76f8
OA
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)"
05f6a46a 471 fi
ff7c76f8 472 update_stamp $UPDATE_STAMP
aff278bf 473 UPDATED=1
c2d0fdd1
MV
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
ff7c76f8 479 else
c98870b0 480 debug_echo "download updated metadata (error)"
05f6a46a 481 fi
ff7c76f8
OA
482else
483 debug_echo "download updated metadata (not run)."
0c132682 484fi
c98870b0
MV
485
486# download all upgradeable packages (if it is requested)
05f6a46a 487DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
aff278bf 488if [ $UPDATED -eq 1 ] && check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
6b519e42
JAK
489 if [ $Debdelta -eq 1 ]; then
490 debdelta-upgrade >/dev/null 2>&1 || true
491 fi
c98870b0
MV
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
498else
499 debug_echo "download upgradable (not run)"
0c132682 500fi
9bd1cf87 501
c98870b0 502# auto upgrade all upgradeable packages
fdd15654 503UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
aff278bf 504if [ $UPDATED -eq 1 ] && which unattended-upgrade >/dev/null && check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
c98870b0
MV
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
511else
512 debug_echo "unattended-upgrade (not run)"
fdd15654
MV
513fi
514
ff7c76f8 515# autoclean package archive
de15fbae
MV
516AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
517if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
c98870b0 518 if eval apt-get $XAPTOPT -y autoclean $XSTDERR; then
ff7c76f8
OA
519 debug_echo "autoclean (success)."
520 update_stamp $AUTOCLEAN_STAMP
521 else
c98870b0 522 debug_echo "autoclean (error)"
ff7c76f8
OA
523 fi
524else
c98870b0 525 debug_echo "autoclean (not run)"
de15fbae
MV
526fi
527
47536509
MV
528# check cache size
529check_size_constraints
0fad7309 530
ff7c76f8
OA
531#
532# vim: set sts=4 ai :
533#
534