]>
Commit | Line | Data |
---|---|---|
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/10periodic file to set your preference. | |
7 | # | |
8 | # Dir "/"; | |
9 | # - RootDir for all configuration files | |
10 | # | |
11 | # Dir::Cache "var/cache/apt/"; | |
12 | # - Set apt package cache directory | |
13 | # | |
14 | # Dir::Cache::Archives "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). Useful 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 oldest 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::Download-Upgradeable-Packages-Debdelta "1"; | |
54 | # - Use debdelta-upgrade to download updates if available (0=disable) | |
55 | # | |
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 | |
61 | # | |
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 | |
71 | # | |
72 | # APT::Periodic::RandomSleep "1800"; | |
73 | # - The apt cron job will delay its execution by a random | |
74 | # time span between zero and 'APT::Periodic::RandomSleep' | |
75 | # seconds. | |
76 | # This is done because otherwise everyone would access the | |
77 | # mirror servers at the same time and put them collectively | |
78 | # under very high strain. | |
79 | # You can set this to '0' if you are using a local mirror and | |
80 | # do not care about the load spikes. | |
81 | # Note that sleeping in the apt job will be delaying the | |
82 | # execution of all subsequent cron.daily jobs. | |
83 | # | |
84 | ||
85 | check_stamp() | |
86 | { | |
87 | stamp="$1" | |
88 | interval="$2" | |
89 | ||
90 | if [ $interval -eq 0 ]; then | |
91 | debug_echo "check_stamp: interval=0" | |
92 | # treat as no time has passed | |
93 | return 1 | |
94 | fi | |
95 | ||
96 | if [ ! -f $stamp ]; then | |
97 | debug_echo "check_stamp: missing time stamp file: $stamp." | |
98 | # treat as enough time has passed | |
99 | return 0 | |
100 | fi | |
101 | ||
102 | # compare midnight today to midnight the day the stamp was updated | |
103 | stamp_file="$stamp" | |
104 | stamp=$(date --date=$(date -r $stamp_file --iso-8601) +%s 2>/dev/null) | |
105 | if [ "$?" != "0" ]; then | |
106 | # Due to some timezones returning 'invalid date' for midnight on | |
107 | # certain dates (e.g. America/Sao_Paulo), if date returns with error | |
108 | # remove the stamp file and return 0. See coreutils bug: | |
109 | # http://lists.gnu.org/archive/html/bug-coreutils/2007-09/msg00176.html | |
110 | rm -f "$stamp_file" | |
111 | return 0 | |
112 | fi | |
113 | ||
114 | now=$(date --date=$(date --iso-8601) +%s 2>/dev/null) | |
115 | if [ "$?" != "0" ]; then | |
116 | # As above, due to some timezones returning 'invalid date' for midnight | |
117 | # on certain dates (e.g. America/Sao_Paulo), if date returns with error | |
118 | # return 0. | |
119 | return 0 | |
120 | fi | |
121 | ||
122 | delta=$(($now-$stamp)) | |
123 | ||
124 | # interval is in days, convert to sec. | |
125 | interval=$(($interval*60*60*24)) | |
126 | debug_echo "check_stamp: interval=$interval, now=$now, stamp=$stamp, delta=$delta (sec)" | |
127 | ||
128 | # remove timestamps a day (or more) in the future and force re-check | |
129 | if [ $stamp -gt $(($now+86400)) ]; then | |
130 | echo "WARNING: file $stamp_file has a timestamp in the future: $stamp" | |
131 | rm -f "$stamp_file" | |
132 | return 0 | |
133 | fi | |
134 | ||
135 | if [ $delta -ge $interval ]; then | |
136 | return 0 | |
137 | fi | |
138 | ||
139 | return 1 | |
140 | } | |
141 | ||
142 | update_stamp() | |
143 | { | |
144 | stamp="$1" | |
145 | touch $stamp | |
146 | } | |
147 | ||
148 | # we check here if autoclean was enough sizewise | |
149 | check_size_constraints() | |
150 | { | |
151 | MaxAge=0 | |
152 | eval $(apt-config shell MaxAge APT::Archives::MaxAge) | |
153 | eval $(apt-config shell MaxAge APT::Periodic::MaxAge) | |
154 | ||
155 | MinAge=2 | |
156 | eval $(apt-config shell MinAge APT::Archives::MinAge) | |
157 | eval $(apt-config shell MinAge APT::Periodic::MinAge) | |
158 | ||
159 | MaxSize=0 | |
160 | eval $(apt-config shell MaxSize APT::Archives::MaxSize) | |
161 | eval $(apt-config shell MaxSize APT::Periodic::MaxSize) | |
162 | ||
163 | Cache="/var/cache/apt/archives/" | |
164 | eval $(apt-config shell Cache Dir::Cache::archives/d) | |
165 | ||
166 | # sanity check | |
167 | if [ -z "$Cache" ]; then | |
168 | echo "empty Dir::Cache::archives, exiting" | |
169 | exit | |
170 | fi | |
171 | ||
172 | # check age | |
173 | if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then | |
174 | debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge and ctime>$MinAge and mtime>$MinAge" | |
175 | find $Cache -name "*.deb" \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f | |
176 | elif [ ! $MaxAge -eq 0 ]; then | |
177 | debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge only" | |
178 | find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f | |
179 | else | |
180 | debug_echo "skip aging since MaxAge is 0" | |
181 | fi | |
182 | ||
183 | # check size | |
184 | if [ ! $MaxSize -eq 0 ]; then | |
185 | # maxSize is in MB | |
186 | MaxSize=$(($MaxSize*1024)) | |
187 | ||
188 | #get current time | |
189 | now=$(date --date=$(date --iso-8601) +%s) | |
190 | MinAge=$(($MinAge*24*60*60)) | |
191 | ||
192 | # reverse-sort by mtime | |
193 | for file in $(ls -rt $Cache/*.deb 2>/dev/null); do | |
194 | du=$(du -s $Cache) | |
195 | size=${du%%/*} | |
196 | # check if the cache is small enough | |
197 | if [ $size -lt $MaxSize ]; then | |
198 | debug_echo "end remove by archive size: size=$size < $MaxSize" | |
199 | break | |
200 | fi | |
201 | ||
202 | # check for MinAge of the file | |
203 | if [ $MinAge -ne 0 ]; then | |
204 | # check both ctime and mtime | |
205 | mtime=$(stat -c %Y $file) | |
206 | ctime=$(stat -c %Z $file) | |
207 | if [ $mtime -gt $ctime ]; then | |
208 | delta=$(($now-$mtime)) | |
209 | else | |
210 | delta=$(($now-$ctime)) | |
211 | fi | |
212 | if [ $delta -le $MinAge ]; then | |
213 | debug_echo "skip remove by archive size: $file, delta=$delta < $MinAge" | |
214 | break | |
215 | else | |
216 | # delete oldest file | |
217 | debug_echo "remove by archive size: $file, delta=$delta >= $MinAge (sec), size=$size >= $MaxSize" | |
218 | rm -f $file | |
219 | fi | |
220 | fi | |
221 | done | |
222 | fi | |
223 | } | |
224 | ||
225 | # deal with the Apt::Periodic::BackupArchiveInterval | |
226 | do_cache_backup() | |
227 | { | |
228 | BackupArchiveInterval="$1" | |
229 | if [ $BackupArchiveInterval -eq 0 ]; then | |
230 | return | |
231 | fi | |
232 | ||
233 | # Set default values and normalize | |
234 | CacheDir="/var/cache/apt" | |
235 | eval $(apt-config shell CacheDir Dir::Cache/d) | |
236 | CacheDir=${CacheDir%/} | |
237 | if [ -z "$CacheDir" ]; then | |
238 | debug_echo "practically empty Dir::Cache, exiting" | |
239 | return 0 | |
240 | fi | |
241 | ||
242 | Cache="${CacheDir}/archives/" | |
243 | eval $(apt-config shell Cache Dir::Cache::Archives/d) | |
244 | if [ -z "$Cache" ]; then | |
245 | debug_echo "practically empty Dir::Cache::archives, exiting" | |
246 | return 0 | |
247 | fi | |
248 | ||
249 | BackupLevel=3 | |
250 | eval $(apt-config shell BackupLevel APT::Periodic::BackupLevel) | |
251 | if [ $BackupLevel -le 1 ]; then | |
252 | BackupLevel=2 ; | |
253 | fi | |
254 | ||
255 | Back="${CacheDir}/backup/" | |
256 | eval $(apt-config shell Back Dir::Cache::Backup/d) | |
257 | if [ -z "$Back" ]; then | |
258 | echo "practically empty Dir::Cache::Backup, exiting" 1>&2 | |
259 | return | |
260 | fi | |
261 | ||
262 | CacheArchive="$(basename "${Cache}")" | |
263 | test -n "${CacheArchive}" || CacheArchive="archives" | |
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 | ||
294 | # sleep for a random interval of time (default 30min) | |
295 | # (some code taken from cron-apt, thanks) | |
296 | random_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 bs=2 count=1 2> /dev/null | cksum | cut -d' ' -f1) % 32767 )) | |
306 | fi | |
307 | TIME=$(($RANDOM % $RandomSleep)) | |
308 | debug_echo "sleeping for $TIME seconds" | |
309 | sleep $TIME | |
310 | } | |
311 | ||
312 | ||
313 | debug_echo() | |
314 | { | |
315 | # Display message if $VERBOSE >= 1 | |
316 | if [ "$VERBOSE" -ge 1 ]; then | |
317 | echo $1 1>&2 | |
318 | fi | |
319 | } | |
320 | ||
321 | check_power(){ | |
322 | # laptop check, on_ac_power returns: | |
323 | # 0 (true) System is on main power | |
324 | # 1 (false) System is not on main power | |
325 | # 255 (false) Power status could not be determined | |
326 | # Desktop systems always return 255 it seems | |
327 | if which on_ac_power >/dev/null; then | |
328 | on_ac_power | |
329 | POWER=$? | |
330 | if [ $POWER -eq 1 ]; then | |
331 | debug_echo "exit: system NOT on main power" | |
332 | return 1 | |
333 | elif [ $POWER -ne 0 ]; then | |
334 | debug_echo "power status ($POWER) undetermined, continuing" | |
335 | fi | |
336 | debug_echo "system is on main power." | |
337 | fi | |
338 | return 0 | |
339 | } | |
340 | ||
341 | # ------------------------ main ---------------------------- | |
342 | ||
343 | if test -r /var/lib/apt/extended_states; then | |
344 | # Backup the 7 last versions of APT's extended_states file | |
345 | # shameless copy from dpkg cron | |
346 | if cd /var/backups ; then | |
347 | if ! cmp -s apt.extended_states.0 /var/lib/apt/extended_states; then | |
348 | cp -p /var/lib/apt/extended_states apt.extended_states | |
349 | savelog -c 7 apt.extended_states >/dev/null | |
350 | fi | |
351 | fi | |
352 | fi | |
353 | ||
354 | # check apt-config existence | |
355 | if ! which apt-config >/dev/null ; then | |
356 | exit 0 | |
357 | fi | |
358 | ||
359 | # check if the user really wants to do something | |
360 | AutoAptEnable=1 # default is yes | |
361 | eval $(apt-config shell AutoAptEnable APT::Periodic::Enable) | |
362 | ||
363 | if [ $AutoAptEnable -eq 0 ]; then | |
364 | exit 0 | |
365 | fi | |
366 | ||
367 | # Set VERBOSE mode from apt-config (or inherit from environment) | |
368 | VERBOSE=0 | |
369 | eval $(apt-config shell VERBOSE APT::Periodic::Verbose) | |
370 | debug_echo "verbose level $VERBOSE" | |
371 | if [ "$VERBOSE" -le 2 ]; then | |
372 | # quiet for 0,1,2 | |
373 | XSTDOUT=">/dev/null" | |
374 | XSTDERR="2>/dev/null" | |
375 | XAPTOPT="-qq" | |
376 | XUUPOPT="" | |
377 | else | |
378 | XSTDOUT="" | |
379 | XSTDERR="" | |
380 | XAPTOPT="" | |
381 | XUUPOPT="-d" | |
382 | fi | |
383 | if [ "$VERBOSE" -ge 3 ]; then | |
384 | # trace output | |
385 | set -x | |
386 | fi | |
387 | ||
388 | check_power || exit 0 | |
389 | ||
390 | # check if we can lock the cache and if the cache is clean | |
391 | if which apt-get >/dev/null && ! eval apt-get check $XAPTOPT $XSTDERR ; then | |
392 | debug_echo "error encountered in cron job with \"apt-get check\"." | |
393 | exit 0 | |
394 | fi | |
395 | ||
396 | # Global current time in seconds since 1970-01-01 00:00:00 UTC | |
397 | now=$(date +%s) | |
398 | ||
399 | # Support old Archive for compatibility. | |
400 | # Document only Periodic for all controlling parameters of this script. | |
401 | ||
402 | UpdateInterval=0 | |
403 | eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists) | |
404 | ||
405 | DownloadUpgradeableInterval=0 | |
406 | eval $(apt-config shell DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages) | |
407 | ||
408 | UnattendedUpgradeInterval=0 | |
409 | eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade) | |
410 | ||
411 | AutocleanInterval=0 | |
412 | eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval) | |
413 | ||
414 | BackupArchiveInterval=0 | |
415 | eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval) | |
416 | ||
417 | Debdelta=1 | |
418 | eval $(apt-config shell Debdelta APT::Periodic::Download-Upgradeable-Packages-Debdelta) | |
419 | ||
420 | # check if we actually have to do anything that requires locking the cache | |
421 | if [ $UpdateInterval -eq 0 ] && | |
422 | [ $DownloadUpgradeableInterval -eq 0 ] && | |
423 | [ $UnattendedUpgradeInterval -eq 0 ] && | |
424 | [ $BackupArchiveInterval -eq 0 ] && | |
425 | [ $AutocleanInterval -eq 0 ]; then | |
426 | ||
427 | # check cache size | |
428 | check_size_constraints | |
429 | ||
430 | exit 0 | |
431 | fi | |
432 | ||
433 | # deal with BackupArchiveInterval | |
434 | do_cache_backup $BackupArchiveInterval | |
435 | ||
436 | # sleep random amount of time to avoid hitting the | |
437 | # mirrors at the same time | |
438 | random_sleep | |
439 | check_power || exit 0 | |
440 | ||
441 | # include default system language so that "apt-get update" will | |
442 | # fetch the right translated package descriptions | |
443 | if [ -r /etc/default/locale ]; then | |
444 | . /etc/default/locale | |
445 | export LANG LANGUAGE LC_MESSAGES LC_ALL | |
446 | fi | |
447 | ||
448 | # update package lists | |
449 | UPDATED=0 | |
450 | UPDATE_STAMP=/var/lib/apt/periodic/update-stamp | |
451 | if check_stamp $UPDATE_STAMP $UpdateInterval; then | |
452 | if eval apt-get $XAPTOPT -y update $XSTDERR; then | |
453 | debug_echo "download updated metadata (success)." | |
454 | if which dbus-send >/dev/null && pidof dbus-daemon >/dev/null; then | |
455 | if dbus-send --system / app.apt.dbus.updated boolean:true ; then | |
456 | debug_echo "send dbus signal (success)" | |
457 | else | |
458 | debug_echo "send dbus signal (error)" | |
459 | fi | |
460 | else | |
461 | debug_echo "dbus signal not send (command not available)" | |
462 | fi | |
463 | update_stamp $UPDATE_STAMP | |
464 | UPDATED=1 | |
465 | else | |
466 | debug_echo "download updated metadata (error)" | |
467 | fi | |
468 | else | |
469 | debug_echo "download updated metadata (not run)." | |
470 | fi | |
471 | ||
472 | # download all upgradeable packages (if it is requested) | |
473 | DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp | |
474 | if [ $UPDATED -eq 1 ] && check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then | |
475 | if [ $Debdelta -eq 1 ]; then | |
476 | debdelta-upgrade >/dev/null 2>&1 || true | |
477 | fi | |
478 | if eval apt-get $XAPTOPT -y -d dist-upgrade $XSTDERR; then | |
479 | update_stamp $DOWNLOAD_UPGRADEABLE_STAMP | |
480 | debug_echo "download upgradable (success)" | |
481 | else | |
482 | debug_echo "download upgradable (error)" | |
483 | fi | |
484 | else | |
485 | debug_echo "download upgradable (not run)" | |
486 | fi | |
487 | ||
488 | # auto upgrade all upgradeable packages | |
489 | UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp | |
490 | if which unattended-upgrade >/dev/null && check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then | |
491 | if unattended-upgrade $XUUPOPT; then | |
492 | update_stamp $UPGRADE_STAMP | |
493 | debug_echo "unattended-upgrade (success)" | |
494 | else | |
495 | debug_echo "unattended-upgrade (error)" | |
496 | fi | |
497 | else | |
498 | debug_echo "unattended-upgrade (not run)" | |
499 | fi | |
500 | ||
501 | # autoclean package archive | |
502 | AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp | |
503 | if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then | |
504 | if eval apt-get $XAPTOPT -y autoclean $XSTDERR; then | |
505 | debug_echo "autoclean (success)." | |
506 | update_stamp $AUTOCLEAN_STAMP | |
507 | else | |
508 | debug_echo "autoclean (error)" | |
509 | fi | |
510 | else | |
511 | debug_echo "autoclean (not run)" | |
512 | fi | |
513 | ||
514 | # check cache size | |
515 | check_size_constraints | |
516 | ||
517 | # | |
518 | # vim: set sts=4 ai : | |
519 | # | |
520 |