]> git.saurik.com Git - apt.git/blame_incremental - debian/apt.cron.daily
* apt-pkg/indexrecords.cc:
[apt.git] / debian / apt.cron.daily
... / ...
CommitLineData
1#!/bin/sh
2#set -e
3#
4# This file understands the following apt configuration variables:
5# Values here are the default.
6# Create /etc/apt/apt.conf.d/02periodic file to set your preference.
7#
8# Dir "/";
9# - RootDir for all configuration files
10#
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)
28# - Set maximum allowed age of a cache package file. If a cache
29# package file is older it is deleted (0=disable)
30#
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)
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#
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)
49#
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
55#
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
65
66check_stamp()
67{
68 stamp="$1"
69 interval="$2"
70
71 if [ $interval -eq 0 ]; then
72 debug_echo "check_stamp: interval=0."
73 # treat as no time has passed
74 return 1
75 fi
76
77 if [ ! -f $stamp ]; then
78 update_stamp $stamp
79 debug_echo "check_stamp: missing time stamp file: $stamp."
80 # treat as enough time has passed
81 return 0
82 fi
83
84 # compare midnight today to midnight the day the stamp was updated
85 stamp=$(date -r $stamp '+%s')
86 delta=$(($now-$stamp))
87
88 # intervall is in days, convert to sec.
89 interval=$(($interval*60*60*24))
90 debug_echo "check_stamp: interval=$interval, now=$now, stamp=$stamp, delta=$delta (sec)"
91
92 if [ $delta -ge $interval ]; then
93 return 0
94 fi
95
96 return 1
97}
98
99update_stamp()
100{
101 stamp="$1"
102 touch $stamp
103}
104
105# we check here if autoclean was enough sizewise
106check_size_constraints()
107{
108 # min-age in days
109 MaxAge=0
110 MinAge=2
111 MaxSize=0
112 CacheDir="var/cache/apt"
113 CacheArchive="archives/"
114 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
115 eval $(apt-config shell MinAge APT::Archives::MinAge)
116 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
117 eval $(apt-config shell Dir Dir)
118 eval $(apt-config shell CacheDir Dir::Cache)
119 eval $(apt-config shell CacheArchive Dir::Cache::archives)
120
121 # sanity check
122 if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
123 echo "empty Dir::Cache or Dir::Cache::archives, exiting"
124 exit
125 fi
126
127 Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
128
129 # check age
130 if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
131 find $Cache -name "*.deb" \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
132 elif [ ! $MaxAge -eq 0 ]; then
133 find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
134 fi
135
136 # check size
137 if [ ! $MaxSize -eq 0 ]; then
138 # maxSize is in MB
139 MaxSize=$(($MaxSize*1024))
140
141 #get current time
142 now=$(date --date=$(date --iso-8601) +%s)
143 MinAge=$(($MinAge*24*60*60))
144
145 # reverse-sort by mtime
146 for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
147 du=$(du -s $Cache)
148 size=${du%%/*}
149 # check if the cache is small enough
150 if [ $size -lt $MaxSize ]; then
151 break
152 fi
153
154 # check for MinAge of the file
155 if [ ! $MinAge -eq 0 ]; then
156 # check both ctime and mtime
157 mtime=$(stat -c %Y $file)
158 ctime=$(stat -c %Z $file)
159 if [ $mtime -gt $ctime ]; then
160 delta=$(($now-$mtime))
161 else
162 delta=$(($now-$ctime))
163 fi
164 #echo "$file ($delta), $MinAge"
165 if [ $delta -le $MinAge ]; then
166 #echo "Skiping $file (delta=$delta)"
167 break
168 fi
169 fi
170
171 # delete oldest file
172 rm -f $file
173 done
174 fi
175}
176
177# sleep for a random interval of time (default 30min)
178# (some code taken from cron-apt, thanks)
179random_sleep()
180{
181 RandomSleep=1800
182 eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
183 if [ $RandomSleep -eq 0 ]; then
184 return
185 fi
186 if [ -z "$RANDOM" ] ; then
187 # A fix for shells that do not have this bash feature.
188 RANDOM=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -c"1-5")
189 fi
190 TIME=$(($RANDOM % $RandomSleep))
191 sleep $TIME
192}
193
194
195debug_echo()
196{
197 # Display message if $VERBOSE >= 1
198 if [ "$VERBOSE" -ge 1 ]; then
199 echo $1 1>&2
200 fi
201}
202
203# main
204
205# check apt-config exstance
206if ! which apt-config >/dev/null ; then
207 exit 0
208fi
209
210# Set VERBOSE mode from apt-config (or inherit from environment)
211eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
212if [ -z "$VERBOSE" ]; then
213 VERBOSE="0"
214fi
215if [ "$VERBOSE" -le 2 ]; then
216 # quiet for 0,1,2
217 XSTDOUT=">/dev/null"
218 XSTDERR="2>/dev/null"
219 XAPTOPT="-qq"
220 XUUPOPT=""
221else
222 XSTDOUT=""
223 XSTDERR=""
224 XAPTOPT=""
225 XUUPOPT="-d"
226fi
227if [ "$VERBOSE" -ge 3 ]; then
228 # trace output
229 set -x
230fi
231
232# laptop check, on_ac_power returns:
233# 0 (true) System is on main power
234# 1 (false) System is not on main power
235# 255 (false) Power status could not be determined
236# Desktop systems always return 255 it seems
237if which on_ac_power >/dev/null; then
238 on_ac_power
239 POWER=$?
240 if [ $POWER -eq 1 ]; then
241 debug_echo "exit: system on main power."
242 exit 0
243 elif [ $POWER -ne 0 ]; then
244 debug_echo "exit: power status ($POWER) undetermined."
245 exit 0
246 fi
247 debug_echo "system is on main power."
248fi
249
250# check if we can lock the cache and if the cache is clean
251if which apt-get >/dev/null && ! eval apt-get check $XAPTOPT $XSTDERR ; then
252 debug_echo "error encountered in cron job with \"apt-get check\"."
253 exit 0
254fi
255# No need to check for apt-get below
256
257# Global current time in seconds since 1970-01-01 00:00:00 UTC
258now=$(date +%s)
259
260# Set default values and normalize
261Dir="/"
262eval $(apt-config shell Dir Dir)
263Dir=${Dir%/}
264
265CacheDir="var/cache/apt/"
266eval $(apt-config shell CacheDir Dir::Cache)
267CacheDir=${CacheDir%/}
268if [ -z "$CacheDir" ]; then
269 debug_echo "practically empty Dir::Cache, exiting"
270 exit 0
271fi
272
273CacheArchive="archives/"
274eval $(apt-config shell CacheArchive Dir::Cache::Archives)
275CacheArchive=${CacheArchive%/}
276if [ -z "$CacheArchive" ]; then
277 debug_echo "practically empty Dir::Cache::archives, exiting"
278 exit 0
279fi
280
281BackupArchiveInterval=0
282eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
283
284BackupLevel=3
285eval $(apt-config shell BackupLevel APT::Periodic::BackupLevel)
286if [ $BackupLevel -le 1 ]; then BackupLevel=2 ; fi
287
288CacheBackup="backup/"
289eval $(apt-config shell CacheBackup Dir::Cache::Backup)
290CacheBackup=${CacheBackup%/}
291if [ -z "$CacheBackup" ]; then
292 echo "practically empty Dir::Cache::Backup, exiting" 1>&2
293 exit 0
294fi
295
296# Support old Archive for compatibility.
297# Document only Periodic for all controling parameters of this script.
298MaxAge=0
299eval $(apt-config shell MaxAge APT::Archives::MaxAge)
300eval $(apt-config shell MaxAge APT::Periodic::MaxAge)
301
302MinAge=2
303eval $(apt-config shell MinAge APT::Archives::MinAge)
304eval $(apt-config shell MinAge APT::Periodic::MinAge)
305
306MaxSize=0
307eval $(apt-config shell MaxSize APT::Archives::MaxSize)
308eval $(apt-config shell MaxSize APT::Periodic::MaxSize)
309
310UpdateInterval=0
311eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists)
312
313DownloadUpgradeableInterval=0
314eval $(apt-config shell DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
315
316UnattendedUpgradeInterval=0
317eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
318
319AutocleanInterval=0
320eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
321
322Cache="${Dir}/${CacheDir}/${CacheArchive}/"
323Back="${Dir}/${CacheDir}/${CacheBackup}/"
324BackX="${Back}${CacheArchive}/"
325for x in $(seq 0 1 $((${BackupLevel}-1))); do
326 eval "Back${x}=${Back}${x}/"
327done
328
329# check if we actually have to do anything
330if [ $UpdateInterval -eq 0 ] &&
331 [ $DownloadUpgradeableInterval -eq 0 ] &&
332 [ $UnattendedUpgradeInterval -eq 0 ] &&
333 [ $BackupArchiveInterval -eq 0 ] &&
334 [ $AutocleanInterval -eq 0 ]; then
335 exit 0
336fi
337
338
339# backup after n-days if archive contents changed.
340# (This uses hardlink to save disk space)
341BACKUP_ARCHIVE_STAMP=/var/lib/apt/periodic/backup-archive-stamp
342if check_stamp $BACKUP_ARCHIVE_STAMP $BackupArchiveInterval; then
343 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
344 mkdir -p $Back
345 rm -rf $Back$((${BackupLevel}-1))
346 for y in $(seq $((${BackupLevel}-1)) -1 1); do
347 eval BackY=${Back}$y
348 eval BackZ=${Back}$(($y-1))
349 if [ -e $BackZ ]; then mv -f $BackZ $BackY ; fi
350 done
351 cp -la $Cache $Back ; mv -f $BackX $Back0
352 update_stamp $BACKUP_ARCHIVE_STAMP
353 debug_echo "backup with hardlinks. (success)"
354 else
355
356 debug_echo "skip backup since same content."
357 fi
358else
359 debug_echo "skip backup since too new."
360fi
361
362# package archive contnts removal by package age
363if [ $MaxAge -ne 0 ] && [ $MinAge -ne 0 ]; then
364 find $Cache -name "*.deb" \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
365 debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge and ctime>$MinAge and mtime>$MinAge"
366elif [ $MaxAge -ne 0 ]; then
367 find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
368 debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge only"
369else
370 debug_echo "skip aging since MaxAge is 0"
371fi
372
373# package archive contnts removal down to $MaxSize
374if [ $MaxSize -ne 0 ]; then
375
376 MinAgeSec=$(($MinAge*24*60*60))
377
378 # reverse-sort by mtime
379 for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
380 du=$(du -m -s $Cache)
381 size=${du%%/*}
382 # check if the cache is small enough
383 if [ $size -lt $MaxSize ]; then
384 debug_echo "end remove by archive size: size=$size < $MaxSize"
385 break
386 fi
387
388 # check for MinAge in second of the file
389 if [ $MinAgeSec -ne 0 ]; then
390 # check both ctime and mtime
391 mtime=$(stat -c %Y $file)
392 ctime=$(stat -c %Z $file)
393 if [ $mtime -gt $ctime ]; then
394 delta=$(($now-$mtime))
395 else
396 delta=$(($now-$ctime))
397 fi
398 if [ $delta -le $MinAgeSec ]; then
399 debug_echo "skip remove by archive size: $file, delta=$delta < $MinAgeSec"
400 else
401 # delete oldest file
402 debug_echo "remove by archive size: $file, delta=$delta >= $MinAgeSec (sec), size=$size >= $MaxSize"
403 rm -f $file
404 fi
405 fi
406
407 done
408fi
409
410# update package lists
411UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
412if check_stamp $UPDATE_STAMP $UpdateInterval; then
413 if eval apt-get $XAPTOPT -y update $XSTDERR; then
414 debug_echo "download updated metadata (success)."
415 if which dbus-send >/dev/null && pidof dbus-daemon >/dev/null; then
416 if dbus-send --system / app.apt.dbus.updated boolean:true ; then
417 debug_echo "send dbus signal (success)"
418 else
419 debug_echo "send dbus signal (error)"
420 fi
421 else
422 debug_echo "dbus signal not send (command not available)"
423 fi
424 update_stamp $UPDATE_STAMP
425 # download all upgradeable packages if it is requested
426 DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
427 if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
428 if eval apt-get $XAPTOPT -y -d dist-upgrade $XSTDERR; then
429 update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
430 debug_echo "download upgradable (success)."
431 # auto upgrade all upgradeable packages
432 UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
433 if which unattended-upgrade >/dev/null && check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
434 if unattended-upgrade $XUUPOPT; then
435 update_stamp $UPGRADE_STAMP
436 debug_echo "unattended-upgrade (success)."
437 else
438 debug_echo "unattended-upgrade (error)."
439 fi
440 else
441 debug_echo "unattended-upgrade (not run)."
442 fi
443 else
444 debug_echo "download upgradable (error)."
445 fi
446 else
447 debug_echo "download upgradable (not run)."
448 fi
449 else
450 debug_echo "download updated metadata (error)."
451 fi
452else
453 debug_echo "download updated metadata (not run)."
454fi
455
456# autoclean package archive
457AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
458if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
459 if apt-get $XAPTOPT -y autoclean $XSTDERR; then
460 debug_echo "autoclean (success)."
461 update_stamp $AUTOCLEAN_STAMP
462 else
463 debug_echo "autoclean (error)."
464 fi
465else
466 debug_echo "autoclean (not run)."
467fi
468
469#
470# vim: set sts=4 ai :
471#
472