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::Enable "1";
18 # - Enable the update/upgrade script (0=disable)
20 # APT::Periodic::BackupArchiveInterval "0";
21 # - Backup after n-days if archive contents changed.(0=disable)
23 # APT::Periodic::BackupLevel "3";
24 # - Backup level.(0=disable), 1 is invalid.
26 # Dir::Cache::Backup "backup/";
27 # - Set periodic package backup directory
29 # APT::Archives::MaxAge "0"; (old, deprecated)
30 # APT::Periodic::MaxAge "0"; (new)
31 # - Set maximum allowed age of a cache package file. If a cache
32 # package file is older it is deleted (0=disable)
34 # APT::Archives::MinAge "2"; (old, deprecated)
35 # APT::Periodic::MinAge "2"; (new)
36 # - Set minimum age of a package file. If a file is younger it
37 # will not be deleted (0=disable). Usefull to prevent races
38 # and to keep backups of the packages for emergency.
40 # APT::Archives::MaxSize "0"; (old, deprecated)
41 # APT::Periodic::MaxSize "0"; (new)
42 # - Set maximum size of the cache in MB (0=disable). If the cache
43 # is bigger, cached package files are deleted until the size
44 # requirement is met (the biggest packages will be deleted
47 # APT::Periodic::Update-Package-Lists "0";
48 # - Do "apt-get update" automatically every n-days (0=disable)
50 # APT::Periodic::Download-Upgradeable-Packages "0";
51 # - Do "apt-get upgrade --download-only" every n-days (0=disable)
53 # APT::Periodic::Download-Upgradeable-Packages-Debdelta "1";
54 # - Use debdelta-upgrade to download updates if available (0=disable)
56 # APT::Periodic::Unattended-Upgrade "0";
57 # - Run the "unattended-upgrade" security upgrade script
58 # every n-days (0=disabled)
59 # Requires the package "unattended-upgrades" and will write
60 # a log in /var/log/unattended-upgrades
62 # APT::Periodic::AutocleanInterval "0";
63 # - Do "apt-get autoclean" every n-days (0=disable)
65 # APT::Periodic::Verbose "0";
66 # - Send report mail to root
67 # 0: no report (or null string)
68 # 1: progress report (actually any string)
69 # 2: + command outputs (remove -qq, remove 2>/dev/null, add -d)
77 if [ $interval -eq 0 ]; then
78 debug_echo
"check_stamp: interval=0"
79 # treat as no time has passed
83 if [ ! -f $stamp ]; then
84 debug_echo
"check_stamp: missing time stamp file: $stamp."
85 # treat as enough time has passed
89 # compare midnight today to midnight the day the stamp was updated
91 stamp
=$(date --date=$(date -r $stamp_file --iso-8601) +%s
2>/dev
/null
)
92 if [ "$?" != "0" ]; then
93 # Due to some timezones returning 'invalid date' for midnight on
94 # certain dates (eg America/Sao_Paulo), if date returns with error
95 # remove the stamp file and return 0. See coreutils bug:
96 # http://lists.gnu.org/archive/html/bug-coreutils/2007-09/msg00176.html
101 now
=$(date --date=$(date --iso-8601) +%s
2>/dev
/null
)
102 if [ "$?" != "0" ]; then
103 # As above, due to some timezones returning 'invalid date' for midnight
104 # on certain dates (eg America/Sao_Paulo), if date returns with error
109 delta
=$(($now-$stamp))
111 # intervall is in days, convert to sec.
112 interval
=$(($interval*60*60*24))
113 debug_echo
"check_stamp: interval=$interval, now=$now, stamp=$stamp, delta=$delta (sec)"
115 # remove timestamps a day (or more) in the future and force re-check
116 if [ $stamp -gt $(($now+86400)) ]; then
117 echo "WARNING: file $stamp_file has a timestamp in the future: $stamp"
122 if [ $delta -ge $interval ]; then
135 # we check here if autoclean was enough sizewise
136 check_size_constraints
()
139 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
140 eval $(apt-config shell MaxAge APT::Periodic::MaxAge)
143 eval $(apt-config shell MinAge APT::Archives::MinAge)
144 eval $(apt-config shell MinAge APT::Periodic::MinAge)
147 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
148 eval $(apt-config shell MaxSize APT::Periodic::MaxSize)
150 CacheDir
="var/cache/apt/"
151 eval $(apt-config shell CacheDir Dir::Cache)
152 CacheDir
=${CacheDir%/}
154 CacheArchive
="archives/"
155 eval $(apt-config shell CacheArchive Dir::Cache::archives)
156 CacheArchive
=${CacheArchive%/}
159 if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
160 echo "empty Dir::Cache or Dir::Cache::archives, exiting"
164 Cache
="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
167 if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
168 debug_echo
"aged: ctime <$MaxAge and mtime <$MaxAge and ctime>$MinAge and mtime>$MinAge"
169 find $Cache -name "*.deb" \
( -mtime +$MaxAge -and -ctime +$MaxAge \
) -and -not \
( -mtime -$MinAge -or -ctime -$MinAge \
) -print0 | xargs -r -0 rm -f
170 elif [ ! $MaxAge -eq 0 ]; then
171 debug_echo
"aged: ctime <$MaxAge and mtime <$MaxAge only"
172 find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
174 debug_echo
"skip aging since MaxAge is 0"
178 if [ ! $MaxSize -eq 0 ]; then
180 MaxSize
=$(($MaxSize*1024))
183 now
=$(date --date=$(date --iso-8601) +%s
)
184 MinAge
=$(($MinAge*24*60*60))
186 # reverse-sort by mtime
187 for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
190 # check if the cache is small enough
191 if [ $size -lt $MaxSize ]; then
192 debug_echo
"end remove by archive size: size=$size < $MaxSize"
196 # check for MinAge of the file
197 if [ $MinAge -ne 0 ]; then
198 # check both ctime and mtime
199 mtime
=$(stat -c %Y $file)
200 ctime
=$(stat -c %Z $file)
201 if [ $mtime -gt $ctime ]; then
202 delta
=$(($now-$mtime))
204 delta
=$(($now-$ctime))
206 if [ $delta -le $MinAge ]; then
207 debug_echo
"skip remove by archive size: $file, delta=$delta < $MinAgeSec"
211 debug_echo
"remove by archive size: $file, delta=$delta >= $MinAgeSec (sec), size=$size >= $MaxSize"
219 # deal with the Apt::Periodic::BackupArchiveInterval
222 BackupArchiveInterval
="$1"
223 if [ $BackupArchiveInterval -eq 0 ]; then
227 # Set default values and normalize
229 eval $(apt-config shell Dir Dir)
232 CacheDir
="var/cache/apt/"
233 eval $(apt-config shell CacheDir Dir::Cache)
234 CacheDir
=${CacheDir%/}
235 if [ -z "$CacheDir" ]; then
236 debug_echo
"practically empty Dir::Cache, exiting"
240 CacheArchive
="archives/"
241 eval $(apt-config shell CacheArchive Dir::Cache::Archives)
242 CacheArchive
=${CacheArchive%/}
243 if [ -z "$CacheArchive" ]; then
244 debug_echo
"practically empty Dir::Cache::archives, exiting"
249 eval $(apt-config shell BackupLevel APT::Periodic::BackupLevel)
250 if [ $BackupLevel -le 1 ]; then
254 CacheBackup
="backup/"
255 eval $(apt-config shell CacheBackup Dir::Cache::Backup)
256 CacheBackup
=${CacheBackup%/}
257 if [ -z "$CacheBackup" ]; then
258 echo "practically empty Dir::Cache::Backup, exiting" 1>&2
262 Cache
="${Dir}/${CacheDir}/${CacheArchive}/"
263 Back
="${Dir}/${CacheDir}/${CacheBackup}/"
264 BackX
="${Back}${CacheArchive}/"
265 for x
in $(seq 0 1 $((${BackupLevel}-1))); do
266 eval "Back${x}=${Back}${x}/"
269 # backup after n-days if archive contents changed.
270 # (This uses hardlink to save disk space)
271 BACKUP_ARCHIVE_STAMP
=/var
/lib
/apt
/periodic
/backup
-archive-stamp
272 if check_stamp
$BACKUP_ARCHIVE_STAMP $BackupArchiveInterval; then
273 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
275 rm -rf $Back$((${BackupLevel}-1))
276 for y
in $(seq $((${BackupLevel}-1)) -1 1); do
278 eval BackZ
=${Back}$(($y-1))
279 if [ -e $BackZ ]; then
280 mv -f $BackZ $BackY ;
283 cp -la $Cache $Back ; mv -f $BackX $Back0
284 update_stamp
$BACKUP_ARCHIVE_STAMP
285 debug_echo
"backup with hardlinks. (success)"
287 debug_echo
"skip backup since same content."
290 debug_echo
"skip backup since too new."
294 # sleep for a random interval of time (default 30min)
295 # (some code taken from cron-apt, thanks)
299 eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
300 if [ $RandomSleep -eq 0 ]; then
303 if [ -z "$RANDOM" ] ; then
304 # A fix for shells that do not have this bash feature.
305 RANDOM
=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -c"1-5")
307 TIME
=$(($RANDOM % $RandomSleep))
308 debug_echo
"sleeping for $TIME seconds"
315 # Display message if $VERBOSE >= 1
316 if [ "$VERBOSE" -ge 1 ]; then
321 # ------------------------ main ----------------------------
323 # Backup the 7 last versions of APT's extended_states file
324 # shameless copy from dpkg cron
325 if cd /var
/backups
; then
326 if ! cmp -s apt.extended_states
.0 /var
/lib
/apt
/extended_states
; then
327 cp -p /var
/lib
/apt
/extended_states apt.extended_states
328 savelog
-c 7 apt.extended_states
>/dev
/null
332 # check apt-config exstance
333 if ! which apt
-config >/dev
/null
; then
337 # check if the user really wants to do something
338 AutoAptEnable
=1 # default is yes
339 eval $(apt-config shell AutoAptEnable APT::Periodic::Enable)
341 if [ $AutoAptEnable -eq 0 ]; then
345 # Set VERBOSE mode from apt-config (or inherit from environment)
347 eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
348 debug_echo
"verbose level $VERBOSE"
349 if [ "$VERBOSE" -le 2 ]; then
352 XSTDERR
="2>/dev/null"
361 if [ "$VERBOSE" -ge 3 ]; then
366 # laptop check, on_ac_power returns:
367 # 0 (true) System is on main power
368 # 1 (false) System is not on main power
369 # 255 (false) Power status could not be determined
370 # Desktop systems always return 255 it seems
371 if which on_ac_power
>/dev
/null
; then
374 if [ $POWER -eq 1 ]; then
375 debug_echo
"exit: system NOT on main power"
377 elif [ $POWER -ne 0 ]; then
378 debug_echo
"power status ($POWER) undetermined, continuing"
380 debug_echo
"system is on main power."
383 # check if we can lock the cache and if the cache is clean
384 if which apt
-get >/dev
/null
&& ! eval apt
-get check
-f $XAPTOPT $XSTDERR ; then
385 debug_echo
"error encountered in cron job with \"apt-get check\"."
389 # Global current time in seconds since 1970-01-01 00:00:00 UTC
392 # Support old Archive for compatibility.
393 # Document only Periodic for all controling parameters of this script.
396 eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists)
398 DownloadUpgradeableInterval
=0
399 eval $(apt-config shell DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
401 UnattendedUpgradeInterval
=0
402 eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
405 eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
407 BackupArchiveInterval
=0
408 eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
411 eval $(apt-config shell Debdelta APT::Periodic::Download-Upgradeable-Packages-Debdelta)
413 # check if we actually have to do anything that requires locking the cache
414 if [ $UpdateInterval -eq 0 ] &&
415 [ $DownloadUpgradeableInterval -eq 0 ] &&
416 [ $UnattendedUpgradeInterval -eq 0 ] &&
417 [ $BackupArchiveInterval -eq 0 ] &&
418 [ $AutocleanInterval -eq 0 ]; then
421 check_size_constraints
426 # deal with BackupArchiveInterval
427 do_cache_backup
$BackupArchiveInterval
429 # sleep random amount of time to avoid hitting the
430 # mirrors at the same time
433 # include default system language so that "apt-get update" will
434 # fetch the right translated package descriptions
435 if [ -r /etc
/default
/locale
]; then
436 .
/etc
/default
/locale
437 export LANG LANGUAGE LC_MESSAGES LC_ALL
440 # update package lists
442 UPDATE_STAMP
=/var
/lib
/apt
/periodic
/update
-stamp
443 if check_stamp
$UPDATE_STAMP $UpdateInterval; then
444 if eval apt
-get $XAPTOPT -y update
$XSTDERR; then
445 debug_echo
"download updated metadata (success)."
446 if which dbus
-send >/dev
/null
&& pidof dbus
-daemon >/dev
/null
; then
447 if dbus
-send --system / app.apt.dbus.updated boolean
:true
; then
448 debug_echo
"send dbus signal (success)"
450 debug_echo
"send dbus signal (error)"
453 debug_echo
"dbus signal not send (command not available)"
455 update_stamp
$UPDATE_STAMP
458 debug_echo
"download updated metadata (error)"
461 debug_echo
"download updated metadata (not run)."
464 # download all upgradeable packages (if it is requested)
465 DOWNLOAD_UPGRADEABLE_STAMP
=/var
/lib
/apt
/periodic
/download
-upgradeable-stamp
466 if [ $UPDATED -eq 1 ] && check_stamp
$DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
467 if [ $Debdelta -eq 1 ]; then
468 debdelta
-upgrade >/dev
/null
2>&1 || true
470 if eval apt
-get $XAPTOPT -y -d dist
-upgrade $XSTDERR; then
471 update_stamp
$DOWNLOAD_UPGRADEABLE_STAMP
472 debug_echo
"download upgradable (success)"
474 debug_echo
"download upgradable (error)"
477 debug_echo
"download upgradable (not run)"
480 # auto upgrade all upgradeable packages
481 UPGRADE_STAMP
=/var
/lib
/apt
/periodic
/upgrade
-stamp
482 if [ $UPDATED -eq 1 ] && which unattended
-upgrade >/dev
/null
&& check_stamp
$UPGRADE_STAMP $UnattendedUpgradeInterval; then
483 if unattended
-upgrade $XUUPOPT; then
484 update_stamp
$UPGRADE_STAMP
485 debug_echo
"unattended-upgrade (success)"
487 debug_echo
"unattended-upgrade (error)"
490 debug_echo
"unattended-upgrade (not run)"
493 # autoclean package archive
494 AUTOCLEAN_STAMP
=/var
/lib
/apt
/periodic
/autoclean
-stamp
495 if check_stamp
$AUTOCLEAN_STAMP $AutocleanInterval; then
496 if eval apt
-get $XAPTOPT -y autoclean
$XSTDERR; then
497 debug_echo
"autoclean (success)."
498 update_stamp
$AUTOCLEAN_STAMP
500 debug_echo
"autoclean (error)"
503 debug_echo
"autoclean (not run)"
507 check_size_constraints
510 # vim: set sts=4 ai :