]> git.saurik.com Git - apt.git/blame - debian/apt.cron.daily
[debian/changelog] correct two typos in the changelog for 0.7.22
[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
OA
322# Set VERBOSE mode from apt-config (or inherit from environment)
323eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
c98870b0 324debug_echo "verbose level $VERBOSE"
ff7c76f8
OA
325if [ -z "$VERBOSE" ]; then
326 VERBOSE="0"
327fi
328if [ "$VERBOSE" -le 2 ]; then
329 # quiet for 0,1,2
330 XSTDOUT=">/dev/null"
331 XSTDERR="2>/dev/null"
332 XAPTOPT="-qq"
333 XUUPOPT=""
334else
335 XSTDOUT=""
336 XSTDERR=""
337 XAPTOPT=""
338 XUUPOPT="-d"
339fi
340if [ "$VERBOSE" -ge 3 ]; then
341 # trace output
342 set -x
343fi
8a139d4d 344
ff7c76f8
OA
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
350if which on_ac_power >/dev/null; then
351 on_ac_power
352 POWER=$?
353 if [ $POWER -eq 1 ]; then
c98870b0 354 debug_echo "exit: system NOT on main power"
ff7c76f8
OA
355 exit 0
356 elif [ $POWER -ne 0 ]; then
c98870b0 357 debug_echo "power status ($POWER) undetermined, continuing"
6cce801a 358 fi
ff7c76f8
OA
359 debug_echo "system is on main power."
360fi
8a139d4d 361
ff7c76f8 362# check if we can lock the cache and if the cache is clean
aff278bf 363if which apt-get >/dev/null && ! eval apt-get check -f $XAPTOPT $XSTDERR ; then
ff7c76f8
OA
364 debug_echo "error encountered in cron job with \"apt-get check\"."
365 exit 0
366fi
8a139d4d 367
ff7c76f8
OA
368# Global current time in seconds since 1970-01-01 00:00:00 UTC
369now=$(date +%s)
6cce801a 370
ff7c76f8
OA
371# Support old Archive for compatibility.
372# Document only Periodic for all controling parameters of this script.
ff7c76f8 373
0c132682 374UpdateInterval=0
ff7c76f8
OA
375eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists)
376
05f6a46a 377DownloadUpgradeableInterval=0
ff7c76f8 378eval $(apt-config shell DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
0c132682 379
fdd15654
MV
380UnattendedUpgradeInterval=0
381eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
382
ff7c76f8
OA
383AutocleanInterval=0
384eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
385
c98870b0
MV
386BackupArchiveInterval=0
387eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
ff7c76f8 388
4c2dcaa1
MV
389# check if we actually have to do anything
390if [ $UpdateInterval -eq 0 ] &&
391 [ $DownloadUpgradeableInterval -eq 0 ] &&
392 [ $UnattendedUpgradeInterval -eq 0 ] &&
2783b261 393 [ $BackupArchiveInterval -eq 0 ] &&
4c2dcaa1
MV
394 [ $AutocleanInterval -eq 0 ]; then
395 exit 0
396fi
fdd15654 397
c98870b0
MV
398# deal with BackupArchiveInterval
399do_cache_backup $BackupArchiveInterval
0c132682 400
4c2dcaa1
MV
401# sleep random amount of time to avoid hitting the
402# mirrors at the same time
69c28efc
MV
403random_sleep
404
ff7c76f8 405# update package lists
aff278bf 406UPDATED=0
05f6a46a
MZ
407UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
408if check_stamp $UPDATE_STAMP $UpdateInterval; then
ff7c76f8
OA
409 if eval apt-get $XAPTOPT -y update $XSTDERR; then
410 debug_echo "download updated metadata (success)."
be993931 411 if which dbus-send >/dev/null && pidof dbus-daemon >/dev/null; then
ff7c76f8
OA
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)"
05f6a46a 419 fi
ff7c76f8 420 update_stamp $UPDATE_STAMP
aff278bf 421 UPDATED=1
ff7c76f8 422 else
c98870b0 423 debug_echo "download updated metadata (error)"
05f6a46a 424 fi
ff7c76f8
OA
425else
426 debug_echo "download updated metadata (not run)."
fdd15654 427fi
c98870b0
MV
428
429# download all upgradeable packages (if it is requested)
430DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
aff278bf 431if [ $UPDATED -eq 1 ] && check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
c98870b0
MV
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
438else
439 debug_echo "download upgradable (not run)"
440fi
441
442# auto upgrade all upgradeable packages
443UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
aff278bf 444if [ $UPDATED -eq 1 ] && which unattended-upgrade >/dev/null && check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
c98870b0
MV
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
451else
452 debug_echo "unattended-upgrade (not run)"
453fi
fdd15654 454
ff7c76f8 455# autoclean package archive
de15fbae
MV
456AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
457if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
c98870b0 458 if eval apt-get $XAPTOPT -y autoclean $XSTDERR; then
ff7c76f8
OA
459 debug_echo "autoclean (success)."
460 update_stamp $AUTOCLEAN_STAMP
461 else
c98870b0 462 debug_echo "autoclean (error)"
ff7c76f8
OA
463 fi
464else
c98870b0 465 debug_echo "autoclean (not run)"
de15fbae
MV
466fi
467
0fad7309
MV
468# check cache size
469check_size_constraints
470
ff7c76f8
OA
471#
472# vim: set sts=4 ai :
473#
474