debian/apt.cron.daily: Script can be disabled by APT::Periodic::Enable=0 (Closes...
[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#
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
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)
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
44# requirement is met (the biggest packages will be deleted
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)
8a139d4d 52#
ff7c76f8
OA
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
8a139d4d 58#
ff7c76f8
OA
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
0c132682 68
05f6a46a 69check_stamp()
0c132682 70{
05f6a46a
MZ
71 stamp="$1"
72 interval="$2"
73
10946ddc 74 if [ $interval -eq 0 ]; then
53391d0f 75 debug_echo "check_stamp: interval=0"
ff7c76f8 76 # treat as no time has passed
10946ddc
MZ
77 return 1
78 fi
79
05f6a46a 80 if [ ! -f $stamp ]; then
ff7c76f8
OA
81 debug_echo "check_stamp: missing time stamp file: $stamp."
82 # treat as enough time has passed
05f6a46a 83 return 0
0c132682 84 fi
05f6a46a
MZ
85
86 # compare midnight today to midnight the day the stamp was updated
a7c526b6
MV
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
05f6a46a 106 delta=$(($now-$stamp))
05f6a46a 107
ff7c76f8 108 # intervall is in days, convert to sec.
8a139d4d 109 interval=$(($interval*60*60*24))
ff7c76f8 110 debug_echo "check_stamp: interval=$interval, now=$now, stamp=$stamp, delta=$delta (sec)"
8a139d4d 111
6e7c6c3f
MV
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
05f6a46a
MZ
119 if [ $delta -ge $interval ]; then
120 return 0
121 fi
122
123 return 1
124}
125
126update_stamp()
127{
128 stamp="$1"
05f6a46a 129 touch $stamp
0c132682
MZ
130}
131
6cce801a 132# we check here if autoclean was enough sizewise
2e8a92e5 133check_size_constraints()
6cce801a 134{
6cce801a 135 MaxAge=0
6cce801a 136 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
c98870b0
MV
137 eval $(apt-config shell MaxAge APT::Periodic::MaxAge)
138
139 MinAge=2
8a139d4d 140 eval $(apt-config shell MinAge APT::Archives::MinAge)
c98870b0
MV
141 eval $(apt-config shell MinAge APT::Periodic::MinAge)
142
143 MaxSize=0
6cce801a 144 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
c98870b0
MV
145 eval $(apt-config shell MaxSize APT::Periodic::MaxSize)
146
147 CacheDir="var/cache/apt/"
6cce801a 148 eval $(apt-config shell CacheDir Dir::Cache)
c98870b0
MV
149 CacheDir=${CacheDir%/}
150
151 CacheArchive="archives/"
6cce801a 152 eval $(apt-config shell CacheArchive Dir::Cache::archives)
c98870b0 153 CacheArchive=${CacheArchive%/}
6cce801a
MV
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
01717245
MV
160
161 Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
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
c98870b0 204 debug_echo "skip remove by archive size: $file, delta=$delta < $MinAgeSec"
3408b58c 205 break
c98870b0
MV
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
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
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
d047c6da 291# sleep for a random interval of time (default 30min)
69c28efc
MV
292# (some code taken from cron-apt, thanks)
293random_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))
c98870b0 305 debug_echo "sleeping for $TIME seconds"
69c28efc
MV
306 sleep $TIME
307}
308
69c28efc 309
ff7c76f8 310debug_echo()
6cce801a 311{
ff7c76f8
OA
312 # Display message if $VERBOSE >= 1
313 if [ "$VERBOSE" -ge 1 ]; then
314 echo $1 1>&2
6cce801a 315 fi
ff7c76f8
OA
316}
317
c98870b0 318# ------------------------ main ----------------------------
69c28efc 319
ff7c76f8
OA
320# check apt-config exstance
321if ! which apt-config >/dev/null ; then
18d38975
OS
322 exit 0
323fi
87ddfb96 324
17443d48
JAK
325# check if the user really wants to do something
326AutoAptEnable=1 # default is yes
327eval $(apt-config shell AutoAptEnable APT::Periodic::Enable)
328
329if [ AutoAptEnable -eq 0 ]; then
330 exit 0
331fi
332
ff7c76f8 333# Set VERBOSE mode from apt-config (or inherit from environment)
6985efb3 334VERBOSE=0
ff7c76f8 335eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
c98870b0 336debug_echo "verbose level $VERBOSE"
ff7c76f8
OA
337if [ "$VERBOSE" -le 2 ]; then
338 # quiet for 0,1,2
339 XSTDOUT=">/dev/null"
340 XSTDERR="2>/dev/null"
341 XAPTOPT="-qq"
342 XUUPOPT=""
343else
344 XSTDOUT=""
345 XSTDERR=""
346 XAPTOPT=""
347 XUUPOPT="-d"
348fi
349if [ "$VERBOSE" -ge 3 ]; then
350 # trace output
351 set -x
352fi
8a139d4d 353
ff7c76f8
OA
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
359if which on_ac_power >/dev/null; then
360 on_ac_power
361 POWER=$?
362 if [ $POWER -eq 1 ]; then
c98870b0 363 debug_echo "exit: system NOT on main power"
ff7c76f8
OA
364 exit 0
365 elif [ $POWER -ne 0 ]; then
c98870b0 366 debug_echo "power status ($POWER) undetermined, continuing"
6cce801a 367 fi
ff7c76f8
OA
368 debug_echo "system is on main power."
369fi
8a139d4d 370
ff7c76f8 371# check if we can lock the cache and if the cache is clean
aff278bf 372if which apt-get >/dev/null && ! eval apt-get check -f $XAPTOPT $XSTDERR ; then
ff7c76f8
OA
373 debug_echo "error encountered in cron job with \"apt-get check\"."
374 exit 0
375fi
8a139d4d 376
ff7c76f8
OA
377# Global current time in seconds since 1970-01-01 00:00:00 UTC
378now=$(date +%s)
6cce801a 379
ff7c76f8
OA
380# Support old Archive for compatibility.
381# Document only Periodic for all controling parameters of this script.
ff7c76f8 382
0c132682 383UpdateInterval=0
ff7c76f8
OA
384eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists)
385
05f6a46a 386DownloadUpgradeableInterval=0
ff7c76f8 387eval $(apt-config shell DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
0c132682 388
fdd15654
MV
389UnattendedUpgradeInterval=0
390eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
391
ff7c76f8
OA
392AutocleanInterval=0
393eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
394
c98870b0
MV
395BackupArchiveInterval=0
396eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
ff7c76f8 397
4c2dcaa1
MV
398# check if we actually have to do anything
399if [ $UpdateInterval -eq 0 ] &&
400 [ $DownloadUpgradeableInterval -eq 0 ] &&
401 [ $UnattendedUpgradeInterval -eq 0 ] &&
2783b261 402 [ $BackupArchiveInterval -eq 0 ] &&
4c2dcaa1
MV
403 [ $AutocleanInterval -eq 0 ]; then
404 exit 0
405fi
fdd15654 406
c98870b0
MV
407# deal with BackupArchiveInterval
408do_cache_backup $BackupArchiveInterval
0c132682 409
4c2dcaa1
MV
410# sleep random amount of time to avoid hitting the
411# mirrors at the same time
69c28efc
MV
412random_sleep
413
ff7c76f8 414# update package lists
aff278bf 415UPDATED=0
05f6a46a
MZ
416UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
417if check_stamp $UPDATE_STAMP $UpdateInterval; then
ff7c76f8
OA
418 if eval apt-get $XAPTOPT -y update $XSTDERR; then
419 debug_echo "download updated metadata (success)."
be993931 420 if which dbus-send >/dev/null && pidof dbus-daemon >/dev/null; then
ff7c76f8
OA
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)"
05f6a46a 428 fi
ff7c76f8 429 update_stamp $UPDATE_STAMP
aff278bf 430 UPDATED=1
ff7c76f8 431 else
c98870b0 432 debug_echo "download updated metadata (error)"
05f6a46a 433 fi
ff7c76f8
OA
434else
435 debug_echo "download updated metadata (not run)."
fdd15654 436fi
c98870b0
MV
437
438# download all upgradeable packages (if it is requested)
439DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
aff278bf 440if [ $UPDATED -eq 1 ] && check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
c98870b0
MV
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
447else
448 debug_echo "download upgradable (not run)"
449fi
450
451# auto upgrade all upgradeable packages
452UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
aff278bf 453if [ $UPDATED -eq 1 ] && which unattended-upgrade >/dev/null && check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
c98870b0
MV
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
460else
461 debug_echo "unattended-upgrade (not run)"
462fi
fdd15654 463
ff7c76f8 464# autoclean package archive
de15fbae
MV
465AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
466if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
c98870b0 467 if eval apt-get $XAPTOPT -y autoclean $XSTDERR; then
ff7c76f8
OA
468 debug_echo "autoclean (success)."
469 update_stamp $AUTOCLEAN_STAMP
470 else
c98870b0 471 debug_echo "autoclean (error)"
ff7c76f8
OA
472 fi
473else
c98870b0 474 debug_echo "autoclean (not run)"
de15fbae
MV
475fi
476
0fad7309
MV
477# check cache size
478check_size_constraints
479
ff7c76f8
OA
480#
481# vim: set sts=4 ai :
482#
483