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.
9 # - RootDir for all configuration files
11 # Dir::Cache "var/apt/cache/";
12 # - Set apt package cache directory
14 # Dir::Cache::Archive "archives/";
15 # - Set package archive directory
17 # APT::Periodic::BackupArchiveInterval "0";
18 # - Backup after n-days if archive contents changed.(0=disable)
20 # APT::Periodic::BackupLevel "3";
21 # - Backup level.(0=disable), 1 is invalid.
23 # Dir::Cache::Backup "backup/";
24 # - Set periodic package backup directory
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)
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.
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
44 # APT::Periodic::Update-Package-Lists "0";
45 # - Do "apt-get update" automatically every n-days (0=disable)
47 # APT::Periodic::Download-Upgradeable-Packages "0";
48 # - Do "apt-get upgrade --download-only" every n-days (0=disable)
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
56 # APT::Periodic::AutocleanInterval "0";
57 # - Do "apt-get autoclean" every n-days (0=disable)
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)
71 if [ $interval -eq 0 ]; then
72 debug_echo
"check_stamp: interval=0."
73 # treat as no time has passed
77 if [ ! -f $stamp ]; then
79 debug_echo
"check_stamp: missing time stamp file: $stamp."
80 # treat as enough time has passed
84 # compare midnight today to midnight the day the stamp was updated
85 stamp
=$(date -r $stamp '+%s')
86 delta
=$(($now-$stamp))
88 # intervall is in days, convert to sec.
89 interval
=$(($interval*60*60*24))
90 debug_echo
"check_stamp: interval=$interval, now=$now, stamp=$stamp, delta=$delta (sec)"
92 if [ $delta -ge $interval ]; then
105 # we check here if autoclean was enough sizewise
106 check_size_constraints
()
112 CacheDir
="var/cache/apt"
113 CacheArchive
="archives/"
114 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
115 eval $(apt-config shell MinAge APT::Archives::MinAge)
116 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
117 eval $(apt-config shell Dir Dir)
118 eval $(apt-config shell CacheDir Dir::Cache)
119 eval $(apt-config shell CacheArchive Dir::Cache::archives)
122 if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
123 echo "empty Dir::Cache or Dir::Cache::archives, exiting"
127 Cache
="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
130 if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
131 find $Cache -name "*.deb" \
( -mtime +$MaxAge -and -ctime +$MaxAge \
) -and -not \
( -mtime -$MinAge -or -ctime -$MinAge \
) -print0 | xargs -r -0 rm -f
132 elif [ ! $MaxAge -eq 0 ]; then
133 find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
137 if [ ! $MaxSize -eq 0 ]; then
139 MaxSize
=$(($MaxSize*1024))
142 now
=$(date --date=$(date --iso-8601) +%s
)
143 MinAge
=$(($MinAge*24*60*60))
145 # reverse-sort by mtime
146 for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
149 # check if the cache is small enough
150 if [ $size -lt $MaxSize ]; then
154 # check for MinAge of the file
155 if [ ! $MinAge -eq 0 ]; then
156 # check both ctime and mtime
157 mtime
=$(stat -c %Y $file)
158 ctime
=$(stat -c %Z $file)
159 if [ $mtime -gt $ctime ]; then
160 delta
=$(($now-$mtime))
162 delta
=$(($now-$ctime))
164 #echo "$file ($delta), $MinAge"
165 if [ $delta -le $MinAge ]; then
166 #echo "Skiping $file (delta=$delta)"
177 # sleep for a random interval of time (default 30min)
178 # (some code taken from cron-apt, thanks)
182 eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
183 if [ $RandomSleep -eq 0 ]; then
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")
190 TIME
=$(($RANDOM % $RandomSleep))
197 # Display message if $VERBOSE >= 1
198 if [ "$VERBOSE" -ge 1 ]; then
205 # check apt-config exstance
206 if ! which apt
-config >/dev
/null
; then
210 # Set VERBOSE mode from apt-config (or inherit from environment)
211 eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
212 if [ -z "$VERBOSE" ]; then
215 if [ "$VERBOSE" -le 2 ]; then
218 XSTDERR
="2>/dev/null"
227 if [ "$VERBOSE" -ge 3 ]; then
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
237 if which on_ac_power
>/dev
/null
; then
240 if [ $POWER -eq 1 ]; then
241 debug_echo
"exit: system on main power."
243 elif [ $POWER -ne 0 ]; then
244 debug_echo
"exit: power status ($POWER) undetermined."
247 debug_echo
"system is on main power."
250 # check if we can lock the cache and if the cache is clean
251 if which apt
-get >/dev
/null
&& ! eval apt
-get check
$XAPTOPT $XSTDERR ; then
252 debug_echo
"error encountered in cron job with \"apt-get check\"."
255 # No need to check for apt-get below
257 # Global current time in seconds since 1970-01-01 00:00:00 UTC
260 # Set default values and normalize
262 eval $(apt-config shell Dir Dir)
265 CacheDir
="var/cache/apt/"
266 eval $(apt-config shell CacheDir Dir::Cache)
267 CacheDir
=${CacheDir%/}
268 if [ -z "$CacheDir" ]; then
269 debug_echo
"practically empty Dir::Cache, exiting"
273 CacheArchive
="archives/"
274 eval $(apt-config shell CacheArchive Dir::Cache::Archives)
275 CacheArchive
=${CacheArchive%/}
276 if [ -z "$CacheArchive" ]; then
277 debug_echo
"practically empty Dir::Cache::archives, exiting"
281 BackupArchiveInterval
=0
282 eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
285 eval $(apt-config shell BackupLevel APT::Periodic::BackupLevel)
286 if [ $BackupLevel -le 1 ]; then BackupLevel
=2 ; fi
288 CacheBackup
="backup/"
289 eval $(apt-config shell CacheBackup Dir::Cache::Backup)
290 CacheBackup
=${CacheBackup%/}
291 if [ -z "$CacheBackup" ]; then
292 echo "practically empty Dir::Cache::Backup, exiting" 1>&2
296 # Support old Archive for compatibility.
297 # Document only Periodic for all controling parameters of this script.
299 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
300 eval $(apt-config shell MaxAge APT::Periodic::MaxAge)
303 eval $(apt-config shell MinAge APT::Archives::MinAge)
304 eval $(apt-config shell MinAge APT::Periodic::MinAge)
307 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
308 eval $(apt-config shell MaxSize APT::Periodic::MaxSize)
311 eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists)
313 DownloadUpgradeableInterval
=0
314 eval $(apt-config shell DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
316 UnattendedUpgradeInterval
=0
317 eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
320 eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
322 Cache
="${Dir}/${CacheDir}/${CacheArchive}/"
323 Back
="${Dir}/${CacheDir}/${CacheBackup}/"
324 BackX
="${Back}${CacheArchive}/"
325 for x
in $(seq 0 1 $((${BackupLevel}-1))); do
326 eval "Back${x}=${Back}${x}/"
329 # check if we actually have to do anything
330 if [ $UpdateInterval -eq 0 ] &&
331 [ $DownloadUpgradeableInterval -eq 0 ] &&
332 [ $UnattendedUpgradeInterval -eq 0 ] &&
333 [ $BackupArchiveInterval -eq 0 ] &&
334 [ $AutocleanInterval -eq 0 ]; then
339 # backup after n-days if archive contents changed.
340 # (This uses hardlink to save disk space)
341 BACKUP_ARCHIVE_STAMP
=/var
/lib
/apt
/periodic
/backup
-archive-stamp
342 if 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
345 rm -rf $Back$((${BackupLevel}-1))
346 for y
in $(seq $((${BackupLevel}-1)) -1 1); do
348 eval BackZ
=${Back}$(($y-1))
349 if [ -e $BackZ ]; then mv -f $BackZ $BackY ; fi
351 cp -la $Cache $Back ; mv -f $BackX $Back0
352 update_stamp
$BACKUP_ARCHIVE_STAMP
353 debug_echo
"backup with hardlinks. (success)"
356 debug_echo
"skip backup since same content."
359 debug_echo
"skip backup since too new."
362 # package archive contnts removal by package age
363 if [ $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"
366 elif [ $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"
370 debug_echo
"skip aging since MaxAge is 0"
373 # package archive contnts removal down to $MaxSize
374 if [ $MaxSize -ne 0 ]; then
376 MinAgeSec
=$(($MinAge*24*60*60))
378 # reverse-sort by mtime
379 for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
380 du
=$(du -m -s $Cache)
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"
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))
396 delta
=$(($now-$ctime))
398 if [ $delta -le $MinAgeSec ]; then
399 debug_echo
"skip remove by archive size: $file, delta=$delta < $MinAgeSec"
402 debug_echo
"remove by archive size: $file, delta=$delta >= $MinAgeSec (sec), size=$size >= $MaxSize"
410 # update package lists
411 UPDATE_STAMP
=/var
/lib
/apt
/periodic
/update
-stamp
412 if check_stamp
$UPDATE_STAMP $UpdateInterval; then
413 if eval apt
-get $XAPTOPT -y update
$XSTDERR; then
414 debug_echo
"download updated metadata (success)."
415 if which dbus
-send >/dev
/null
&& pidof dbus
-daemon >/dev
/null
; then
416 if dbus
-send --system / app.apt.dbus.updated boolean
:true
; then
417 debug_echo
"send dbus signal (success)"
419 debug_echo
"send dbus signal (error)"
422 debug_echo
"dbus signal not send (command not available)"
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)."
438 debug_echo
"unattended-upgrade (error)."
441 debug_echo
"unattended-upgrade (not run)."
444 debug_echo
"download upgradable (error)."
447 debug_echo
"download upgradable (not run)."
450 debug_echo
"download updated metadata (error)."
453 debug_echo
"download updated metadata (not run)."
456 # autoclean package archive
457 AUTOCLEAN_STAMP
=/var
/lib
/apt
/periodic
/autoclean
-stamp
458 if check_stamp
$AUTOCLEAN_STAMP $AutocleanInterval; then
459 if apt
-get $XAPTOPT -y autoclean
$XSTDERR; then
460 debug_echo
"autoclean (success)."
461 update_stamp
$AUTOCLEAN_STAMP
463 debug_echo
"autoclean (error)."
466 debug_echo
"autoclean (not run)."
470 # vim: set sts=4 ai :