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