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