]> git.saurik.com Git - apt.git/blame - debian/apt.systemd.daily
apt.systemd.daily: Put opening brace of check_power on extra line
[apt.git] / debian / apt.systemd.daily
CommitLineData
0c132682 1#!/bin/sh
0c132682 2#set -e
8a139d4d
MV
3#
4# This file understands the following apt configuration variables:
ff7c76f8 5# Values here are the default.
f9be02d7 6# Create /etc/apt/apt.conf.d/10periodic file to set your preference.
8a139d4d 7#
ff7c76f8
OA
8# Dir "/";
9# - RootDir for all configuration files
8a139d4d 10#
0d0bdb60 11# Dir::Cache "var/cache/apt/";
ff7c76f8
OA
12# - Set apt package cache directory
13#
c1cde32e 14# Dir::Cache::Archives "archives/";
ff7c76f8
OA
15# - Set package archive directory
16#
17443d48
JAK
17# APT::Periodic::Enable "1";
18# - Enable the update/upgrade script (0=disable)
19#
ff7c76f8
OA
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)
8a139d4d
MV
31# - Set maximum allowed age of a cache package file. If a cache
32# package file is older it is deleted (0=disable)
33#
ff7c76f8
OA
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
1e3f4083 37# will not be deleted (0=disable). Useful to prevent races
ff7c76f8
OA
38# and to keep backups of the packages for emergency.
39#
40# APT::Archives::MaxSize "0"; (old, deprecated)
41# APT::Periodic::MaxSize "0"; (new)
8a139d4d
MV
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
12c4e7c9 44# requirement is met (the oldest packages will be deleted
8a139d4d
MV
45# first).
46#
ff7c76f8
OA
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)
6b519e42
JAK
52#
53# APT::Periodic::Download-Upgradeable-Packages-Debdelta "1";
54# - Use debdelta-upgrade to download updates if available (0=disable)
55#
ff7c76f8
OA
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
8a139d4d 61#
ff7c76f8
OA
62# APT::Periodic::AutocleanInterval "0";
63# - Do "apt-get autoclean" every n-days (0=disable)
64#
73d52d65
MV
65# APT::Periodic::CleanInterval "0";
66# - Do "apt-get clean" every n-days (0=disable)
67#
ff7c76f8
OA
68# APT::Periodic::Verbose "0";
69# - Send report mail to root
70# 0: no report (or null string)
71# 1: progress report (actually any string)
72# 2: + command outputs (remove -qq, remove 2>/dev/null, add -d)
73# 3: + trace on
1d6afdd9 74#
0c132682 75
05f6a46a 76check_stamp()
0c132682 77{
05f6a46a
MZ
78 stamp="$1"
79 interval="$2"
80
10946ddc 81 if [ $interval -eq 0 ]; then
53391d0f 82 debug_echo "check_stamp: interval=0"
ff7c76f8 83 # treat as no time has passed
10946ddc
MZ
84 return 1
85 fi
86
05f6a46a 87 if [ ! -f $stamp ]; then
ff7c76f8
OA
88 debug_echo "check_stamp: missing time stamp file: $stamp."
89 # treat as enough time has passed
05f6a46a 90 return 0
0c132682 91 fi
05f6a46a
MZ
92
93 # compare midnight today to midnight the day the stamp was updated
a7c526b6
MV
94 stamp_file="$stamp"
95 stamp=$(date --date=$(date -r $stamp_file --iso-8601) +%s 2>/dev/null)
96 if [ "$?" != "0" ]; then
97 # Due to some timezones returning 'invalid date' for midnight on
e9db7e74 98 # certain dates (e.g. America/Sao_Paulo), if date returns with error
a7c526b6
MV
99 # remove the stamp file and return 0. See coreutils bug:
100 # http://lists.gnu.org/archive/html/bug-coreutils/2007-09/msg00176.html
101 rm -f "$stamp_file"
102 return 0
103 fi
104
105 now=$(date --date=$(date --iso-8601) +%s 2>/dev/null)
106 if [ "$?" != "0" ]; then
107 # As above, due to some timezones returning 'invalid date' for midnight
e9db7e74 108 # on certain dates (e.g. America/Sao_Paulo), if date returns with error
a7c526b6
MV
109 # return 0.
110 return 0
111 fi
112
05f6a46a 113 delta=$(($now-$stamp))
05f6a46a 114
e9db7e74 115 # interval is in days, convert to sec.
8a139d4d 116 interval=$(($interval*60*60*24))
ff7c76f8 117 debug_echo "check_stamp: interval=$interval, now=$now, stamp=$stamp, delta=$delta (sec)"
8a139d4d 118
6e7c6c3f
MV
119 # remove timestamps a day (or more) in the future and force re-check
120 if [ $stamp -gt $(($now+86400)) ]; then
121 echo "WARNING: file $stamp_file has a timestamp in the future: $stamp"
122 rm -f "$stamp_file"
123 return 0
124 fi
125
05f6a46a
MZ
126 if [ $delta -ge $interval ]; then
127 return 0
128 fi
129
130 return 1
131}
132
133update_stamp()
134{
135 stamp="$1"
05f6a46a 136 touch $stamp
0c132682
MZ
137}
138
6cce801a 139# we check here if autoclean was enough sizewise
2e8a92e5 140check_size_constraints()
6cce801a 141{
6cce801a 142 MaxAge=0
6cce801a 143 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
c98870b0
MV
144 eval $(apt-config shell MaxAge APT::Periodic::MaxAge)
145
146 MinAge=2
8a139d4d 147 eval $(apt-config shell MinAge APT::Archives::MinAge)
c98870b0
MV
148 eval $(apt-config shell MinAge APT::Periodic::MinAge)
149
150 MaxSize=0
6cce801a 151 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
c98870b0
MV
152 eval $(apt-config shell MaxSize APT::Periodic::MaxSize)
153
c1cde32e
DK
154 Cache="/var/cache/apt/archives/"
155 eval $(apt-config shell Cache Dir::Cache::archives/d)
6cce801a
MV
156
157 # sanity check
c1cde32e
DK
158 if [ -z "$Cache" ]; then
159 echo "empty Dir::Cache::archives, exiting"
6cce801a
MV
160 exit
161 fi
6cce801a
MV
162
163 # check age
8a139d4d 164 if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
c98870b0 165 debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge and ctime>$MinAge and mtime>$MinAge"
8e29e348 166 find $Cache -name "*.deb" \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
8a139d4d 167 elif [ ! $MaxAge -eq 0 ]; then
c98870b0 168 debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge only"
8e29e348 169 find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
c98870b0
MV
170 else
171 debug_echo "skip aging since MaxAge is 0"
6cce801a
MV
172 fi
173
174 # check size
175 if [ ! $MaxSize -eq 0 ]; then
8a139d4d
MV
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
6cce801a 183 # reverse-sort by mtime
3408b58c 184 for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
6cce801a
MV
185 du=$(du -s $Cache)
186 size=${du%%/*}
187 # check if the cache is small enough
188 if [ $size -lt $MaxSize ]; then
c98870b0 189 debug_echo "end remove by archive size: size=$size < $MaxSize"
6cce801a
MV
190 break
191 fi
8a139d4d
MV
192
193 # check for MinAge of the file
c98870b0 194 if [ $MinAge -ne 0 ]; then
8e29e348 195 # check both ctime and mtime
3971f8e8 196 mtime=$(stat -c %Y $file)
8e29e348
MV
197 ctime=$(stat -c %Z $file)
198 if [ $mtime -gt $ctime ]; then
199 delta=$(($now-$mtime))
200 else
201 delta=$(($now-$ctime))
202 fi
8a139d4d 203 if [ $delta -le $MinAge ]; then
e838ca08 204 debug_echo "skip remove by archive size: $file, delta=$delta < $MinAge"
3408b58c 205 break
c98870b0
MV
206 else
207 # delete oldest file
e838ca08 208 debug_echo "remove by archive size: $file, delta=$delta >= $MinAge (sec), size=$size >= $MaxSize"
c98870b0 209 rm -f $file
8a139d4d
MV
210 fi
211 fi
6cce801a
MV
212 done
213 fi
214}
215
c98870b0
MV
216# deal with the Apt::Periodic::BackupArchiveInterval
217do_cache_backup()
218{
219 BackupArchiveInterval="$1"
220 if [ $BackupArchiveInterval -eq 0 ]; then
221 return
222 fi
223
224 # Set default values and normalize
c1cde32e
DK
225 CacheDir="/var/cache/apt"
226 eval $(apt-config shell CacheDir Dir::Cache/d)
c98870b0
MV
227 CacheDir=${CacheDir%/}
228 if [ -z "$CacheDir" ]; then
229 debug_echo "practically empty Dir::Cache, exiting"
230 return 0
231 fi
232
c1cde32e
DK
233 Cache="${CacheDir}/archives/"
234 eval $(apt-config shell Cache Dir::Cache::Archives/d)
235 if [ -z "$Cache" ]; then
c98870b0
MV
236 debug_echo "practically empty Dir::Cache::archives, exiting"
237 return 0
238 fi
239
240 BackupLevel=3
241 eval $(apt-config shell BackupLevel APT::Periodic::BackupLevel)
242 if [ $BackupLevel -le 1 ]; then
243 BackupLevel=2 ;
244 fi
245
c1cde32e
DK
246 Back="${CacheDir}/backup/"
247 eval $(apt-config shell Back Dir::Cache::Backup/d)
248 if [ -z "$Back" ]; then
c98870b0
MV
249 echo "practically empty Dir::Cache::Backup, exiting" 1>&2
250 return
251 fi
252
c1cde32e
DK
253 CacheArchive="$(basename "${Cache}")"
254 test -n "${CacheArchive}" || CacheArchive="archives"
c98870b0
MV
255 BackX="${Back}${CacheArchive}/"
256 for x in $(seq 0 1 $((${BackupLevel}-1))); do
257 eval "Back${x}=${Back}${x}/"
258 done
259
260 # backup after n-days if archive contents changed.
261 # (This uses hardlink to save disk space)
262 BACKUP_ARCHIVE_STAMP=/var/lib/apt/periodic/backup-archive-stamp
263 if check_stamp $BACKUP_ARCHIVE_STAMP $BackupArchiveInterval; then
264 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
265 mkdir -p $Back
266 rm -rf $Back$((${BackupLevel}-1))
267 for y in $(seq $((${BackupLevel}-1)) -1 1); do
268 eval BackY=${Back}$y
269 eval BackZ=${Back}$(($y-1))
270 if [ -e $BackZ ]; then
271 mv -f $BackZ $BackY ;
272 fi
273 done
274 cp -la $Cache $Back ; mv -f $BackX $Back0
275 update_stamp $BACKUP_ARCHIVE_STAMP
276 debug_echo "backup with hardlinks. (success)"
277 else
278 debug_echo "skip backup since same content."
279 fi
280 else
281 debug_echo "skip backup since too new."
282 fi
283}
284
ff7c76f8 285debug_echo()
6cce801a 286{
ff7c76f8
OA
287 # Display message if $VERBOSE >= 1
288 if [ "$VERBOSE" -ge 1 ]; then
289 echo $1 1>&2
6cce801a 290 fi
ff7c76f8
OA
291}
292
ea4c6b3e
JAK
293check_power()
294{
e20d3bcf
JAK
295 # laptop check, on_ac_power returns:
296 # 0 (true) System is on main power
297 # 1 (false) System is not on main power
298 # 255 (false) Power status could not be determined
299 # Desktop systems always return 255 it seems
8deda84e 300 if which on_ac_power >/dev/null 2>&1; then
e20d3bcf
JAK
301 on_ac_power
302 POWER=$?
303 if [ $POWER -eq 1 ]; then
304 debug_echo "exit: system NOT on main power"
305 return 1
306 elif [ $POWER -ne 0 ]; then
307 debug_echo "power status ($POWER) undetermined, continuing"
308 fi
309 debug_echo "system is on main power."
310 fi
311 return 0
312}
313
c98870b0 314# ------------------------ main ----------------------------
69c28efc 315
a3b76cde
DK
316if test -r /var/lib/apt/extended_states; then
317 # Backup the 7 last versions of APT's extended_states file
318 # shameless copy from dpkg cron
319 if cd /var/backups ; then
320 if ! cmp -s apt.extended_states.0 /var/lib/apt/extended_states; then
321 cp -p /var/lib/apt/extended_states apt.extended_states
322 savelog -c 7 apt.extended_states >/dev/null
323 fi
a23a7811
DK
324 fi
325fi
326
e9db7e74 327# check apt-config existence
8deda84e 328if ! which apt-config >/dev/null 2>&1; then
18d38975
OS
329 exit 0
330fi
87ddfb96 331
17443d48
JAK
332# check if the user really wants to do something
333AutoAptEnable=1 # default is yes
334eval $(apt-config shell AutoAptEnable APT::Periodic::Enable)
335
9a64707c 336if [ $AutoAptEnable -eq 0 ]; then
17443d48
JAK
337 exit 0
338fi
339
ff7c76f8 340# Set VERBOSE mode from apt-config (or inherit from environment)
6985efb3 341VERBOSE=0
ff7c76f8 342eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
c98870b0 343debug_echo "verbose level $VERBOSE"
ff7c76f8
OA
344if [ "$VERBOSE" -le 2 ]; then
345 # quiet for 0,1,2
346 XSTDOUT=">/dev/null"
347 XSTDERR="2>/dev/null"
348 XAPTOPT="-qq"
349 XUUPOPT=""
350else
351 XSTDOUT=""
352 XSTDERR=""
353 XAPTOPT=""
354 XUUPOPT="-d"
355fi
356if [ "$VERBOSE" -ge 3 ]; then
357 # trace output
358 set -x
359fi
8a139d4d 360
e20d3bcf 361check_power || exit 0
8a139d4d 362
ff7c76f8 363# check if we can lock the cache and if the cache is clean
8deda84e 364if which apt-get >/dev/null 2>&1 && ! eval apt-get check $XAPTOPT $XSTDERR ; then
ff7c76f8
OA
365 debug_echo "error encountered in cron job with \"apt-get check\"."
366 exit 0
367fi
8a139d4d 368
ff7c76f8
OA
369# Global current time in seconds since 1970-01-01 00:00:00 UTC
370now=$(date +%s)
6cce801a 371
ff7c76f8 372# Support old Archive for compatibility.
1e3f4083 373# Document only Periodic for all controlling parameters of this script.
ff7c76f8 374
0c132682 375UpdateInterval=0
ff7c76f8
OA
376eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists)
377
05f6a46a 378DownloadUpgradeableInterval=0
ff7c76f8 379eval $(apt-config shell DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
0c132682 380
fdd15654
MV
381UnattendedUpgradeInterval=0
382eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
383
ff7c76f8
OA
384AutocleanInterval=0
385eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
386
73d52d65
MV
387CleanInterval=0
388eval $(apt-config shell CleanInterval APT::Periodic::CleanInterval)
389
c98870b0
MV
390BackupArchiveInterval=0
391eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
ff7c76f8 392
6b519e42
JAK
393Debdelta=1
394eval $(apt-config shell Debdelta APT::Periodic::Download-Upgradeable-Packages-Debdelta)
395
879cbcc8 396# check if we actually have to do anything that requires locking the cache
4c2dcaa1
MV
397if [ $UpdateInterval -eq 0 ] &&
398 [ $DownloadUpgradeableInterval -eq 0 ] &&
399 [ $UnattendedUpgradeInterval -eq 0 ] &&
2783b261 400 [ $BackupArchiveInterval -eq 0 ] &&
73d52d65
MV
401 [ $AutocleanInterval -eq 0 ] &&
402 [ $CleanInterval -eq 0 ]; then
879cbcc8
DK
403
404 # check cache size
405 check_size_constraints
406
4c2dcaa1
MV
407 exit 0
408fi
fdd15654 409
c98870b0
MV
410# deal with BackupArchiveInterval
411do_cache_backup $BackupArchiveInterval
0c132682 412
14669d4b 413# ensure we don't do this on battery
e20d3bcf 414check_power || exit 0
69c28efc 415
966a4c53
MV
416# include default system language so that "apt-get update" will
417# fetch the right translated package descriptions
418if [ -r /etc/default/locale ]; then
419 . /etc/default/locale
6e239830 420 export LANG LANGUAGE LC_MESSAGES LC_ALL
966a4c53
MV
421fi
422
ff7c76f8 423# update package lists
aff278bf 424UPDATED=0
05f6a46a
MZ
425UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
426if check_stamp $UPDATE_STAMP $UpdateInterval; then
ff7c76f8
OA
427 if eval apt-get $XAPTOPT -y update $XSTDERR; then
428 debug_echo "download updated metadata (success)."
8deda84e 429 if which dbus-send >/dev/null 2>&1 && pidof dbus-daemon >/dev/null 2>&1; then
ff7c76f8
OA
430 if dbus-send --system / app.apt.dbus.updated boolean:true ; then
431 debug_echo "send dbus signal (success)"
432 else
433 debug_echo "send dbus signal (error)"
434 fi
435 else
436 debug_echo "dbus signal not send (command not available)"
05f6a46a 437 fi
ff7c76f8 438 update_stamp $UPDATE_STAMP
aff278bf 439 UPDATED=1
ff7c76f8 440 else
c98870b0 441 debug_echo "download updated metadata (error)"
05f6a46a 442 fi
ff7c76f8
OA
443else
444 debug_echo "download updated metadata (not run)."
fdd15654 445fi
c98870b0
MV
446
447# download all upgradeable packages (if it is requested)
448DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
aff278bf 449if [ $UPDATED -eq 1 ] && check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
6b519e42
JAK
450 if [ $Debdelta -eq 1 ]; then
451 debdelta-upgrade >/dev/null 2>&1 || true
452 fi
c98870b0
MV
453 if eval apt-get $XAPTOPT -y -d dist-upgrade $XSTDERR; then
454 update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
455 debug_echo "download upgradable (success)"
456 else
457 debug_echo "download upgradable (error)"
458 fi
459else
460 debug_echo "download upgradable (not run)"
461fi
462
463# auto upgrade all upgradeable packages
464UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
8deda84e 465if which unattended-upgrade >/dev/null 2>&1 && check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
c98870b0
MV
466 if unattended-upgrade $XUUPOPT; then
467 update_stamp $UPGRADE_STAMP
468 debug_echo "unattended-upgrade (success)"
469 else
470 debug_echo "unattended-upgrade (error)"
471 fi
472else
473 debug_echo "unattended-upgrade (not run)"
474fi
fdd15654 475
73d52d65
MV
476# clean package archive
477CLEAN_STAMP=/var/lib/apt/periodic/clean-stamp
478if check_stamp $CLEAN_STAMP $CleanInterval; then
479 if eval apt-get $XAPTOPT -y clean $XSTDERR; then
480 debug_echo "clean (success)."
481 update_stamp $CLEAN_STAMP
482 else
483 debug_echo "clean (error)"
484 fi
485else
486 debug_echo "clean (not run)"
487fi
488
ff7c76f8 489# autoclean package archive
de15fbae
MV
490AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
491if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
c98870b0 492 if eval apt-get $XAPTOPT -y autoclean $XSTDERR; then
ff7c76f8
OA
493 debug_echo "autoclean (success)."
494 update_stamp $AUTOCLEAN_STAMP
495 else
c98870b0 496 debug_echo "autoclean (error)"
ff7c76f8
OA
497 fi
498else
c98870b0 499 debug_echo "autoclean (not run)"
de15fbae
MV
500fi
501
0fad7309
MV
502# check cache size
503check_size_constraints
504
ff7c76f8
OA
505#
506# vim: set sts=4 ai :
507#
508