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