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