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