debian/apt.cron.daily: Make sure that VERBOSE is always set
[ntk/apt.git] / debian / apt.cron.daily
CommitLineData
0c132682 1#!/bin/sh
0c132682 2#set -e
8a139d4d
MV
3#
4# This file understands the following apt configuration variables:
ff7c76f8
OA
5# Values here are the default.
6# Create /etc/apt/apt.conf.d/02periodic file to set your preference.
8a139d4d 7#
ff7c76f8
OA
8# Dir "/";
9# - RootDir for all configuration files
8a139d4d 10#
ff7c76f8
OA
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)
8a139d4d
MV
28# - Set maximum allowed age of a cache package file. If a cache
29# package file is older it is deleted (0=disable)
30#
ff7c76f8
OA
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)
8a139d4d
MV
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#
ff7c76f8
OA
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)
8a139d4d 49#
ff7c76f8
OA
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
8a139d4d 55#
ff7c76f8
OA
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
0c132682 65
05f6a46a 66check_stamp()
0c132682 67{
05f6a46a
MZ
68 stamp="$1"
69 interval="$2"
70
10946ddc 71 if [ $interval -eq 0 ]; then
53391d0f 72 debug_echo "check_stamp: interval=0"
ff7c76f8 73 # treat as no time has passed
10946ddc
MZ
74 return 1
75 fi
76
05f6a46a 77 if [ ! -f $stamp ]; then
ff7c76f8
OA
78 debug_echo "check_stamp: missing time stamp file: $stamp."
79 # treat as enough time has passed
05f6a46a 80 return 0
0c132682 81 fi
05f6a46a
MZ
82
83 # compare midnight today to midnight the day the stamp was updated
a7c526b6
MV
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
05f6a46a 103 delta=$(($now-$stamp))
05f6a46a 104
ff7c76f8 105 # intervall is in days, convert to sec.
8a139d4d 106 interval=$(($interval*60*60*24))
ff7c76f8 107 debug_echo "check_stamp: interval=$interval, now=$now, stamp=$stamp, delta=$delta (sec)"
8a139d4d 108
6e7c6c3f
MV
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
05f6a46a
MZ
116 if [ $delta -ge $interval ]; then
117 return 0
118 fi
119
120 return 1
121}
122
123update_stamp()
124{
125 stamp="$1"
05f6a46a 126 touch $stamp
0c132682
MZ
127}
128
6cce801a 129# we check here if autoclean was enough sizewise
2e8a92e5 130check_size_constraints()
6cce801a 131{
6cce801a 132 MaxAge=0
6cce801a 133 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
c98870b0
MV
134 eval $(apt-config shell MaxAge APT::Periodic::MaxAge)
135
136 MinAge=2
8a139d4d 137 eval $(apt-config shell MinAge APT::Archives::MinAge)
c98870b0
MV
138 eval $(apt-config shell MinAge APT::Periodic::MinAge)
139
140 MaxSize=0
6cce801a 141 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
c98870b0
MV
142 eval $(apt-config shell MaxSize APT::Periodic::MaxSize)
143
144 CacheDir="var/cache/apt/"
6cce801a 145 eval $(apt-config shell CacheDir Dir::Cache)
c98870b0
MV
146 CacheDir=${CacheDir%/}
147
148 CacheArchive="archives/"
6cce801a 149 eval $(apt-config shell CacheArchive Dir::Cache::archives)
c98870b0 150 CacheArchive=${CacheArchive%/}
6cce801a
MV
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
01717245
MV
157
158 Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
6cce801a
MV
159
160 # check age
8a139d4d 161 if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
c98870b0 162 debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge and ctime>$MinAge and mtime>$MinAge"
8e29e348 163 find $Cache -name "*.deb" \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
8a139d4d 164 elif [ ! $MaxAge -eq 0 ]; then
c98870b0 165 debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge only"
8e29e348 166 find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
c98870b0
MV
167 else
168 debug_echo "skip aging since MaxAge is 0"
6cce801a
MV
169 fi
170
171 # check size
172 if [ ! $MaxSize -eq 0 ]; then
8a139d4d
MV
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
6cce801a 180 # reverse-sort by mtime
3408b58c 181 for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
6cce801a
MV
182 du=$(du -s $Cache)
183 size=${du%%/*}
184 # check if the cache is small enough
185 if [ $size -lt $MaxSize ]; then
c98870b0 186 debug_echo "end remove by archive size: size=$size < $MaxSize"
6cce801a
MV
187 break
188 fi
8a139d4d
MV
189
190 # check for MinAge of the file
c98870b0 191 if [ $MinAge -ne 0 ]; then
8e29e348 192 # check both ctime and mtime
3971f8e8 193 mtime=$(stat -c %Y $file)
8e29e348
MV
194 ctime=$(stat -c %Z $file)
195 if [ $mtime -gt $ctime ]; then
196 delta=$(($now-$mtime))
197 else
198 delta=$(($now-$ctime))
199 fi
8a139d4d 200 if [ $delta -le $MinAge ]; then
c98870b0 201 debug_echo "skip remove by archive size: $file, delta=$delta < $MinAgeSec"
3408b58c 202 break
c98870b0
MV
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
8a139d4d
MV
207 fi
208 fi
6cce801a
MV
209 done
210 fi
211}
212
c98870b0
MV
213# deal with the Apt::Periodic::BackupArchiveInterval
214do_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
d047c6da 288# sleep for a random interval of time (default 30min)
69c28efc
MV
289# (some code taken from cron-apt, thanks)
290random_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))
c98870b0 302 debug_echo "sleeping for $TIME seconds"
69c28efc
MV
303 sleep $TIME
304}
305
69c28efc 306
ff7c76f8 307debug_echo()
6cce801a 308{
ff7c76f8
OA
309 # Display message if $VERBOSE >= 1
310 if [ "$VERBOSE" -ge 1 ]; then
311 echo $1 1>&2
6cce801a 312 fi
ff7c76f8
OA
313}
314
c98870b0 315# ------------------------ main ----------------------------
69c28efc 316
ff7c76f8
OA
317# check apt-config exstance
318if ! which apt-config >/dev/null ; then
18d38975
OS
319 exit 0
320fi
87ddfb96 321
ff7c76f8 322# Set VERBOSE mode from apt-config (or inherit from environment)
6985efb3 323VERBOSE=0
ff7c76f8 324eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
c98870b0 325debug_echo "verbose level $VERBOSE"
ff7c76f8
OA
326if [ "$VERBOSE" -le 2 ]; then
327 # quiet for 0,1,2
328 XSTDOUT=">/dev/null"
329 XSTDERR="2>/dev/null"
330 XAPTOPT="-qq"
331 XUUPOPT=""
332else
333 XSTDOUT=""
334 XSTDERR=""
335 XAPTOPT=""
336 XUUPOPT="-d"
337fi
338if [ "$VERBOSE" -ge 3 ]; then
339 # trace output
340 set -x
341fi
8a139d4d 342
ff7c76f8
OA
343# laptop check, on_ac_power returns:
344# 0 (true) System is on main power
345# 1 (false) System is not on main power
346# 255 (false) Power status could not be determined
347# Desktop systems always return 255 it seems
348if which on_ac_power >/dev/null; then
349 on_ac_power
350 POWER=$?
351 if [ $POWER -eq 1 ]; then
c98870b0 352 debug_echo "exit: system NOT on main power"
ff7c76f8
OA
353 exit 0
354 elif [ $POWER -ne 0 ]; then
c98870b0 355 debug_echo "power status ($POWER) undetermined, continuing"
6cce801a 356 fi
ff7c76f8
OA
357 debug_echo "system is on main power."
358fi
8a139d4d 359
ff7c76f8 360# check if we can lock the cache and if the cache is clean
aff278bf 361if which apt-get >/dev/null && ! eval apt-get check -f $XAPTOPT $XSTDERR ; then
ff7c76f8
OA
362 debug_echo "error encountered in cron job with \"apt-get check\"."
363 exit 0
364fi
8a139d4d 365
ff7c76f8
OA
366# Global current time in seconds since 1970-01-01 00:00:00 UTC
367now=$(date +%s)
6cce801a 368
ff7c76f8
OA
369# Support old Archive for compatibility.
370# Document only Periodic for all controling parameters of this script.
ff7c76f8 371
0c132682 372UpdateInterval=0
ff7c76f8
OA
373eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists)
374
05f6a46a 375DownloadUpgradeableInterval=0
ff7c76f8 376eval $(apt-config shell DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
0c132682 377
fdd15654
MV
378UnattendedUpgradeInterval=0
379eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
380
ff7c76f8
OA
381AutocleanInterval=0
382eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
383
c98870b0
MV
384BackupArchiveInterval=0
385eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
ff7c76f8 386
4c2dcaa1
MV
387# check if we actually have to do anything
388if [ $UpdateInterval -eq 0 ] &&
389 [ $DownloadUpgradeableInterval -eq 0 ] &&
390 [ $UnattendedUpgradeInterval -eq 0 ] &&
2783b261 391 [ $BackupArchiveInterval -eq 0 ] &&
4c2dcaa1
MV
392 [ $AutocleanInterval -eq 0 ]; then
393 exit 0
394fi
fdd15654 395
c98870b0
MV
396# deal with BackupArchiveInterval
397do_cache_backup $BackupArchiveInterval
0c132682 398
4c2dcaa1
MV
399# sleep random amount of time to avoid hitting the
400# mirrors at the same time
69c28efc
MV
401random_sleep
402
ff7c76f8 403# update package lists
aff278bf 404UPDATED=0
05f6a46a
MZ
405UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
406if check_stamp $UPDATE_STAMP $UpdateInterval; then
ff7c76f8
OA
407 if eval apt-get $XAPTOPT -y update $XSTDERR; then
408 debug_echo "download updated metadata (success)."
be993931 409 if which dbus-send >/dev/null && pidof dbus-daemon >/dev/null; then
ff7c76f8
OA
410 if dbus-send --system / app.apt.dbus.updated boolean:true ; then
411 debug_echo "send dbus signal (success)"
412 else
413 debug_echo "send dbus signal (error)"
414 fi
415 else
416 debug_echo "dbus signal not send (command not available)"
05f6a46a 417 fi
ff7c76f8 418 update_stamp $UPDATE_STAMP
aff278bf 419 UPDATED=1
ff7c76f8 420 else
c98870b0 421 debug_echo "download updated metadata (error)"
05f6a46a 422 fi
ff7c76f8
OA
423else
424 debug_echo "download updated metadata (not run)."
fdd15654 425fi
c98870b0
MV
426
427# download all upgradeable packages (if it is requested)
428DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
aff278bf 429if [ $UPDATED -eq 1 ] && check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
c98870b0
MV
430 if eval apt-get $XAPTOPT -y -d dist-upgrade $XSTDERR; then
431 update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
432 debug_echo "download upgradable (success)"
433 else
434 debug_echo "download upgradable (error)"
435 fi
436else
437 debug_echo "download upgradable (not run)"
438fi
439
440# auto upgrade all upgradeable packages
441UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
aff278bf 442if [ $UPDATED -eq 1 ] && which unattended-upgrade >/dev/null && check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
c98870b0
MV
443 if unattended-upgrade $XUUPOPT; then
444 update_stamp $UPGRADE_STAMP
445 debug_echo "unattended-upgrade (success)"
446 else
447 debug_echo "unattended-upgrade (error)"
448 fi
449else
450 debug_echo "unattended-upgrade (not run)"
451fi
fdd15654 452
ff7c76f8 453# autoclean package archive
de15fbae
MV
454AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
455if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
c98870b0 456 if eval apt-get $XAPTOPT -y autoclean $XSTDERR; then
ff7c76f8
OA
457 debug_echo "autoclean (success)."
458 update_stamp $AUTOCLEAN_STAMP
459 else
c98870b0 460 debug_echo "autoclean (error)"
ff7c76f8
OA
461 fi
462else
c98870b0 463 debug_echo "autoclean (not run)"
de15fbae
MV
464fi
465
0fad7309
MV
466# check cache size
467check_size_constraints
468
ff7c76f8
OA
469#
470# vim: set sts=4 ai :
471#
472