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