doc/examples/configure-index:
[ntk/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#
ff7c76f8
OA
11# Dir::Cache "var/apt/cache/";
12# - Set apt package cache directory
13#
14# Dir::Cache::Archive "archives/";
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
37# will not be deleted (0=disable). Usefull to prevent races
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
44# requirement is met (the biggest packages will be deleted
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
94 # certain dates (eg America/Sao_Paulo), if date returns with error
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
104 # on certain dates (eg America/Sao_Paulo), if date returns with error
105 # return 0.
106 return 0
107 fi
108
05f6a46a 109 delta=$(($now-$stamp))
05f6a46a 110
ff7c76f8 111 # intervall 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
150 CacheDir="var/cache/apt/"
6cce801a 151 eval $(apt-config shell CacheDir Dir::Cache)
c98870b0
MV
152 CacheDir=${CacheDir%/}
153
154 CacheArchive="archives/"
6cce801a 155 eval $(apt-config shell CacheArchive Dir::Cache::archives)
c98870b0 156 CacheArchive=${CacheArchive%/}
6cce801a
MV
157
158 # sanity check
159 if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
160 echo "empty Dir::Cache or Dir::Cache::archives, exiting"
161 exit
162 fi
01717245
MV
163
164 Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
6cce801a
MV
165
166 # check age
8a139d4d 167 if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
c98870b0 168 debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge and ctime>$MinAge and mtime>$MinAge"
8e29e348 169 find $Cache -name "*.deb" \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
8a139d4d 170 elif [ ! $MaxAge -eq 0 ]; then
c98870b0 171 debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge only"
8e29e348 172 find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
c98870b0
MV
173 else
174 debug_echo "skip aging since MaxAge is 0"
6cce801a
MV
175 fi
176
177 # check size
178 if [ ! $MaxSize -eq 0 ]; then
8a139d4d
MV
179 # maxSize is in MB
180 MaxSize=$(($MaxSize*1024))
181
182 #get current time
183 now=$(date --date=$(date --iso-8601) +%s)
184 MinAge=$(($MinAge*24*60*60))
185
6cce801a 186 # reverse-sort by mtime
3408b58c 187 for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
6cce801a
MV
188 du=$(du -s $Cache)
189 size=${du%%/*}
190 # check if the cache is small enough
191 if [ $size -lt $MaxSize ]; then
c98870b0 192 debug_echo "end remove by archive size: size=$size < $MaxSize"
6cce801a
MV
193 break
194 fi
8a139d4d
MV
195
196 # check for MinAge of the file
c98870b0 197 if [ $MinAge -ne 0 ]; then
8e29e348 198 # check both ctime and mtime
3971f8e8 199 mtime=$(stat -c %Y $file)
8e29e348
MV
200 ctime=$(stat -c %Z $file)
201 if [ $mtime -gt $ctime ]; then
202 delta=$(($now-$mtime))
203 else
204 delta=$(($now-$ctime))
205 fi
8a139d4d 206 if [ $delta -le $MinAge ]; then
c98870b0 207 debug_echo "skip remove by archive size: $file, delta=$delta < $MinAgeSec"
3408b58c 208 break
c98870b0
MV
209 else
210 # delete oldest file
211 debug_echo "remove by archive size: $file, delta=$delta >= $MinAgeSec (sec), size=$size >= $MaxSize"
212 rm -f $file
8a139d4d
MV
213 fi
214 fi
6cce801a
MV
215 done
216 fi
217}
218
c98870b0
MV
219# deal with the Apt::Periodic::BackupArchiveInterval
220do_cache_backup()
221{
222 BackupArchiveInterval="$1"
223 if [ $BackupArchiveInterval -eq 0 ]; then
224 return
225 fi
226
227 # Set default values and normalize
228 Dir="/"
229 eval $(apt-config shell Dir Dir)
230 Dir=${Dir%/}
231
232 CacheDir="var/cache/apt/"
233 eval $(apt-config shell CacheDir Dir::Cache)
234 CacheDir=${CacheDir%/}
235 if [ -z "$CacheDir" ]; then
236 debug_echo "practically empty Dir::Cache, exiting"
237 return 0
238 fi
239
240 CacheArchive="archives/"
241 eval $(apt-config shell CacheArchive Dir::Cache::Archives)
242 CacheArchive=${CacheArchive%/}
243 if [ -z "$CacheArchive" ]; then
244 debug_echo "practically empty Dir::Cache::archives, exiting"
245 return 0
246 fi
247
248 BackupLevel=3
249 eval $(apt-config shell BackupLevel APT::Periodic::BackupLevel)
250 if [ $BackupLevel -le 1 ]; then
251 BackupLevel=2 ;
252 fi
253
254 CacheBackup="backup/"
255 eval $(apt-config shell CacheBackup Dir::Cache::Backup)
256 CacheBackup=${CacheBackup%/}
257 if [ -z "$CacheBackup" ]; then
258 echo "practically empty Dir::Cache::Backup, exiting" 1>&2
259 return
260 fi
261
262 Cache="${Dir}/${CacheDir}/${CacheArchive}/"
263 Back="${Dir}/${CacheDir}/${CacheBackup}/"
264 BackX="${Back}${CacheArchive}/"
265 for x in $(seq 0 1 $((${BackupLevel}-1))); do
266 eval "Back${x}=${Back}${x}/"
267 done
268
269 # backup after n-days if archive contents changed.
270 # (This uses hardlink to save disk space)
271 BACKUP_ARCHIVE_STAMP=/var/lib/apt/periodic/backup-archive-stamp
272 if check_stamp $BACKUP_ARCHIVE_STAMP $BackupArchiveInterval; then
273 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
274 mkdir -p $Back
275 rm -rf $Back$((${BackupLevel}-1))
276 for y in $(seq $((${BackupLevel}-1)) -1 1); do
277 eval BackY=${Back}$y
278 eval BackZ=${Back}$(($y-1))
279 if [ -e $BackZ ]; then
280 mv -f $BackZ $BackY ;
281 fi
282 done
283 cp -la $Cache $Back ; mv -f $BackX $Back0
284 update_stamp $BACKUP_ARCHIVE_STAMP
285 debug_echo "backup with hardlinks. (success)"
286 else
287 debug_echo "skip backup since same content."
288 fi
289 else
290 debug_echo "skip backup since too new."
291 fi
292}
293
d047c6da 294# sleep for a random interval of time (default 30min)
69c28efc
MV
295# (some code taken from cron-apt, thanks)
296random_sleep()
297{
298 RandomSleep=1800
299 eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
300 if [ $RandomSleep -eq 0 ]; then
301 return
302 fi
303 if [ -z "$RANDOM" ] ; then
304 # A fix for shells that do not have this bash feature.
305 RANDOM=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -c"1-5")
306 fi
307 TIME=$(($RANDOM % $RandomSleep))
c98870b0 308 debug_echo "sleeping for $TIME seconds"
69c28efc
MV
309 sleep $TIME
310}
311
69c28efc 312
ff7c76f8 313debug_echo()
6cce801a 314{
ff7c76f8
OA
315 # Display message if $VERBOSE >= 1
316 if [ "$VERBOSE" -ge 1 ]; then
317 echo $1 1>&2
6cce801a 318 fi
ff7c76f8
OA
319}
320
c98870b0 321# ------------------------ main ----------------------------
69c28efc 322
a23a7811
DK
323# Backup the 7 last versions of APT's extended_states file
324# shameless copy from dpkg cron
325if cd /var/backups ; then
326 if ! cmp -s apt.extended_states.0 /var/lib/apt/extended_states; then
327 cp -p /var/lib/apt/extended_states apt.extended_states
328 savelog -c 7 apt.extended_states >/dev/null
329 fi
330fi
331
ff7c76f8
OA
332# check apt-config exstance
333if ! which apt-config >/dev/null ; then
18d38975
OS
334 exit 0
335fi
87ddfb96 336
17443d48
JAK
337# check if the user really wants to do something
338AutoAptEnable=1 # default is yes
339eval $(apt-config shell AutoAptEnable APT::Periodic::Enable)
340
9a64707c 341if [ $AutoAptEnable -eq 0 ]; then
17443d48
JAK
342 exit 0
343fi
344
ff7c76f8 345# Set VERBOSE mode from apt-config (or inherit from environment)
6985efb3 346VERBOSE=0
ff7c76f8 347eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
c98870b0 348debug_echo "verbose level $VERBOSE"
ff7c76f8
OA
349if [ "$VERBOSE" -le 2 ]; then
350 # quiet for 0,1,2
351 XSTDOUT=">/dev/null"
352 XSTDERR="2>/dev/null"
353 XAPTOPT="-qq"
354 XUUPOPT=""
355else
356 XSTDOUT=""
357 XSTDERR=""
358 XAPTOPT=""
359 XUUPOPT="-d"
360fi
361if [ "$VERBOSE" -ge 3 ]; then
362 # trace output
363 set -x
364fi
8a139d4d 365
ff7c76f8
OA
366# laptop check, on_ac_power returns:
367# 0 (true) System is on main power
368# 1 (false) System is not on main power
369# 255 (false) Power status could not be determined
370# Desktop systems always return 255 it seems
371if which on_ac_power >/dev/null; then
372 on_ac_power
373 POWER=$?
374 if [ $POWER -eq 1 ]; then
c98870b0 375 debug_echo "exit: system NOT on main power"
ff7c76f8
OA
376 exit 0
377 elif [ $POWER -ne 0 ]; then
c98870b0 378 debug_echo "power status ($POWER) undetermined, continuing"
6cce801a 379 fi
ff7c76f8
OA
380 debug_echo "system is on main power."
381fi
8a139d4d 382
ff7c76f8 383# check if we can lock the cache and if the cache is clean
aff278bf 384if which apt-get >/dev/null && ! eval apt-get check -f $XAPTOPT $XSTDERR ; then
ff7c76f8
OA
385 debug_echo "error encountered in cron job with \"apt-get check\"."
386 exit 0
387fi
8a139d4d 388
ff7c76f8
OA
389# Global current time in seconds since 1970-01-01 00:00:00 UTC
390now=$(date +%s)
6cce801a 391
ff7c76f8
OA
392# Support old Archive for compatibility.
393# Document only Periodic for all controling parameters of this script.
ff7c76f8 394
0c132682 395UpdateInterval=0
ff7c76f8
OA
396eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists)
397
05f6a46a 398DownloadUpgradeableInterval=0
ff7c76f8 399eval $(apt-config shell DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
0c132682 400
fdd15654
MV
401UnattendedUpgradeInterval=0
402eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
403
ff7c76f8
OA
404AutocleanInterval=0
405eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
406
c98870b0
MV
407BackupArchiveInterval=0
408eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
ff7c76f8 409
6b519e42
JAK
410Debdelta=1
411eval $(apt-config shell Debdelta APT::Periodic::Download-Upgradeable-Packages-Debdelta)
412
879cbcc8 413# check if we actually have to do anything that requires locking the cache
4c2dcaa1
MV
414if [ $UpdateInterval -eq 0 ] &&
415 [ $DownloadUpgradeableInterval -eq 0 ] &&
416 [ $UnattendedUpgradeInterval -eq 0 ] &&
2783b261 417 [ $BackupArchiveInterval -eq 0 ] &&
4c2dcaa1 418 [ $AutocleanInterval -eq 0 ]; then
879cbcc8
DK
419
420 # check cache size
421 check_size_constraints
422
4c2dcaa1
MV
423 exit 0
424fi
fdd15654 425
c98870b0
MV
426# deal with BackupArchiveInterval
427do_cache_backup $BackupArchiveInterval
0c132682 428
4c2dcaa1
MV
429# sleep random amount of time to avoid hitting the
430# mirrors at the same time
69c28efc
MV
431random_sleep
432
ff7c76f8 433# update package lists
aff278bf 434UPDATED=0
05f6a46a
MZ
435UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
436if check_stamp $UPDATE_STAMP $UpdateInterval; then
ff7c76f8
OA
437 if eval apt-get $XAPTOPT -y update $XSTDERR; then
438 debug_echo "download updated metadata (success)."
be993931 439 if which dbus-send >/dev/null && pidof dbus-daemon >/dev/null; then
ff7c76f8
OA
440 if dbus-send --system / app.apt.dbus.updated boolean:true ; then
441 debug_echo "send dbus signal (success)"
442 else
443 debug_echo "send dbus signal (error)"
444 fi
445 else
446 debug_echo "dbus signal not send (command not available)"
05f6a46a 447 fi
ff7c76f8 448 update_stamp $UPDATE_STAMP
aff278bf 449 UPDATED=1
ff7c76f8 450 else
c98870b0 451 debug_echo "download updated metadata (error)"
05f6a46a 452 fi
ff7c76f8
OA
453else
454 debug_echo "download updated metadata (not run)."
fdd15654 455fi
c98870b0
MV
456
457# download all upgradeable packages (if it is requested)
458DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
aff278bf 459if [ $UPDATED -eq 1 ] && check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
6b519e42
JAK
460 if [ $Debdelta -eq 1 ]; then
461 debdelta-upgrade >/dev/null 2>&1 || true
462 fi
c98870b0
MV
463 if eval apt-get $XAPTOPT -y -d dist-upgrade $XSTDERR; then
464 update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
465 debug_echo "download upgradable (success)"
466 else
467 debug_echo "download upgradable (error)"
468 fi
469else
470 debug_echo "download upgradable (not run)"
471fi
472
473# auto upgrade all upgradeable packages
474UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
aff278bf 475if [ $UPDATED -eq 1 ] && which unattended-upgrade >/dev/null && check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
c98870b0
MV
476 if unattended-upgrade $XUUPOPT; then
477 update_stamp $UPGRADE_STAMP
478 debug_echo "unattended-upgrade (success)"
479 else
480 debug_echo "unattended-upgrade (error)"
481 fi
482else
483 debug_echo "unattended-upgrade (not run)"
484fi
fdd15654 485
ff7c76f8 486# autoclean package archive
de15fbae
MV
487AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
488if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
c98870b0 489 if eval apt-get $XAPTOPT -y autoclean $XSTDERR; then
ff7c76f8
OA
490 debug_echo "autoclean (success)."
491 update_stamp $AUTOCLEAN_STAMP
492 else
c98870b0 493 debug_echo "autoclean (error)"
ff7c76f8
OA
494 fi
495else
c98870b0 496 debug_echo "autoclean (not run)"
de15fbae
MV
497fi
498
0fad7309
MV
499# check cache size
500check_size_constraints
501
ff7c76f8
OA
502#
503# vim: set sts=4 ai :
504#
505