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