]> git.saurik.com Git - apt.git/blob - debian/apt.cron.daily
39bf5bc1d04e94ddf0c47c9fddf82b1d0aadb3a2
[apt.git] / debian / apt.cron.daily
1 #!/bin/sh
2 #set -e
3 #
4 # This file understands the following apt configuration variables:
5 # Values here are the default.
6 # Create /etc/apt/apt.conf.d/02periodic file to set your preference.
7 #
8 # Dir "/";
9 # - RootDir for all configuration files
10 #
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)
28 # - Set maximum allowed age of a cache package file. If a cache
29 # package file is older it is deleted (0=disable)
30 #
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)
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 #
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
55 #
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
65
66 check_stamp()
67 {
68 stamp="$1"
69 interval="$2"
70
71 if [ $interval -eq 0 ]; then
72 debug_echo "check_stamp: interval=0."
73 # treat as no time has passed
74 return 1
75 fi
76
77 if [ ! -f $stamp ]; then
78 debug_echo "check_stamp: missing time stamp file: $stamp."
79 # treat as enough time has passed
80 return 0
81 fi
82
83 # compare midnight today to midnight the day the stamp was updated
84 stamp=$(date -r $stamp '+%s')
85 delta=$(($now-$stamp))
86
87 # intervall is in days, convert to sec.
88 interval=$(($interval*60*60*24))
89 debug_echo "check_stamp: interval=$interval, now=$now, stamp=$stamp, delta=$delta (sec)"
90
91 if [ $delta -ge $interval ]; then
92 return 0
93 fi
94
95 return 1
96 }
97
98 update_stamp()
99 {
100 stamp="$1"
101 touch $stamp
102 }
103
104 # we check here if autoclean was enough sizewise
105 check_size_constraints()
106 {
107 MaxAge=0
108 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
109 eval $(apt-config shell MaxAge APT::Periodic::MaxAge)
110
111 MinAge=2
112 eval $(apt-config shell MinAge APT::Archives::MinAge)
113 eval $(apt-config shell MinAge APT::Periodic::MinAge)
114
115 MaxSize=0
116 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
117 eval $(apt-config shell MaxSize APT::Periodic::MaxSize)
118
119 CacheDir="var/cache/apt/"
120 eval $(apt-config shell CacheDir Dir::Cache)
121 CacheDir=${CacheDir%/}
122
123 CacheArchive="archives/"
124 eval $(apt-config shell CacheArchive Dir::Cache::archives)
125 CacheArchive=${CacheArchive%/}
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
132
133 Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
134
135 # check age
136 if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
137 debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge and ctime>$MinAge and mtime>$MinAge"
138 find $Cache -name "*.deb" \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
139 elif [ ! $MaxAge -eq 0 ]; then
140 debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge only"
141 find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
142 else
143 debug_echo "skip aging since MaxAge is 0"
144 fi
145
146 # check size
147 if [ ! $MaxSize -eq 0 ]; then
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
155 # reverse-sort by mtime
156 for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
157 du=$(du -s $Cache)
158 size=${du%%/*}
159 # check if the cache is small enough
160 if [ $size -lt $MaxSize ]; then
161 debug_echo "end remove by archive size: size=$size < $MaxSize"
162 break
163 fi
164
165 # check for MinAge of the file
166 if [ $MinAge -ne 0 ]; then
167 # check both ctime and mtime
168 mtime=$(stat -c %Y $file)
169 ctime=$(stat -c %Z $file)
170 if [ $mtime -gt $ctime ]; then
171 delta=$(($now-$mtime))
172 else
173 delta=$(($now-$ctime))
174 fi
175 if [ $delta -le $MinAge ]; then
176 debug_echo "skip remove by archive size: $file, delta=$delta < $MinAgeSec"
177 break
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
182 fi
183 fi
184 done
185 fi
186 }
187
188 # deal with the Apt::Periodic::BackupArchiveInterval
189 do_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
263 # sleep for a random interval of time (default 30min)
264 # (some code taken from cron-apt, thanks)
265 random_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))
277 debug_echo "sleeping for $TIME seconds"
278 sleep $TIME
279 }
280
281
282 debug_echo()
283 {
284 # Display message if $VERBOSE >= 1
285 if [ "$VERBOSE" -ge 1 ]; then
286 echo $1 1>&2
287 fi
288 }
289
290 # ------------------------ main ----------------------------
291
292 # check apt-config exstance
293 if ! which apt-config >/dev/null ; then
294 exit 0
295 fi
296
297 # Set VERBOSE mode from apt-config (or inherit from environment)
298 eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
299 debug_echo "verbose level $VERBOSE"
300 if [ -z "$VERBOSE" ]; then
301 VERBOSE="0"
302 fi
303 if [ "$VERBOSE" -le 2 ]; then
304 # quiet for 0,1,2
305 XSTDOUT=">/dev/null"
306 XSTDERR="2>/dev/null"
307 XAPTOPT="-qq"
308 XUUPOPT=""
309 else
310 XSTDOUT=""
311 XSTDERR=""
312 XAPTOPT=""
313 XUUPOPT="-d"
314 fi
315 if [ "$VERBOSE" -ge 3 ]; then
316 # trace output
317 set -x
318 fi
319
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
325 if which on_ac_power >/dev/null; then
326 on_ac_power
327 POWER=$?
328 if [ $POWER -eq 1 ]; then
329 debug_echo "exit: system NOT on main power"
330 exit 0
331 elif [ $POWER -ne 0 ]; then
332 debug_echo "power status ($POWER) undetermined, continuing"
333 fi
334 debug_echo "system is on main power."
335 fi
336
337 # check if we can lock the cache and if the cache is clean
338 if 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
341 fi
342
343 # Global current time in seconds since 1970-01-01 00:00:00 UTC
344 now=$(date +%s)
345
346 # Support old Archive for compatibility.
347 # Document only Periodic for all controling parameters of this script.
348
349 UpdateInterval=0
350 eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists)
351
352 DownloadUpgradeableInterval=0
353 eval $(apt-config shell DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
354
355 UnattendedUpgradeInterval=0
356 eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
357
358 AutocleanInterval=0
359 eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
360
361 BackupArchiveInterval=0
362 eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
363
364 # check if we actually have to do anything
365 if [ $UpdateInterval -eq 0 ] &&
366 [ $DownloadUpgradeableInterval -eq 0 ] &&
367 [ $UnattendedUpgradeInterval -eq 0 ] &&
368 [ $BackupArchiveInterval -eq 0 ] &&
369 [ $AutocleanInterval -eq 0 ]; then
370 exit 0
371 fi
372
373 # deal with BackupArchiveInterval
374 do_cache_backup $BackupArchiveInterval
375
376 # sleep random amount of time to avoid hitting the
377 # mirrors at the same time
378 random_sleep
379
380 # update package lists
381 UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
382 if check_stamp $UPDATE_STAMP $UpdateInterval; then
383 if eval apt-get $XAPTOPT -y update $XSTDERR; then
384 debug_echo "download updated metadata (success)."
385 if which dbus-send >/dev/null && pidof dbus-daemon >/dev/null; then
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)"
393 fi
394 update_stamp $UPDATE_STAMP
395 else
396 debug_echo "download updated metadata (error)"
397 fi
398 else
399 debug_echo "download updated metadata (not run)."
400 fi
401
402 # download all upgradeable packages (if it is requested)
403 DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
404 if 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
411 else
412 debug_echo "download upgradable (not run)"
413 fi
414
415 # auto upgrade all upgradeable packages
416 UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
417 if 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
424 else
425 debug_echo "unattended-upgrade (not run)"
426 fi
427
428 # autoclean package archive
429 AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
430 if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
431 if eval apt-get $XAPTOPT -y autoclean $XSTDERR; then
432 debug_echo "autoclean (success)."
433 update_stamp $AUTOCLEAN_STAMP
434 else
435 debug_echo "autoclean (error)"
436 fi
437 else
438 debug_echo "autoclean (not run)"
439 fi
440
441 # check cache size
442 check_size_constraints
443
444 #
445 # vim: set sts=4 ai :
446 #
447