6 # This file understands the following apt configuration variables:
7 # Values here are the default.
8 # Create /etc/apt/apt.conf.d/02periodic file to set your preference.
11 # - RootDir for all configuration files
13 # Dir::Cache "var/apt/cache/";
14 # - Set apt package cache directory
16 # Dir::Cache::Archive "archives/";
17 # - Set package archive directory
19 # APT::Periodic::Enable "1";
20 # - Enable the update/upgrade script (0=disable)
22 # APT::Periodic::BackupArchiveInterval "0";
23 # - Backup after n-days if archive contents changed.(0=disable)
25 # APT::Periodic::BackupLevel "3";
26 # - Backup level.(0=disable), 1 is invalid.
28 # Dir::Cache::Backup "backup/";
29 # - Set periodic package backup directory
31 # APT::Archives::MaxAge "0"; (old, deprecated)
32 # APT::Periodic::MaxAge "0"; (new)
33 # - Set maximum allowed age of a cache package file. If a cache
34 # package file is older it is deleted (0=disable)
36 # APT::Archives::MinAge "2"; (old, deprecated)
37 # APT::Periodic::MinAge "2"; (new)
38 # - Set minimum age of a package file. If a file is younger it
39 # will not be deleted (0=disable). Usefull to prevent races
40 # and to keep backups of the packages for emergency.
42 # APT::Archives::MaxSize "0"; (old, deprecated)
43 # APT::Periodic::MaxSize "0"; (new)
44 # - Set maximum size of the cache in MB (0=disable). If the cache
45 # is bigger, cached package files are deleted until the size
46 # requirement is met (the biggest packages will be deleted
49 # APT::Periodic::Update-Package-Lists "0";
50 # - Do "apt-get update" automatically every n-days (0=disable)
52 # "APT::Periodic::Download-Upgradeable-Packages=0",
53 # - Do "apt-get upgrade --download-only" every n-days (0=disable)
55 # APT::Periodic::Download-Upgradeable-Packages-Debdelta "1";
56 # - Use debdelta-upgrade to download updates if available (0=disable)
58 # APT::Periodic::Unattended-Upgrade "0";
59 # - Run the "unattended-upgrade" security upgrade script
60 # every n-days (0=disabled)
61 # Requires the package "unattended-upgrades" and will write
62 # a log in /var/log/unattended-upgrades
64 # "APT::Archives::MaxAge",
65 # - Set maximum allowed age of a cache package file. If a cache
66 # package file is older it is deleted (0=disable)
68 # "APT::Archives::MaxSize",
69 # - Set maximum size of the cache in MB (0=disable). If the cache
70 # is bigger, cached package files are deleted until the size
71 # requirement is met (the biggest packages will be deleted
74 # "APT::Archives::MinAge"
75 # - Set minimum age of a package file. If a file is younger it
76 # will not be deleted (0=disable). Usefull to prevent races
77 # and to keep backups of the packages for emergency.
85 if [ $interval -eq 0 ]; then
86 debug_echo
"check_stamp: interval=0"
87 # treat as no time has passed
91 if [ ! -f $stamp ]; then
92 debug_echo
"check_stamp: missing time stamp file: $stamp."
93 # treat as enough time has passed
97 # compare midnight today to midnight the day the stamp was updated
99 stamp
=$(date --date=$(date -r $stamp_file --iso-8601) +%s
2>/dev
/null
)
100 if [ "$?" != "0" ]; then
101 # Due to some timezones returning 'invalid date' for midnight on
102 # certain dates (eg America/Sao_Paulo), if date returns with error
103 # remove the stamp file and return 0. See coreutils bug:
104 # http://lists.gnu.org/archive/html/bug-coreutils/2007-09/msg00176.html
109 now
=$(date --date=$(date --iso-8601) +%s
2>/dev
/null
)
110 if [ "$?" != "0" ]; then
111 # As above, due to some timezones returning 'invalid date' for midnight
112 # on certain dates (eg America/Sao_Paulo), if date returns with error
117 delta
=$(($now-$stamp))
119 # intervall is in days,
120 interval
=$(($interval*60*60*24))
121 #echo "stampfile: $1"
122 #echo "interval=$interval, now=$now, stamp=$stamp, delta=$delta"
124 # remove timestamps a day (or more) in the future and force re-check
125 if [ $stamp -gt $(($now+86400)) ]; then
126 echo "WARNING: file $stamp_file has a timestamp in the future: $stamp"
131 # remove timestamps a day (or more) in the future and force re-check
132 if [ $stamp -gt $(($now+86400)) ]; then
133 echo "WARNING: file $stamp_file has a timestamp in the future: $stamp"
138 if [ $delta -ge $interval ]; then
152 # we check here if autoclean was enough sizewise
153 check_size_constraints
()
156 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
157 eval $(apt-config shell MaxAge APT::Periodic::MaxAge)
160 eval $(apt-config shell MinAge APT::Archives::MinAge)
161 eval $(apt-config shell MinAge APT::Periodic::MinAge)
164 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
165 eval $(apt-config shell MaxSize APT::Periodic::MaxSize)
167 CacheDir
="var/cache/apt/"
168 eval $(apt-config shell CacheDir Dir::Cache)
169 CacheDir
=${CacheDir%/}
171 CacheArchive
="archives/"
172 eval $(apt-config shell CacheArchive Dir::Cache::archives)
173 CacheArchive
=${CacheArchive%/}
176 if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
177 echo "empty Dir::Cache or Dir::Cache::archives, exiting"
181 Cache
="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
184 if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
185 debug_echo
"aged: ctime <$MaxAge and mtime <$MaxAge and ctime>$MinAge and mtime>$MinAge"
186 find $Cache -name "*.deb" \
( -mtime +$MaxAge -and -ctime +$MaxAge \
) -and -not \
( -mtime -$MinAge -or -ctime -$MinAge \
) -print0 | xargs -r -0 rm -f
187 elif [ ! $MaxAge -eq 0 ]; then
188 debug_echo
"aged: ctime <$MaxAge and mtime <$MaxAge only"
189 find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
191 debug_echo
"skip aging since MaxAge is 0"
195 if [ ! $MaxSize -eq 0 ]; then
197 MaxSize
=$(($MaxSize*1024))
200 now
=$(date --date=$(date --iso-8601) +%s
)
201 MinAge
=$(($MinAge*24*60*60))
203 # reverse-sort by mtime
204 for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
207 # check if the cache is small enough
208 if [ $size -lt $MaxSize ]; then
209 debug_echo
"end remove by archive size: size=$size < $MaxSize"
213 # check for MinAge of the file
214 if [ $MinAge -ne 0 ]; then
215 # check both ctime and mtime
216 mtime
=$(stat -c %Y $file)
217 ctime
=$(stat -c %Z $file)
218 if [ $mtime -gt $ctime ]; then
219 delta
=$(($now-$mtime))
221 delta
=$(($now-$ctime))
223 if [ $delta -le $MinAge ]; then
224 debug_echo
"skip remove by archive size: $file, delta=$delta < $MinAgeSec"
228 debug_echo
"remove by archive size: $file, delta=$delta >= $MinAgeSec (sec), size=$size >= $MaxSize"
236 # deal with the Apt::Periodic::BackupArchiveInterval
239 BackupArchiveInterval
="$1"
240 if [ $BackupArchiveInterval -eq 0 ]; then
244 # Set default values and normalize
246 eval $(apt-config shell Dir Dir)
249 CacheDir
="var/cache/apt/"
250 eval $(apt-config shell CacheDir Dir::Cache)
251 CacheDir
=${CacheDir%/}
252 if [ -z "$CacheDir" ]; then
253 debug_echo
"practically empty Dir::Cache, exiting"
257 CacheArchive
="archives/"
258 eval $(apt-config shell CacheArchive Dir::Cache::Archives)
259 CacheArchive
=${CacheArchive%/}
260 if [ -z "$CacheArchive" ]; then
261 debug_echo
"practically empty Dir::Cache::archives, exiting"
266 eval $(apt-config shell BackupLevel APT::Periodic::BackupLevel)
267 if [ $BackupLevel -le 1 ]; then
271 CacheBackup
="backup/"
272 eval $(apt-config shell CacheBackup Dir::Cache::Backup)
273 CacheBackup
=${CacheBackup%/}
274 if [ -z "$CacheBackup" ]; then
275 echo "practically empty Dir::Cache::Backup, exiting" 1>&2
279 Cache
="${Dir}/${CacheDir}/${CacheArchive}/"
280 Back
="${Dir}/${CacheDir}/${CacheBackup}/"
281 BackX
="${Back}${CacheArchive}/"
282 for x
in $(seq 0 1 $((${BackupLevel}-1))); do
283 eval "Back${x}=${Back}${x}/"
286 # backup after n-days if archive contents changed.
287 # (This uses hardlink to save disk space)
288 BACKUP_ARCHIVE_STAMP
=/var
/lib
/apt
/periodic
/backup
-archive-stamp
289 if check_stamp
$BACKUP_ARCHIVE_STAMP $BackupArchiveInterval; then
290 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
292 rm -rf $Back$((${BackupLevel}-1))
293 for y
in $(seq $((${BackupLevel}-1)) -1 1); do
295 eval BackZ
=${Back}$(($y-1))
296 if [ -e $BackZ ]; then
297 mv -f $BackZ $BackY ;
300 cp -la $Cache $Back ; mv -f $BackX $Back0
301 update_stamp
$BACKUP_ARCHIVE_STAMP
302 debug_echo
"backup with hardlinks. (success)"
304 debug_echo
"skip backup since same content."
307 debug_echo
"skip backup since too new."
311 # sleep for a random interval of time (default 30min)
312 # (some code taken from cron-apt, thanks)
316 eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
317 if [ $RandomSleep -eq 0 ]; then
320 if [ -z "$RANDOM" ] ; then
321 # A fix for shells that do not have this bash feature.
322 RANDOM
=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -c"1-5")
324 TIME
=$(($RANDOM % $RandomSleep))
325 debug_echo
"sleeping for $TIME seconds"
332 # Display message if $VERBOSE >= 1
333 if [ "$VERBOSE" -ge 1 ]; then
338 # ------------------------ main ----------------------------
340 # check apt-config exstance
341 if ! which apt
-config >/dev
/null
; then
345 # check if the user really wants to do something
346 AutoAptEnable
=1 # default is yes
347 eval $(apt-config shell AutoAptEnable APT::Periodic::Enable)
349 if [ $AutoAptEnable -eq 0 ]; then
353 # Set VERBOSE mode from apt-config (or inherit from environment)
355 eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
356 debug_echo
"verbose level $VERBOSE"
357 if [ "$VERBOSE" -le 2 ]; then
360 XSTDERR
="2>/dev/null"
369 if [ "$VERBOSE" -ge 3 ]; then
374 # laptop check, on_ac_power returns:
375 # 0 (true) System is on main power
376 # 1 (false) System is not on main power
377 # 255 (false) Power status could not be determined
378 # Desktop systems always return 255 it seems
379 if which on_ac_power
>/dev
/null
; then
382 if [ $POWER -eq 1 ]; then
383 debug_echo
"exit: system NOT on main power"
385 elif [ $POWER -ne 0 ]; then
386 debug_echo
"power status ($POWER) undetermined, continuing"
388 debug_echo
"system is on main power."
391 # check if we can lock the cache and if the cache is clean
392 if which apt
-get >/dev
/null
&& ! eval apt
-get check
-f $XAPTOPT $XSTDERR ; then
393 debug_echo
"error encountered in cron job with \"apt-get check\"."
397 # Global current time in seconds since 1970-01-01 00:00:00 UTC
400 # Support old Archive for compatibility.
401 # Document only Periodic for all controling parameters of this script.
404 DownloadUpgradeableInterval
=0
405 eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
406 AutocleanInterval
=$DownloadUpgradeableInterval
407 eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
408 UnattendedUpgradeInterval
=0
409 eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
412 eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
414 BackupArchiveInterval
=0
415 eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
418 eval $(apt-config shell Debdelta APT::Periodic::Download-Upgradeable-Packages-Debdelta)
420 # check if we actually have to do anything that requires locking the cache
421 if [ $UpdateInterval -eq 0 ] &&
422 [ $DownloadUpgradeableInterval -eq 0 ] &&
423 [ $UnattendedUpgradeInterval -eq 0 ] &&
424 [ $AutocleanInterval -eq 0 ]; then
427 check_size_constraints
432 # set the proxy based on the admin users gconf settings
433 admin_user
=$(getent group admin|cut -d: -f4|cut -d, -f1)
434 if [ -n "$admin_user" ] && [ -x /usr
/bin
/sudo
] && [ -z "$http_proxy" ] && [ -x /usr
/bin
/gconftool
]; then
435 use
=$(sudo -u "$admin_user" gconftool --get /system/http_proxy/use_http_proxy 2>/dev/null)
436 host=$(sudo -u "$admin_user" gconftool --get /system/http_proxy/host 2>/dev/null)
437 port
=$(sudo -u "$admin_user" gconftool --get /system/http_proxy/port 2>/dev/null)
438 if [ "$use" = "true" ] && [ -n "$host" ] && [ -n "$port" ]; then
439 export http_proxy
="http://$host:$port/"
444 # deal with BackupArchiveInterval
445 do_cache_backup
$BackupArchiveInterval
447 # sleep random amount of time to avoid hitting the
448 # mirrors at the same time
451 # update package lists
453 UPDATE_STAMP
=/var
/lib
/apt
/periodic
/update
-stamp
454 if check_stamp
$UPDATE_STAMP $UpdateInterval; then
455 # check for a new archive signing key (against the master keyring)
456 if eval apt
-key net
-update $XSTDERR; then
457 debug_echo
"apt-key net-update (success)"
459 debug_echo
"apt-key net-update (failure)"
461 if eval apt
-get $XAPTOPT -y update
-o APT
::Update
::Auth
-Failure::=\"cp /usr
/share
/apt
/apt
-auth-failure.note
/var
/lib
/update
-notifier/user.d
/\" $XSTDERR; then
462 debug_echo
"download updated metadata (success)."
463 if which dbus
-send >/dev
/null
&& pidof dbus
-daemon >/dev
/null
; then
464 if dbus
-send --system / app.apt.dbus.updated boolean
:true
; then
465 debug_echo
"send dbus signal (success)"
467 debug_echo
"send dbus signal (error)"
470 debug_echo
"dbus signal not send (command not available)"
472 update_stamp
$UPDATE_STAMP
474 # now run apt-xapian-index if it is installed to ensure the index
476 if [ -x /usr
/sbin
/update
-apt-xapian-index ]; then
477 ionice
-c3 update
-apt-xapian-index -q
480 debug_echo
"download updated metadata (error)"
483 debug_echo
"download updated metadata (not run)."
486 # download all upgradeable packages (if it is requested)
487 DOWNLOAD_UPGRADEABLE_STAMP
=/var
/lib
/apt
/periodic
/download
-upgradeable-stamp
488 if [ $UPDATED -eq 1 ] && check_stamp
$DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
489 if [ $Debdelta -eq 1 ]; then
490 debdelta
-upgrade >/dev
/null
2>&1 || true
492 if eval apt
-get $XAPTOPT -y -d dist
-upgrade $XSTDERR; then
493 update_stamp
$DOWNLOAD_UPGRADEABLE_STAMP
494 debug_echo
"download upgradable (success)"
496 debug_echo
"download upgradable (error)"
499 debug_echo
"download upgradable (not run)"
502 # auto upgrade all upgradeable packages
503 UPGRADE_STAMP
=/var
/lib
/apt
/periodic
/upgrade
-stamp
504 if [ $UPDATED -eq 1 ] && which unattended
-upgrade >/dev
/null
&& check_stamp
$UPGRADE_STAMP $UnattendedUpgradeInterval; then
505 if unattended
-upgrade $XUUPOPT; then
506 update_stamp
$UPGRADE_STAMP
507 debug_echo
"unattended-upgrade (success)"
509 debug_echo
"unattended-upgrade (error)"
512 debug_echo
"unattended-upgrade (not run)"
515 # autoclean package archive
516 AUTOCLEAN_STAMP
=/var
/lib
/apt
/periodic
/autoclean
-stamp
517 if check_stamp
$AUTOCLEAN_STAMP $AutocleanInterval; then
518 if eval apt
-get $XAPTOPT -y autoclean
$XSTDERR; then
519 debug_echo
"autoclean (success)."
520 update_stamp
$AUTOCLEAN_STAMP
522 debug_echo
"autoclean (error)"
525 debug_echo
"autoclean (not run)"
529 check_size_constraints
532 # vim: set sts=4 ai :