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
78 debug_echo
"check_stamp: missing time stamp file: $stamp."
79 # treat as enough time has passed
83 # compare midnight today to midnight the day the stamp was updated
85 stamp
=$(date --date=$(date -r $stamp_file --iso-8601) +%s
2>/dev
/null
)
86 if [ "$?" != "0" ]; then
87 # Due to some timezones returning 'invalid date' for midnight on
88 # certain dates (eg America/Sao_Paulo), if date returns with error
89 # remove the stamp file and return 0. See coreutils bug:
90 # http://lists.gnu.org/archive/html/bug-coreutils/2007-09/msg00176.html
95 now
=$(date --date=$(date --iso-8601) +%s
2>/dev
/null
)
96 if [ "$?" != "0" ]; then
97 # As above, due to some timezones returning 'invalid date' for midnight
98 # on certain dates (eg America/Sao_Paulo), if date returns with error
103 delta
=$(($now-$stamp))
105 # intervall is in days, convert to sec.
106 interval
=$(($interval*60*60*24))
107 debug_echo
"check_stamp: interval=$interval, now=$now, stamp=$stamp, delta=$delta (sec)"
109 # remove timestamps a day (or more) in the future and force re-check
110 if [ $stamp -gt $(($now+86400)) ]; then
111 echo "WARNING: file $stamp_file has a timestamp in the future: $stamp"
116 if [ $delta -ge $interval ]; then
129 # we check here if autoclean was enough sizewise
130 check_size_constraints
()
133 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
134 eval $(apt-config shell MaxAge APT::Periodic::MaxAge)
137 eval $(apt-config shell MinAge APT::Archives::MinAge)
138 eval $(apt-config shell MinAge APT::Periodic::MinAge)
141 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
142 eval $(apt-config shell MaxSize APT::Periodic::MaxSize)
144 CacheDir
="var/cache/apt/"
145 eval $(apt-config shell CacheDir Dir::Cache)
146 CacheDir
=${CacheDir%/}
148 CacheArchive
="archives/"
149 eval $(apt-config shell CacheArchive Dir::Cache::archives)
150 CacheArchive
=${CacheArchive%/}
153 if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
154 echo "empty Dir::Cache or Dir::Cache::archives, exiting"
158 Cache
="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
161 if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
162 debug_echo
"aged: ctime <$MaxAge and mtime <$MaxAge and ctime>$MinAge and mtime>$MinAge"
163 find $Cache -name "*.deb" \
( -mtime +$MaxAge -and -ctime +$MaxAge \
) -and -not \
( -mtime -$MinAge -or -ctime -$MinAge \
) -print0 | xargs -r -0 rm -f
164 elif [ ! $MaxAge -eq 0 ]; then
165 debug_echo
"aged: ctime <$MaxAge and mtime <$MaxAge only"
166 find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
168 debug_echo
"skip aging since MaxAge is 0"
172 if [ ! $MaxSize -eq 0 ]; then
174 MaxSize
=$(($MaxSize*1024))
177 now
=$(date --date=$(date --iso-8601) +%s
)
178 MinAge
=$(($MinAge*24*60*60))
180 # reverse-sort by mtime
181 for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
184 # check if the cache is small enough
185 if [ $size -lt $MaxSize ]; then
186 debug_echo
"end remove by archive size: size=$size < $MaxSize"
190 # check for MinAge of the file
191 if [ $MinAge -ne 0 ]; then
192 # check both ctime and mtime
193 mtime
=$(stat -c %Y $file)
194 ctime
=$(stat -c %Z $file)
195 if [ $mtime -gt $ctime ]; then
196 delta
=$(($now-$mtime))
198 delta
=$(($now-$ctime))
200 if [ $delta -le $MinAge ]; then
201 debug_echo
"skip remove by archive size: $file, delta=$delta < $MinAgeSec"
205 debug_echo
"remove by archive size: $file, delta=$delta >= $MinAgeSec (sec), size=$size >= $MaxSize"
213 # deal with the Apt::Periodic::BackupArchiveInterval
216 BackupArchiveInterval
="$1"
217 if [ $BackupArchiveInterval -eq 0 ]; then
221 # Set default values and normalize
223 eval $(apt-config shell Dir Dir)
226 CacheDir
="var/cache/apt/"
227 eval $(apt-config shell CacheDir Dir::Cache)
228 CacheDir
=${CacheDir%/}
229 if [ -z "$CacheDir" ]; then
230 debug_echo
"practically empty Dir::Cache, exiting"
234 CacheArchive
="archives/"
235 eval $(apt-config shell CacheArchive Dir::Cache::Archives)
236 CacheArchive
=${CacheArchive%/}
237 if [ -z "$CacheArchive" ]; then
238 debug_echo
"practically empty Dir::Cache::archives, exiting"
243 eval $(apt-config shell BackupLevel APT::Periodic::BackupLevel)
244 if [ $BackupLevel -le 1 ]; then
248 CacheBackup
="backup/"
249 eval $(apt-config shell CacheBackup Dir::Cache::Backup)
250 CacheBackup
=${CacheBackup%/}
251 if [ -z "$CacheBackup" ]; then
252 echo "practically empty Dir::Cache::Backup, exiting" 1>&2
256 Cache
="${Dir}/${CacheDir}/${CacheArchive}/"
257 Back
="${Dir}/${CacheDir}/${CacheBackup}/"
258 BackX
="${Back}${CacheArchive}/"
259 for x
in $(seq 0 1 $((${BackupLevel}-1))); do
260 eval "Back${x}=${Back}${x}/"
263 # backup after n-days if archive contents changed.
264 # (This uses hardlink to save disk space)
265 BACKUP_ARCHIVE_STAMP
=/var
/lib
/apt
/periodic
/backup
-archive-stamp
266 if check_stamp
$BACKUP_ARCHIVE_STAMP $BackupArchiveInterval; then
267 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
269 rm -rf $Back$((${BackupLevel}-1))
270 for y
in $(seq $((${BackupLevel}-1)) -1 1); do
272 eval BackZ
=${Back}$(($y-1))
273 if [ -e $BackZ ]; then
274 mv -f $BackZ $BackY ;
277 cp -la $Cache $Back ; mv -f $BackX $Back0
278 update_stamp
$BACKUP_ARCHIVE_STAMP
279 debug_echo
"backup with hardlinks. (success)"
281 debug_echo
"skip backup since same content."
284 debug_echo
"skip backup since too new."
288 # sleep for a random interval of time (default 30min)
289 # (some code taken from cron-apt, thanks)
293 eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
294 if [ $RandomSleep -eq 0 ]; then
297 if [ -z "$RANDOM" ] ; then
298 # A fix for shells that do not have this bash feature.
299 RANDOM
=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -c"1-5")
301 TIME
=$(($RANDOM % $RandomSleep))
302 debug_echo
"sleeping for $TIME seconds"
309 # Display message if $VERBOSE >= 1
310 if [ "$VERBOSE" -ge 1 ]; then
315 # ------------------------ main ----------------------------
317 # check apt-config exstance
318 if ! which apt
-config >/dev
/null
; then
322 # Set VERBOSE mode from apt-config (or inherit from environment)
323 eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
324 debug_echo
"verbose level $VERBOSE"
325 if [ -z "$VERBOSE" ]; then
328 if [ "$VERBOSE" -le 2 ]; then
331 XSTDERR
="2>/dev/null"
340 if [ "$VERBOSE" -ge 3 ]; then
345 # laptop check, on_ac_power returns:
346 # 0 (true) System is on main power
347 # 1 (false) System is not on main power
348 # 255 (false) Power status could not be determined
349 # Desktop systems always return 255 it seems
350 if which on_ac_power
>/dev
/null
; then
353 if [ $POWER -eq 1 ]; then
354 debug_echo
"exit: system NOT on main power"
356 elif [ $POWER -ne 0 ]; then
357 debug_echo
"power status ($POWER) undetermined, continuing"
359 debug_echo
"system is on main power."
362 # check if we can lock the cache and if the cache is clean
363 if which apt
-get >/dev
/null
&& ! eval apt
-get check
-f $XAPTOPT $XSTDERR ; then
364 debug_echo
"error encountered in cron job with \"apt-get check\"."
368 # Global current time in seconds since 1970-01-01 00:00:00 UTC
371 # Support old Archive for compatibility.
372 # Document only Periodic for all controling parameters of this script.
375 eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists)
377 DownloadUpgradeableInterval
=0
378 eval $(apt-config shell DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
380 UnattendedUpgradeInterval
=0
381 eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
384 eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
386 BackupArchiveInterval
=0
387 eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
389 # check if we actually have to do anything
390 if [ $UpdateInterval -eq 0 ] &&
391 [ $DownloadUpgradeableInterval -eq 0 ] &&
392 [ $UnattendedUpgradeInterval -eq 0 ] &&
393 [ $BackupArchiveInterval -eq 0 ] &&
394 [ $AutocleanInterval -eq 0 ]; then
398 # deal with BackupArchiveInterval
399 do_cache_backup
$BackupArchiveInterval
401 # sleep random amount of time to avoid hitting the
402 # mirrors at the same time
405 # update package lists
407 UPDATE_STAMP
=/var
/lib
/apt
/periodic
/update
-stamp
408 if check_stamp
$UPDATE_STAMP $UpdateInterval; then
409 if eval apt
-get $XAPTOPT -y update
$XSTDERR; then
410 debug_echo
"download updated metadata (success)."
411 if which dbus
-send >/dev
/null
&& pidof dbus
-daemon >/dev
/null
; then
412 if dbus
-send --system / app.apt.dbus.updated boolean
:true
; then
413 debug_echo
"send dbus signal (success)"
415 debug_echo
"send dbus signal (error)"
418 debug_echo
"dbus signal not send (command not available)"
420 update_stamp
$UPDATE_STAMP
423 debug_echo
"download updated metadata (error)"
426 debug_echo
"download updated metadata (not run)."
429 # download all upgradeable packages (if it is requested)
430 DOWNLOAD_UPGRADEABLE_STAMP
=/var
/lib
/apt
/periodic
/download
-upgradeable-stamp
431 if [ $UPDATED -eq 1 ] && check_stamp
$DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
432 if eval apt
-get $XAPTOPT -y -d dist
-upgrade $XSTDERR; then
433 update_stamp
$DOWNLOAD_UPGRADEABLE_STAMP
434 debug_echo
"download upgradable (success)"
436 debug_echo
"download upgradable (error)"
439 debug_echo
"download upgradable (not run)"
442 # auto upgrade all upgradeable packages
443 UPGRADE_STAMP
=/var
/lib
/apt
/periodic
/upgrade
-stamp
444 if [ $UPDATED -eq 1 ] && which unattended
-upgrade >/dev
/null
&& check_stamp
$UPGRADE_STAMP $UnattendedUpgradeInterval; then
445 if unattended
-upgrade $XUUPOPT; then
446 update_stamp
$UPGRADE_STAMP
447 debug_echo
"unattended-upgrade (success)"
449 debug_echo
"unattended-upgrade (error)"
452 debug_echo
"unattended-upgrade (not run)"
455 # autoclean package archive
456 AUTOCLEAN_STAMP
=/var
/lib
/apt
/periodic
/autoclean
-stamp
457 if check_stamp
$AUTOCLEAN_STAMP $AutocleanInterval; then
458 if eval apt
-get $XAPTOPT -y autoclean
$XSTDERR; then
459 debug_echo
"autoclean (success)."
460 update_stamp
$AUTOCLEAN_STAMP
462 debug_echo
"autoclean (error)"
465 debug_echo
"autoclean (not run)"
469 check_size_constraints
472 # vim: set sts=4 ai :